github.com/Aoi-hosizora/ahlib-more@v1.5.1-0.20230404072844-256112befaf6/xcrypto/xcrypto_test.go (about) 1 package xcrypto 2 3 import ( 4 "github.com/Aoi-hosizora/ahlib/xtesting" 5 "testing" 6 ) 7 8 func TestUintHasher(t *testing.T) { 9 for _, tc := range []struct { 10 giveFn func(string) uint32 11 giveText string 12 want uint32 13 }{ 14 {FNV32, "", 0x811c9dc5}, 15 {FNV32, "test", 0xbc2c0be9}, 16 {FNV32, "hello world", 0x548da96f}, 17 {FNV32, "测试 テス тест", 0x15a513dc}, 18 {FNV32a, "", 0x811c9dc5}, 19 {FNV32a, "test", 0xafd071e5}, 20 {FNV32a, "hello world", 0xd58b3fa7}, 21 {FNV32a, "测试 テス тест", 0x1e0ace72}, 22 {CRC32, "", 0x0}, 23 {CRC32, "test", 0xd87f7e0c}, 24 {CRC32, "hello world", 0xd4a1185}, 25 {CRC32, "测试 テス тест", 0xa699b2d6}, 26 {ADLER32, "", 0x1}, 27 {ADLER32, "test", 0x45d01c1}, 28 {ADLER32, "hello world", 0x1a0b045d}, 29 {ADLER32, "测试 テス тест", 0xa43d0e1a}, 30 } { 31 xtesting.Equal(t, tc.giveFn(tc.giveText), tc.want) 32 } 33 34 for _, tc := range []struct { 35 giveFn func(string) uint64 36 giveText string 37 want uint64 38 }{ 39 {FNV64, "", 0xcbf29ce484222325}, 40 {FNV64, "test", 0x8c093f7e9fccbf69}, 41 {FNV64, "hello world", 0x7dcf62cdb1910e6f}, 42 {FNV64, "测试 テス тест", 0xefa05d5a0bc1da7c}, 43 {FNV64a, "", 0xcbf29ce484222325}, 44 {FNV64a, "test", 0xf9e6e6ef197c2b25}, 45 {FNV64a, "hello world", 0x779a65e7023cd2e7}, 46 {FNV64a, "测试 テス тест", 0xa8009ce94a3ad872}, 47 {CRC64, "", 0x0}, 48 {CRC64, "test", 0x287c72c850000000}, 49 {CRC64, "hello world", 0xb9cf3f572ad9ac3e}, 50 {CRC64, "测试 テス тест", 0xe16038d3f4fca746}, 51 } { 52 xtesting.Equal(t, tc.giveFn(tc.giveText), tc.want) 53 } 54 } 55 56 func TestStringHasher(t *testing.T) { 57 for _, tc := range []struct { 58 giveFn func(string) string 59 giveText string 60 wantText string 61 }{ 62 {FNV128, "", "6c62272e07bb014262b821756295c58d"}, 63 {FNV128, "test", "66ab2a8b6f757277b806e89c56faf339"}, 64 {FNV128, "hello world", "e1b1650f0631aef5566634b6c074ac1f"}, 65 {FNV128a, "", "6c62272e07bb014262b821756295c58d"}, 66 {FNV128a, "test", "69d061a9c5757277b806e99413dd99a5"}, 67 {FNV128a, "hello world", "6c155799fdc8eec4b91523808e7726b7"}, 68 {MD4, "", "31d6cfe0d16ae931b73c59d7e0c089c0"}, 69 {MD4, "test", "db346d691d7acc4dc2625db19f9e3f52"}, 70 {MD4, "hello world", "aa010fbc1d14c795d86ef98c95479d17"}, 71 {MD5, "", "d41d8cd98f00b204e9800998ecf8427e"}, 72 {MD5, "test", "098f6bcd4621d373cade4e832627b4f6"}, 73 {MD5, "hello world", "5eb63bbbe01eeed093cb22bb8f5acdc3"}, 74 {SHA1, "", "da39a3ee5e6b4b0d3255bfef95601890afd80709"}, 75 {SHA1, "test", "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"}, 76 {SHA1, "hello world", "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"}, 77 {SHA224, "", "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f"}, 78 {SHA224, "test", "90a3ed9e32b2aaf4c61c410eb925426119e1a9dc53d4286ade99a809"}, 79 {SHA224, "hello world", "2f05477fc24bb4faefd86517156dafdecec45b8ad3cf2522a563582b"}, 80 {SHA256, "", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"}, 81 {SHA256, "test", "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"}, 82 {SHA256, "hello world", "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"}, 83 {SHA384, "", "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b"}, 84 {SHA384, "test", "768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf17a0a9"}, 85 {SHA384, "hello world", "fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd"}, 86 {SHA512, "", "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"}, 87 {SHA512, "test", "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"}, 88 {SHA512, "hello world", "309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f"}, 89 {SHA512_224, "", "6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4"}, 90 {SHA512_224, "test", "06001bf08dfb17d2b54925116823be230e98b5c6c278303bc4909a8c"}, 91 {SHA512_224, "hello world", "22e0d52336f64a998085078b05a6e37b26f8120f43bf4db4c43a64ee"}, 92 {SHA512_256, "", "c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a"}, 93 {SHA512_256, "test", "3d37fe58435e0d87323dee4a2c1b339ef954de63716ee79f5747f94d974f913f"}, 94 {SHA512_256, "hello world", "0ac561fac838104e3f2e4ad107b4bee3e938bf15f2b15f009ccccd61a913f017"}, 95 {SHA3_224, "", "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7"}, 96 {SHA3_224, "test", "3797bf0afbbfca4a7bbba7602a2b552746876517a7f9b7ce2db0ae7b"}, 97 {SHA3_224, "hello world", "dfb7f18c77e928bb56faeb2da27291bd790bc1045cde45f3210bb6c5"}, 98 {SHA3_256, "", "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"}, 99 {SHA3_256, "test", "36f028580bb02cc8272a9a020f4200e346e276ae664e45ee80745574e2f5ab80"}, 100 {SHA3_256, "hello world", "644bcc7e564373040999aac89e7622f3ca71fba1d972fd94a31c3bfbf24e3938"}, 101 {SHA3_384, "", "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004"}, 102 {SHA3_384, "test", "e516dabb23b6e30026863543282780a3ae0dccf05551cf0295178d7ff0f1b41eecb9db3ff219007c4e097260d58621bd"}, 103 {SHA3_384, "hello world", "83bff28dde1b1bf5810071c6643c08e5b05bdb836effd70b403ea8ea0a634dc4997eb1053aa3593f590f9c63630dd90b"}, 104 {SHA3_512, "", "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26"}, 105 {SHA3_512, "test", "9ece086e9bac491fac5c1d1046ca11d737b92a2b2ebd93f005d7b710110c0a678288166e7fbe796883a4f2e9b3ca9f484f521d0ce464345cc1aec96779149c14"}, 106 {SHA3_512, "hello world", "840006653e9ac9e95117a15c915caab81662918e925de9e004f774ff82d7079a40d4d27b1b372657c61d46d470304c88c788b3a4527ad074d1dccbee5dbaa99a"}, 107 } { 108 xtesting.Equal(t, tc.giveFn(tc.giveText), tc.wantText) 109 } 110 } 111 112 func TestEncodeDecode(t *testing.T) { 113 testStr := "test" 114 testBs := []byte(testStr) 115 helloWorldStr := "hello world" 116 helloWorldBs := []byte(helloWorldStr) 117 test2Str := "测试 テス тест" 118 test2Bs := []byte(test2Str) 119 120 for _, tc := range []struct { 121 giveFn1 func([]byte) []byte 122 giveFn2 func([]byte) string 123 give []byte 124 want string 125 }{ 126 {HexEncodeToBytes, HexEncodeToString, nil, ""}, 127 {HexEncodeToBytes, HexEncodeToString, testBs, "74657374"}, 128 {HexEncodeToBytes, HexEncodeToString, helloWorldBs, "68656c6c6f20776f726c64"}, 129 {HexEncodeToBytes, HexEncodeToString, test2Bs, "e6b58be8af9520e38386e382b920d182d0b5d181d182"}, 130 {Base32EncodeToBytes, Base32EncodeToString, nil, ""}, 131 {Base32EncodeToBytes, Base32EncodeToString, testBs, "ORSXG5A="}, 132 {Base32EncodeToBytes, Base32EncodeToString, helloWorldBs, "NBSWY3DPEB3W64TMMQ======"}, 133 {Base32EncodeToBytes, Base32EncodeToString, test2Bs, "422YX2FPSUQOHA4G4OBLSIGRQLILLUMB2GBA===="}, 134 {Base64EncodeToBytes, Base64EncodeToString, nil, ""}, 135 {Base64EncodeToBytes, Base64EncodeToString, testBs, "dGVzdA=="}, 136 {Base64EncodeToBytes, Base64EncodeToString, helloWorldBs, "aGVsbG8gd29ybGQ="}, 137 {Base64EncodeToBytes, Base64EncodeToString, test2Bs, "5rWL6K+VIOODhuOCuSDRgtC10YHRgg=="}, 138 } { 139 xtesting.Equal(t, string(tc.giveFn1(tc.give)), tc.want) 140 xtesting.Equal(t, tc.giveFn2(tc.give), tc.want) 141 } 142 143 for _, tc := range []struct { 144 giveFn1 func([]byte) ([]byte, error) 145 giveFn2 func(string) ([]byte, error) 146 give string 147 want string 148 }{ 149 {HexDecodeFromBytes, HexDecodeFromString, "", ""}, 150 {HexDecodeFromBytes, HexDecodeFromString, "74657374", testStr}, 151 {HexDecodeFromBytes, HexDecodeFromString, "68656c6c6f20776f726c64", helloWorldStr}, 152 {HexDecodeFromBytes, HexDecodeFromString, "e6b58be8af9520e38386e382b920d182d0b5d181d182", test2Str}, 153 {Base32DecodeFromBytes, Base32DecodeFromString, "", ""}, 154 {Base32DecodeFromBytes, Base32DecodeFromString, "ORSXG5A=", testStr}, 155 {Base32DecodeFromBytes, Base32DecodeFromString, "NBSWY3DPEB3W64TMMQ======", helloWorldStr}, 156 {Base32DecodeFromBytes, Base32DecodeFromString, "422YX2FPSUQOHA4G4OBLSIGRQLILLUMB2GBA====", test2Str}, 157 {Base64DecodeFromBytes, Base64DecodeFromString, "", ""}, 158 {Base64DecodeFromBytes, Base64DecodeFromString, "dGVzdA==", testStr}, 159 {Base64DecodeFromBytes, Base64DecodeFromString, "aGVsbG8gd29ybGQ=", helloWorldStr}, 160 {Base64DecodeFromBytes, Base64DecodeFromString, "5rWL6K+VIOODhuOCuSDRgtC10YHRgg==", test2Str}, 161 } { 162 bs1, err := tc.giveFn1([]byte(tc.give)) 163 xtesting.Nil(t, err) 164 xtesting.Equal(t, string(bs1), tc.want) 165 166 bs2, err := tc.giveFn2(tc.give) 167 xtesting.Nil(t, err) 168 xtesting.Equal(t, string(bs2), tc.want) 169 } 170 } 171 172 func TestPKCS5(t *testing.T) { 173 for _, tc := range []struct { 174 giveData []byte 175 giveSize int 176 wantAligned []byte 177 wantPanic bool 178 }{ 179 {[]byte{}, 1, []byte{0x1}, false}, 180 {[]byte{}, 4, []byte{0x4, 0x4, 0x4, 0x4}, false}, 181 {[]byte{'t', 'e', 's', 't'}, 1, []byte{'t', 'e', 's', 't', 0x1}, false}, 182 {[]byte{'t', 'e', 's', 't'}, 5, []byte{'t', 'e', 's', 't', 0x1}, false}, 183 {[]byte{'t', 'e', 's', 't'}, 2, []byte{'t', 'e', 's', 't', 0x2, 0x2}, false}, 184 {[]byte{'t', 'e', 's', 't'}, 7, []byte{'t', 'e', 's', 't', 0x3, 0x3, 0x3}, false}, 185 {[]byte{'t', 'e', 's', 't'}, 4, []byte{'t', 'e', 's', 't', 0x4, 0x4, 0x4, 0x4}, false}, 186 {[]byte{'t', 'e', 's', 't', ' '}, 9, []byte{'t', 'e', 's', 't', ' ', 0x4, 0x4, 0x4, 0x4}, false}, 187 {[]byte{'t', 'e', 's', 't'}, 0, nil, true}, 188 {[]byte{'t', 'e', 's', 't'}, -1, nil, true}, 189 } { 190 if tc.wantPanic { 191 xtesting.Panic(t, func() { PKCS5Padding(tc.giveData, tc.giveSize) }) 192 } else { 193 xtesting.Equal(t, PKCS5Padding(tc.giveData, tc.giveSize), tc.wantAligned) 194 xtesting.Equal(t, PKCS5Trimming(tc.wantAligned), tc.giveData) 195 } 196 } 197 } 198 199 func TestBcrypt(t *testing.T) { 200 _ = BcryptMaxCost // too slow 201 202 for _, tc := range []struct { 203 givePass string 204 giveCost int 205 useDefault bool 206 }{ 207 {"", 0, false}, // -> BcryptMinCost 208 {"test", 0, false}, 209 {"hello world", 0, false}, 210 {"", BcryptMinCost, false}, 211 {"test", BcryptMinCost, false}, 212 {"hello world", BcryptMinCost, false}, 213 {"", BcryptDefaultCost, false}, 214 {"test", BcryptDefaultCost, false}, 215 {"hello world", BcryptDefaultCost, false}, 216 {"", 0, true}, 217 {"test", 0, true}, 218 {"hello world", 0, true}, 219 } { 220 pass := []byte(tc.givePass) 221 var encrypted []byte 222 if tc.useDefault { 223 var err error 224 encrypted, err = BcryptEncrypt(pass, tc.giveCost) 225 xtesting.Nil(t, err) 226 } else { 227 var err error 228 encrypted, err = BcryptEncryptWithDefaultCost(pass) 229 xtesting.Nil(t, err) 230 } 231 232 ok, err := BcryptCompare(pass, encrypted) 233 xtesting.True(t, ok) 234 xtesting.Nil(t, err) 235 236 ok, err = BcryptCompare([]byte("fake password"), encrypted) 237 xtesting.False(t, ok) 238 xtesting.Nil(t, err) 239 240 ok, err = BcryptCompare(pass, []byte{}) 241 xtesting.False(t, ok) 242 xtesting.NotNil(t, err) 243 } 244 }