rigel is a HMAC-SHA512 implementation optimized for embedded devices. The first version didn't support streaming messages, but somebody pointed out the importance of this and it's since been added.
I think you could make the code a bit more DRY defining module-level types like [u8; 128] and [u8; 64]. I'd like to see more Rust libraries that try to minimize better the heap allocations
2 Likes
Thanks for taking a look at the code. I'm sorry I don't really see how that would DRY my code, but I haven't really used module-level types for these things before. Could you give me an example?
Your code contains:
fn pad_key_to_ipad(key: &[u8]) -> [u8; 128] {
let mut padded_key = [0x36; 128];
//...
padded_key
}
pub fn hmac_sha512(key: &[u8], message: &[u8]) -> [u8; 64] {
//...
let mut mac: [u8; 64] = [0u8; 64];
//...
mac
}
pub struct HmacSha512 {
buffer: [u8; 128],
hasher: Sha512
}
If you define two types like this (their name should be improved):
type Ipad = [u8; 128];
type Mac = [u8; 64];
You can then write:
fn pad_key_to_ipad(key: &[u8]) -> Ipad {
let mut padded_key = [0x36; 128];
//...
padded_key
}
pub fn hmac_sha512(key: &[u8], message: &[u8]) -> Mac {
//...
let mut mac: Mac = [0; 64];
//...
mac
}
pub struct HmacSha512 {
buffer: Ipad,
hasher: Sha512
}
Eventually I hope Rust will allow more DRY code like this:
let mut mac = Mac::default();
1 Like
That'll make the meaning behind the numbers a lot clearer too, thanks!