github.com/emmansun/gmsm@v0.29.1/zuc/example_test.go (about) 1 package zuc_test 2 3 import ( 4 "crypto/rand" 5 "encoding/hex" 6 "fmt" 7 "io" 8 9 "github.com/emmansun/gmsm/zuc" 10 ) 11 12 func ExampleNewCipher() { 13 // Load your secret key from a safe place and reuse it across multiple 14 // NewCipher calls. (Obviously don't use this example key for anything 15 // real.) If you want to convert a passphrase to a key, use a suitable 16 // package like bcrypt or scrypt. 17 key, _ := hex.DecodeString("6368616e676520746869732070617373") 18 plaintext := []byte("some plaintext") 19 20 const ivSize = zuc.IVSize128 21 // The IV needs to be unique, but not secure. Therefore it's common to 22 // include it at the beginning of the ciphertext. 23 ciphertext := make([]byte, ivSize+len(plaintext)) 24 iv := ciphertext[:ivSize] 25 if _, err := io.ReadFull(rand.Reader, iv); err != nil { 26 panic(err) 27 } 28 29 stream, err := zuc.NewCipher(key, iv) 30 if err != nil { 31 panic(err) 32 } 33 stream.XORKeyStream(ciphertext[ivSize:], plaintext) 34 35 // It's important to remember that ciphertexts must be authenticated 36 // (i.e. by using crypto/hmac) as well as being encrypted in order to 37 // be secure. 38 39 // Stream cipher is the same for both encryption and decryption, so we can 40 // also decrypt that ciphertext with NewCTR. 41 42 plaintext2 := make([]byte, len(plaintext)) 43 stream, err = zuc.NewCipher(key, iv) 44 if err != nil { 45 panic(err) 46 } 47 stream.XORKeyStream(plaintext2, ciphertext[ivSize:]) 48 49 fmt.Printf("%s\n", plaintext2) 50 // Output: some plaintext 51 } 52 53 func ExampleNewCipher_zuc256() { 54 // Load your secret key from a safe place and reuse it across multiple 55 // NewCipher calls. (Obviously don't use this example key for anything 56 // real.) If you want to convert a passphrase to a key, use a suitable 57 // package like bcrypt or scrypt. 58 key, _ := hex.DecodeString("6368616e6765207468697320706173736368616e676520746869732070617373") 59 plaintext := []byte("some plaintext") 60 61 const ivSize = zuc.IVSize256 62 // The IV needs to be unique, but not secure. Therefore it's common to 63 // include it at the beginning of the ciphertext. 64 ciphertext := make([]byte, ivSize+len(plaintext)) 65 iv := ciphertext[:ivSize] 66 if _, err := io.ReadFull(rand.Reader, iv); err != nil { 67 panic(err) 68 } 69 70 stream, err := zuc.NewCipher(key, iv) 71 if err != nil { 72 panic(err) 73 } 74 stream.XORKeyStream(ciphertext[ivSize:], plaintext) 75 76 // It's important to remember that ciphertexts must be authenticated 77 // (i.e. by using crypto/hmac) as well as being encrypted in order to 78 // be secure. 79 80 // Stream cipher is the same for both encryption and decryption, so we can 81 // also decrypt that ciphertext with NewCTR. 82 83 plaintext2 := make([]byte, len(plaintext)) 84 stream, err = zuc.NewCipher(key, iv) 85 if err != nil { 86 panic(err) 87 } 88 stream.XORKeyStream(plaintext2, ciphertext[ivSize:]) 89 90 fmt.Printf("%s\n", plaintext2) 91 // Output: some plaintext 92 } 93 94 func ExampleNewHash() { 95 // Load your secret key from a safe place and reuse it across multiple 96 // NewCipher calls. (Obviously don't use this example key for anything 97 // real.) If you want to convert a passphrase to a key, use a suitable 98 // package like bcrypt or scrypt. 99 key, _ := hex.DecodeString("6368616e676520746869732070617373") 100 101 // iv should be generated randomly 102 iv, _ := hex.DecodeString("6368616e676520746869732070617373") 103 104 h, err := zuc.NewHash(key, iv) 105 if err != nil { 106 panic(err) 107 } 108 h.Write([]byte("hello world\n")) 109 fmt.Printf("%x", h.Sum(nil)) 110 // Output: c43cd26a 111 } 112 113 func ExampleNewHash256_tagSize4() { 114 // Load your secret key from a safe place and reuse it across multiple 115 // NewCipher calls. (Obviously don't use this example key for anything 116 // real.) If you want to convert a passphrase to a key, use a suitable 117 // package like bcrypt or scrypt. 118 key, _ := hex.DecodeString("6368616e6765207468697320706173736368616e676520746869732070617373") 119 120 // iv should be generated randomly 121 iv, _ := hex.DecodeString("6368616e6765207468697320706173736368616e676520") 122 123 h, err := zuc.NewHash256(key, iv, 4) 124 if err != nil { 125 panic(err) 126 } 127 h.Write([]byte("hello world\n")) 128 fmt.Printf("%x", h.Sum(nil)) 129 // Output: b76f96ed 130 } 131 132 func ExampleNewHash256_tagSize8() { 133 // Load your secret key from a safe place and reuse it across multiple 134 // NewCipher calls. (Obviously don't use this example key for anything 135 // real.) If you want to convert a passphrase to a key, use a suitable 136 // package like bcrypt or scrypt. 137 key, _ := hex.DecodeString("6368616e6765207468697320706173736368616e676520746869732070617373") 138 139 // iv should be generated randomly 140 iv, _ := hex.DecodeString("6368616e6765207468697320706173736368616e676520") 141 142 h, err := zuc.NewHash256(key, iv, 8) 143 if err != nil { 144 panic(err) 145 } 146 h.Write([]byte("hello world\n")) 147 fmt.Printf("%x", h.Sum(nil)) 148 // Output: f28aea6c9db3dc69 149 } 150 151 func ExampleNewHash256_tagSize16() { 152 // Load your secret key from a safe place and reuse it across multiple 153 // NewCipher calls. (Obviously don't use this example key for anything 154 // real.) If you want to convert a passphrase to a key, use a suitable 155 // package like bcrypt or scrypt. 156 key, _ := hex.DecodeString("6368616e6765207468697320706173736368616e676520746869732070617373") 157 158 // iv should be generated randomly 159 iv, _ := hex.DecodeString("6368616e6765207468697320706173736368616e676520") 160 161 h, err := zuc.NewHash256(key, iv, 16) 162 if err != nil { 163 panic(err) 164 } 165 h.Write([]byte("hello world\n")) 166 fmt.Printf("%x", h.Sum(nil)) 167 // Output: fd8d10ea65b6369cccc07d50b4657d84 168 } 169 170 func ExampleZUC128Mac_Finish() { 171 key := make([]byte, 16) 172 iv := make([]byte, 16) 173 h, err := zuc.NewHash(key, iv) 174 if err != nil { 175 panic(err) 176 } 177 fmt.Printf("%x", h.Finish([]byte{0}, 1)) 178 // Output: c8a9595e 179 } 180 181 func ExampleZUC128Mac_Finish_mixed() { 182 key := []byte{ 183 0xc9, 0xe6, 0xce, 0xc4, 0x60, 0x7c, 0x72, 0xdb, 184 0x00, 0x0a, 0xef, 0xa8, 0x83, 0x85, 0xab, 0x0a, 185 } 186 187 // iv should be generated randomly 188 iv, _ := hex.DecodeString("a94059da50000000294059da50008000") 189 190 h, err := zuc.NewHash(key, iv) 191 if err != nil { 192 panic(err) 193 } 194 195 in, _ := hex.DecodeString("983b41d47d780c9e1ad11d7eb70391b1de0b35da2dc62f83e7b78d6306ca0ea07e941b7be91348f9fcb170e2217fecd97f9f68adb16e5d7d21e569d280ed775cebde3f4093c53881") 196 h.Write(in) 197 fmt.Printf("%x", h.Finish([]byte{0}, 1)) 198 // Output: fae8ff0b 199 }