Skip to content

Commit 7cc074b

Browse files
do detection only once
1 parent 44ef648 commit 7cc074b

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

sha2/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ fake-simd = "0.1"
1616
opaque-debug = "0.2"
1717
sha2-asm = { version="0.5", optional=true }
1818

19+
[dependencies.lazy_static]
20+
version = "1.4.0"
21+
default-features = false
22+
# no_std feature is an anti-pattern. Why, lazy_static, why?
23+
# See https://github.com/rust-lang-nursery/lazy-static.rs/issues/150
24+
features = ["spin_no_std"]
25+
1926
[dev-dependencies]
2027
digest = { version = "0.8", features = ["dev"] }
2128
hex-literal = "0.1"

sha2/src/sha256.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ use platform::Implementation;
1010
type BlockSize = U64;
1111
type Block = GenericArray<u8, BlockSize>;
1212

13+
lazy_static::lazy_static! {
14+
static ref IMPL: Implementation = Implementation::detect();
15+
}
16+
1317
/// A structure that represents that state of a digest computation for the
1418
/// SHA-2 512 family of digest functions
1519
#[derive(Clone)]
1620
struct Engine256State {
1721
h: [u32; 8],
18-
implementation: Implementation,
1922
}
2023

2124
impl Engine256State {
2225
fn new(h: &[u32; STATE_LEN]) -> Engine256State {
23-
Engine256State {
24-
h: *h,
25-
implementation: Implementation::detect(),
26-
}
26+
Engine256State { h: *h }
2727
}
2828

2929
pub fn process_block(&mut self, block: &Block) {
3030
let block = unsafe { &*(block.as_ptr() as *const [u8; 64]) };
31-
self.implementation.compress256(&mut self.h, block);
31+
IMPL.compress256(&mut self.h, block);
3232
}
3333
}
3434

0 commit comments

Comments
 (0)