github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/cry/example_test.go (about) 1 package cry_test 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/bingoohuang/gg/pkg/cry" 8 ) 9 10 func ExampleNewAESOpt() { 11 // sample plain text 12 sampleText := "Halo this is encrypted text!!!" 13 14 // it's random string must be hexa a-f & 0-9 15 const key = "fa89277fb1e1c344709190deeac4465c2b28396423c8534a90c86322d0ec9dcf" 16 // define AES option 17 aesOpt, err := cry.NewAESOpt(key) 18 if err != nil { 19 log.Println("ERR", err) 20 return 21 } 22 23 // Encrypt text using AES algorithm 24 cipherText, err := aesOpt.Encrypt([]byte(sampleText)) 25 if err != nil { 26 log.Println("ERR", err) 27 return 28 } 29 // 每次加密结果不一样(因为加入了随机的nonce) 30 // fmt.Println("Encrypted data", string(cipherText)) 31 // Encrypted data 28509ae15364b3994846eeb4079122825285ac7863f94fab43aacb43b5a81e2f93c58743c5d089ae4fefe468b1e240ac88cb50e9316a2f618d55 32 // Encrypted data b91a0019778033730704da5881a39c955f58c878f6a7938da107a2cbb9d752100af56024463d901c75b1ea6cb63089bf88e8b758b9786de9150e 33 34 // Decrypt text using AES algorithm 35 plainText, err := aesOpt.Decrypt([]byte(cipherText)) 36 if err != nil { 37 log.Println("ERR", err) 38 return 39 } 40 fmt.Println("Decrypted data", string(plainText)) 41 42 // Output: 43 // Decrypted data Halo this is encrypted text!!! 44 } 45 46 func ExampleNewDESOpt() { 47 // sample plain text 48 sampleText := "Halo this is encrypted text!!!" 49 // it's character 24 bit 50 const key = "123456781234567812345678" 51 // define DES option 52 desOpt, err := cry.NewDESOpt(key) 53 if err != nil { 54 log.Println("ERR", err) 55 return 56 } 57 58 // Encrypt text using DES algorithm 59 cipherText, err := desOpt.Encrypt([]byte(sampleText)) 60 if err != nil { 61 log.Println("ERR", err) 62 return 63 } 64 // fmt.Println("Encrypted data:", cipherText) 65 // 每次都得到一样的数据 66 // Encrypted data: k1Uoi4OsCMBSCeVxdBmwfVuO2PxndJZSfCsXIULB7F0= 67 68 // Decrypt text using DES algorithm 69 plainText, err := desOpt.Decrypt([]byte(cipherText)) 70 if err != nil { 71 log.Println("ERR", err) 72 return 73 } 74 fmt.Println("Decrypted data:", plainText) 75 // Output: 76 // Decrypted data: Halo this is encrypted text!!! 77 } 78 79 func ExampleNewRC4Opt() { 80 // sample plain text 81 sampleText := "Halo this is encrypted text!!!" 82 // it's character 24 bit 83 const key = "123456781234567812345678" 84 // define RC4 option 85 rc4Opt, err := cry.NewRC4Opt(key) 86 if err != nil { 87 log.Println("ERR", err) 88 return 89 } 90 91 // Encrypt text using RC4 algorithm 92 cipherText, err := rc4Opt.Encrypt([]byte(sampleText)) 93 if err != nil { 94 log.Println("ERR", err) 95 return 96 } 97 // fmt.Println("Encrypted data:", cipherText) 98 // 每次都得到一样的数据 99 // Encrypted data: f39255bb29c5b6ce831363bc865866c600e7ed3dac0c5dc13c63a196788c 100 101 // Decrypt text using RC4 algorithm 102 plainText, err := rc4Opt.Decrypt([]byte(cipherText)) 103 if err != nil { 104 log.Println("ERR", err) 105 return 106 } 107 fmt.Println("Decrypted data:", plainText) 108 // Output: 109 // Decrypted data: Halo this is encrypted text!!! 110 }