rqbit/crates/sha1w/src/lib.rs

92 lines
1.9 KiB
Rust
Raw Normal View History

// Wrapper for sha1 libraries.
// Sha1 computation is the majority of CPU usage of this library.
// openssl seems 2-3x faster, so using it for now, but
// leaving the pure-rust impl here too. Maybe someday make them
2021-07-01 17:18:07 +01:00
// runtime swappable or enabled with a feature.
#[cfg(feature = "sha1-openssl")]
2021-07-03 19:10:59 +01:00
pub type Sha1 = Sha1Openssl;
#[cfg(feature = "sha1-rust")]
2021-07-03 19:10:59 +01:00
pub type Sha1 = Sha1Rust;
#[cfg(feature = "sha1-system")]
2021-07-03 19:10:59 +01:00
pub type Sha1 = Sha1System;
pub trait ISha1 {
fn new() -> Self;
fn update(&mut self, buf: &[u8]);
fn finish(self) -> [u8; 20];
}
2021-07-01 19:17:44 +01:00
#[cfg(feature = "sha1-rust")]
pub struct Sha1Rust {
inner: sha1::Sha1,
}
2021-07-01 19:17:44 +01:00
#[cfg(feature = "sha1-rust")]
impl ISha1 for Sha1Rust {
fn new() -> Self {
Sha1Rust {
inner: sha1::Sha1::new(),
}
}
fn update(&mut self, buf: &[u8]) {
self.inner.update(buf)
}
fn finish(self) -> [u8; 20] {
self.inner.digest().bytes()
}
}
2021-07-01 19:17:44 +01:00
#[cfg(feature = "sha1-openssl")]
pub struct Sha1Openssl {
inner: openssl::sha::Sha1,
}
2021-07-01 19:17:44 +01:00
#[cfg(feature = "sha1-openssl")]
impl ISha1 for Sha1Openssl {
fn new() -> Self {
Self {
inner: openssl::sha::Sha1::new(),
}
}
fn update(&mut self, buf: &[u8]) {
self.inner.update(buf)
}
fn finish(self) -> [u8; 20] {
self.inner.finish()
}
}
2021-07-01 17:18:07 +01:00
2021-07-01 19:17:44 +01:00
#[cfg(feature = "sha1-system")]
2021-07-01 17:18:07 +01:00
pub struct Sha1System {
inner: crypto_hash::Hasher,
}
2021-07-01 19:17:44 +01:00
#[cfg(feature = "sha1-system")]
2021-07-01 17:18:07 +01:00
impl ISha1 for Sha1System {
fn new() -> Self {
Self {
inner: crypto_hash::Hasher::new(crypto_hash::Algorithm::SHA1),
}
}
fn update(&mut self, buf: &[u8]) {
2021-07-02 10:21:19 +01:00
use std::io::Write;
2021-07-01 17:18:07 +01:00
self.inner.write_all(buf).unwrap();
}
fn finish(mut self) -> [u8; 20] {
let result = self.inner.finish();
debug_assert_eq!(result.len(), 20);
2021-07-01 17:18:07 +01:00
let mut result_arr = [0u8; 20];
result_arr.copy_from_slice(&result);
result_arr
}
}