Socket
Socket
Sign inDemoInstall

github.com/yi-jiayu/presents

Package Overview
Dependencies
0
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/yi-jiayu/presents

Package presents implements a block cipher-based method of converting 64-bit unsigned integers to and from strings. The intended application is towards the obfuscation of sequential database IDs. This example show how to encode and decode IDs. This example shows how to use a custom alphabet as well as shuffling it.


Version published

Readme

Source

presents

GoDoc Build Status codecov Go Report Card

Like hashids, but based on block ciphers.

Inspired by this StackOverflow answer suggesting using a block cipher to obfuscate IDs.

How it works

The PRESENT block cipher [1] operates on 8-byte (64-bit) blocks and supports key lengths of 80-bits or 128-bits. It can be used to create a reversible mapping from 64-bit integers to 64-bit integers.

The resultant 64-bit integer is then converted to and from a string using an arbitrary change of base algorithm and a provided alphabet.

Triple DES, which also has a 64-bit block size, can be used as well.

Usage

package main

import (
	"fmt"
	"log"

	"github.com/yi-jiayu/presents"
)

func main() {
	// 80-bit PRESENT block cipher key
	key := make([]byte, 10)
	p, err := presents.New(key, nil)
	// with 3DES instead of PRESENT
	// key := make([]byte, 24)
	// p, err := presents.NewTripleDES(key, nil)
	if err != nil {
		log.Fatal(err)
	}

	s := p.Wrap(1213486160)
	fmt.Println(s) // 90NyXHLckhA

	n, err := p.Unwrap(s)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(n) // 1213486160
}

Performance

Some benchmarks on my i5-7200U:

$ go test -bench . -benchtime 10s
goos: windows
goarch: amd64
pkg: github.com/yi-jiayu/presents
BenchmarkPresents_Wrap-4                 1000000             10344 ns/op
BenchmarkPresentsTripleDES_Wrap-4       20000000               621 ns/op
BenchmarkPresentsBlowFish_Wrap-4        50000000               347 ns/op
PASS
ok      github.com/yi-jiayu/presents    41.590s

References

  1. Bogdanov A. et al. (2007) PRESENT: An Ultra-Lightweight Block Cipher. In: Paillier P., Verbauwhede I. (eds) Cryptographic Hardware and Embedded Systems - CHES 2007. CHES 2007. Lecture Notes in Computer Science, vol 4727. Springer, Berlin, Heidelberg (pdf)

FAQs

Last updated on 07 Sep 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc