I'm trying to figure out how to add a field that is a cryptographic hash of other fields. I have:
pub struct Message {
pub size: usize,
pub id: MessageId,
pub attribute: MessageAttribute,
}
I'd like to have something like:
pub struct Message {
pub size: usize,
pub id: MessageId,
pub attribute: MessageAttribute,
pub hash: MessageHash,
}
pub struct MessageHash(pub Vec<u8>);
I could create another struct and then compute the hash when setting up the struct:
pub struct HashedMessage {
pub content: Message,
pub hash: MessageHash,
}
pub fn message_hash(data: &Message) -> MessageHash {
let mut hasher = DefaultHasher::new();
data.hash(&mut hasher);
MessageHash(hasher.finalize().to_vec())
}
let content = Message { /* ... */ };
let hash = hash_message(msg);
let msg = HashedMessage { content, hash };
This method introduces another struct, and I would have to change the codebase to use the new struct in place of the old one.
Another way I is to have a new method that receives each member of the struct, and then output the final struct with the hash field computed over the inputs. This seems reasonable. A possible option would be to set the hash to 0 in this method and then compute the hash using message_hash over the Message struct (the first one, with hash field embedded), and then assign it.
Is there another way? What would be an idiomatic, correct way to add a hash? These are messages that will be serialized and sent over the wire, so I can't have a method that will compute the hash every time.
serdecan be told to ignore thishashfield - Alexey LarionovDefaultHasheris not a cryptographic hash. - Shepmasterhashfield, you can simply writecontent(&self) -> &Messageandinto_content(self) -> Messageaccessors (or implementBorrowandFrom, if that suits your use case) and continue to useMessagewhereHashedMessageis not needed, using the wrapper only for the parts where a hash is necessary. (If the rest of the codebase does need a hashed message, obviously you'd have to change it anyway.) - trentclSha256Hasher:) - Nikola Knezevic