presents
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() {
key := make([]byte, 10)
p, err := presents.New(key, nil)
if err != nil {
log.Fatal(err)
}
s := p.Wrap(1213486160)
fmt.Println(s)
n, err := p.Unwrap(s)
if err != nil {
log.Fatal(err)
}
fmt.Println(n)
}
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
- 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)