v2 / vlib / crypto / cipher / aes_cfb_test.v
29 lines · 23 sloc · 866 bytes · fb192d949bd2e8f814c5826411ff61c92a96f796
Raw
1import crypto.aes
2import crypto.cipher
3
4fn test_aes_cfb() {
5 key := '6368616e676520746869732070617373'.bytes()
6 iv := '1234567890123456'.bytes()
7 str := '73c86d43a9d700a253a96c85b0f6b03ac9792e0e757f869cca306bd3cba1c62b'
8
9 mut src := str.bytes()
10
11 aes_cfb_en(mut src, key, iv)
12 assert src.hex() == '04380c1470ab2d8a0b3f9b4c1949b8ac57dfecca20ab539cd9862a262857ed3e4be1b1bb1d590f3c9eb760ef3d0c202b38e79ea53efbe4a3334f0a872e82f208'
13
14 aes_cfb_de(mut src, key, iv)
15 assert src.bytestr() == str
16 println('test_aes_cfb ok')
17}
18
19fn aes_cfb_en(mut src []u8, key []u8, iv []u8) {
20 block := aes.new_cipher(key)
21 mut mode := cipher.new_cfb_encrypter(block, iv)
22 mode.xor_key_stream(mut src, src.clone())
23}
24
25fn aes_cfb_de(mut src []u8, key []u8, iv []u8) {
26 block := aes.new_cipher(key)
27 mut mode := cipher.new_cfb_decrypter(block, iv)
28 mode.xor_key_stream(mut src, src.clone())
29}
30