extern crate krypton; use krypton::hash::sha2::Sha256; use krypton::mac::Mac; use krypton::mac::hmac::Hmac; use krypton::utility::random::random_bytes; #[test] fn hmac_test() { let mut hmac = Hmac::::new(&[0x41; 60]); let mut tag: [u8; 32] = [0; 32]; hmac.update(&[0x41; 32]); hmac.digest(&mut tag); assert_eq!(tag[..], [ 0x33, 0xf5, 0x6a, 0x6b, 0x52, 0x69, 0xe9, 0xb0, 0x1e, 0x0d, 0x11, 0x2f, 0x3d, 0xae, 0x36, 0x9a, 0x8b, 0xa6, 0x53, 0x6f, 0x6d, 0xbb, 0x6a, 0x00, 0xfa, 0xca, 0x9f, 0x09, 0xf2, 0x44, 0xe9, 0x43][..]); hmac.update(&[0x41; 30]); hmac.digest(&mut tag); assert_eq!(tag[..], [ 0x6b, 0xa5, 0x52, 0x2a, 0x9b, 0xd6, 0x3b, 0xad, 0x63, 0x37, 0x00, 0x02, 0xa3, 0x98, 0x60, 0x54, 0x45, 0xb5, 0xab, 0xc4, 0x48, 0x41, 0x56, 0x9f, 0x1f, 0x04, 0x7b, 0x2f, 0x9c, 0x9e, 0xf1, 0x29][..]); hmac.reset(); hmac.digest(&mut tag); assert_eq!(tag[..], [ 0xf7, 0xc6, 0xa9, 0x41, 0xed, 0xb4, 0x1c, 0x76, 0xd9, 0xf7, 0x0f, 0xc6, 0x70, 0x33, 0xbb, 0x86, 0x25, 0x78, 0xb0, 0xbf, 0xd0, 0x32, 0x63, 0x22, 0x67, 0x13, 0x4f, 0x92, 0x86, 0xd4, 0x7b, 0xb5][..]); let mut secret: [u8; 68] = [0; 68]; random_bytes(&mut secret); let mut message: [u8; 32] = [0; 32]; random_bytes(&mut message); let mut hmac = Hmac::::new(&secret); hmac.update(&message); hmac.digest(&mut tag); assert!(hmac.verify(&tag)); }