github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/encrypt/crypto/option_example.go (about) 1 //go:build ignore 2 3 package crypto 4 5 import ( 6 "github.com/jxskiss/base62" 7 "golang.org/x/text/encoding/charmap" 8 ) 9 10 func ExampleLatin1Option() { 11 ciphertext, err := CFBEncrypt(plaintext, key, Encoder(latin1ToUTF8)) 12 // ... 13 14 plaintext, err := CFBDecrypt(ciphertext, key, Decoder(utf8ToLatin1)) 15 // ... 16 } 17 18 // Be safe with legacy python code by convert arbitrary bytes to utf8. 19 20 func latin1ToUTF8(data []byte) ([]byte, error) { 21 return charmap.ISO8859_1.NewDecoder().Bytes(data) 22 } 23 24 func utf8ToLatin1(data []byte) ([]byte, error) { 25 return charmap.ISO8859_1.NewEncoder().Bytes(data) 26 } 27 28 func ExampleBase62Options() { 29 ciphertext, err := CFBEncrypt(plaintext, key, base62Option(base62.StdEncoding)) 30 // ... 31 32 plaintext, err := CFBDecrypt(ciphertext, key, base62Option(base62.StdEncoding)) 33 // ... 34 } 35 36 func base62Option(enc *base62.Encoding) Option { 37 if enc == nil { 38 enc = base62.StdEncoding 39 } 40 return func(opt *options) { 41 opt.encoder = func(src []byte) ([]byte, error) { 42 dst := enc.Encode(src) 43 return dst, nil 44 } 45 opt.decoder = func(src []byte) ([]byte, error) { 46 return enc.Decode(src) 47 } 48 } 49 }