Skip to content

Commit

Permalink
do detection only once
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Nov 28, 2019
1 parent 44ef648 commit 7cc074b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
7 changes: 7 additions & 0 deletions sha2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ fake-simd = "0.1"
opaque-debug = "0.2"
sha2-asm = { version="0.5", optional=true }

[dependencies.lazy_static]
version = "1.4.0"
default-features = false
# no_std feature is an anti-pattern. Why, lazy_static, why?
# See https://github.com/rust-lang-nursery/lazy-static.rs/issues/150
features = ["spin_no_std"]

[dev-dependencies]
digest = { version = "0.8", features = ["dev"] }
hex-literal = "0.1"
Expand Down
12 changes: 6 additions & 6 deletions sha2/src/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ use platform::Implementation;
type BlockSize = U64;
type Block = GenericArray<u8, BlockSize>;

lazy_static::lazy_static! {
static ref IMPL: Implementation = Implementation::detect();
}

/// A structure that represents that state of a digest computation for the
/// SHA-2 512 family of digest functions
#[derive(Clone)]
struct Engine256State {
h: [u32; 8],
implementation: Implementation,
}

impl Engine256State {
fn new(h: &[u32; STATE_LEN]) -> Engine256State {
Engine256State {
h: *h,
implementation: Implementation::detect(),
}
Engine256State { h: *h }
}

pub fn process_block(&mut self, block: &Block) {
let block = unsafe { &*(block.as_ptr() as *const [u8; 64]) };
self.implementation.compress256(&mut self.h, block);
IMPL.compress256(&mut self.h, block);
}
}

Expand Down

0 comments on commit 7cc074b

Please sign in to comment.