| 1 | import crypto.cipher |
| 2 | import crypto.aes |
| 3 | import crypto.des |
| 4 | |
| 5 | struct StreamCipher { |
| 6 | cipher cipher.Stream |
| 7 | } |
| 8 | |
| 9 | fn test_ofb_stream_cipher() ! { |
| 10 | key := '123456789012345678901234'.bytes() |
| 11 | iv := 'abcdegfh'.bytes() |
| 12 | |
| 13 | block := des.new_cipher(key[..8]) |
| 14 | c := cipher.new_ofb(block, iv) |
| 15 | |
| 16 | s := StreamCipher{ |
| 17 | cipher: c |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | fn test_ofb_byte_by_byte() { |
| 22 | key := []u8{len: 16, init: index} |
| 23 | iv := []u8{len: 16, init: index} |
| 24 | txt := []u8{len: 32, init: index} |
| 25 | mut out := []u8{len: 32} |
| 26 | |
| 27 | mut ofb := cipher.new_ofb(aes.new_cipher(key), iv) |
| 28 | for i in 0 .. 32 { |
| 29 | ofb.xor_key_stream(mut out[i..i + 1], txt[i..i + 1]) |
| 30 | } |
| 31 | assert out == [u8(10), 149, 9, 182, 69, 107, 246, 66, 249, 202, 158, 83, 202, 94, 228, 85, |
| 32 | 190, 246, 12, 182, 85, 194, 184, 92, 243, 121, 164, 215, 69, 34, 168, 124] |
| 33 | } |
| 34 | |