admin管理员组文章数量:1287581
In Rust I have an impl Hash
from a third party crate. How do I calculate a cryptographic hash of it, e.g. SHA1 or SHA3?
As far as I can tell the hasher objects in the sha1
and sha3
crates don't implement the Hasher
trait, presumably because it's not really designed for cryptographic purposes (Hasher::finish()
returns u64
). However I don't see why they can't impl Hasher
and then just provide their own finish()
methods that return the full hash instead of u64
.
Does something like that exist?
Please avoid lectures about security; I am already fully aware.
In Rust I have an impl Hash
from a third party crate. How do I calculate a cryptographic hash of it, e.g. SHA1 or SHA3?
As far as I can tell the hasher objects in the sha1
and sha3
crates don't implement the Hasher
trait, presumably because it's not really designed for cryptographic purposes (Hasher::finish()
returns u64
). However I don't see why they can't impl Hasher
and then just provide their own finish()
methods that return the full hash instead of u64
.
Does something like that exist?
Please avoid lectures about security; I am already fully aware.
Share Improve this question edited Feb 23 at 21:46 Timmmm asked Feb 23 at 19:50 TimmmmTimmmm 97k80 gold badges409 silver badges579 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 5Ok I figured it out and it wasn't too hard, though I'm kind of surprised I'm the first person to run into this. Anyway...
use std::hash::{Hash, Hasher};
// Note you can import the `digest` crate directly if you don't want to
// depend on sha3. But since I'm using sha3 I just get it via the re-export.
use sha3::digest::{Output, Digest};
pub fn hash_digest<H: Hash, D: Digest>(hash: H, digest: D) -> Output<D> {
let mut digest_hasher = DigestHasher { digest };
hash.hash(&mut digest_hasher);
digest_hasher.digest.finalize()
}
pub struct DigestHasher<D: Digest> {
digest: D,
}
impl<D: Digest> Hasher for DigestHasher<D> {
fn finish(&self) -> u64 {
unimplemented!("Do not call finish()");
}
fn write(&mut self, bytes: &[u8]) {
self.digest.update(bytes);
}
}
#[cfg(test)]
mod test {
use super::*;
#[derive(Hash)]
struct TestHash {
name: String,
age: u64,
}
#[test]
fn test_hash_digest() {
let hash = TestHash { name: "dave".to_string(), age: 5 };
let digest = sha3::Sha3_256::new();
let output = hash_digest(hash, digest);
assert_eq!(format!("{output:#x}"), "8b8ae45bfe6457ca91313ef2dd51b2e94aa93615ab85e2f833f9c8cfaa81c183");
}
}
本文标签: rustHow to calculate the sha1sha3 hash of an impl HashStack Overflow
版权声明:本文标题:rust - How to calculate the sha1sha3 hash of an `impl Hash`? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741304658a2371295.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Hash
trait come into play here? – squiguy Commented Feb 23 at 21:11std::hash::Hash
is to create astd::hash::Hasher
(e.g.std::hash::SipHasher
) and then callthe_std_hash_Hash.hash(&mut the_sip_hasher)
, thenthe_sip_hasher.finalize()
.std::hash::Hasher
hasfinalize()
. Yeah it's kind of confusing. – Timmmm Commented Feb 23 at 21:31Hash
's portabilty. It's not really designed to be a stable interface outside of the current running process. How data is passed to theHasher
is generally an implementation detail and not to be replied upon. Could be fine for your use case but I wanted to mention it just in case. – kmdreko Commented Feb 23 at 22:50