Documentation ¶
Overview ¶
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.
Example ¶
This example show how to encode and decode IDs.
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) 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) }
Output: 90NyXHLckhA 1213486160
Example (CustomAlphabet) ¶
This example shows how to use a custom alphabet as well as shuffling it.
package main import ( "fmt" "log" "github.com/yi-jiayu/presents" ) func main() { // 80-bit PRESENT block cipher key key := make([]byte, 10) options := &presents.Options{ Alphabet: "_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", Shuffle: true, Seed: 12, } p, err := presents.New(key, options) 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) }
Output: tiKYxtU_2ZB 1213486160
Index ¶
Examples ¶
Constants ¶
const DefaultAlphabet = alphabet("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
DefaultAlphabet contains printable characters from 0-9, A-Z and a-z, similar to a base62 encoding.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Presents ¶
type Presents struct {
// contains filtered or unexported fields
}
Presents contains a cipher.Block implementing PRESENT and an alphabet for converting between 64-bit integers and strings.
func New ¶
New creates a new Presents struct using the PRESENT block cipher. If options.Alphabet is not the empty string, it will be used as the alphabet. If options.Shuffle is true, the alphabet will be shuffled based on options.Seed.
func NewTripleDES ¶
NewTripleDES creates a new Presents struct using Triple DES instead of PRESENT. If options.Alphabet is not the empty string, it will be used as the alphabet. If options.Shuffle is true, the alphabet will be shuffled based on options.Seed.
Example ¶
This example shows how to use the triple DES cipher instead of PRESENT.
package main import ( "fmt" "log" "github.com/yi-jiayu/presents" ) func main() { // 24-byte triple DES key key := make([]byte, 24) p, err := presents.NewTripleDES(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) }
Output: Yq4drxXQtcJ 1213486160
func NewWithCipher ¶
NewWithCipher returns a new Presents instance from the provided cipher.Block and options. The provided cipher.Block should have a 64-bit block size.
Example ¶
You can also provide your own cipher.Block implementation as long as it has a 64-bit block size. This example shows how you can use the Blowfish cipher provided by the golang.org/x/crypto/blowfish package.
package main import ( "fmt" "log" "github.com/yi-jiayu/presents" "golang.org/x/crypto/blowfish" ) func main() { // 56-byte Blowfish key key := make([]byte, 56) c, err := blowfish.NewCipher(key) if err != nil { log.Fatal(err) } p, err := presents.NewWithCipher(c, 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) }
Output: woSdQdAYuiK 1213486160