cryptoxide/sha1.rs
1//! An implementation of the SHA-1 cryptographic hash algorithm.
2//!
3//! it is however discouraged to use this algorithm in any application as is, as this is
4//! not considered secured anymore. the algorithm is deprecated since 2011, and chosen prefix
5//! attack are practical.
6//!
7//! However the hash function is still pervasively used in other contextes where security is still
8//! ok (e.g. hmac-sha1), so on this basis is available here.
9//!
10//! # Example
11//!
12//! ```
13//! use cryptoxide::{sha1::Sha1, digest::Digest};
14//!
15//! let mut digest = [0u8; 20];
16//! let mut context = Sha1::new();
17//! context.input(b"hello world");
18//! context.result(&mut digest);
19//! ```
20
21use crate::digest::Digest;
22use crate::hashing::sha1;
23
24/// Structure representing the state of a Sha1 computation
25#[derive(Clone)]
26pub struct Sha1 {
27 ctx: sha1::Context,
28 computed: bool,
29}
30
31impl Sha1 {
32 /// Construct a `sha` object
33 pub const fn new() -> Sha1 {
34 Sha1 {
35 ctx: sha1::Sha1::new(),
36 computed: false,
37 }
38 }
39}
40
41impl Digest for Sha1 {
42 fn reset(&mut self) {
43 self.ctx.reset();
44 self.computed = false;
45 }
46 fn input(&mut self, msg: &[u8]) {
47 assert!(!self.computed, "context is already finalized, needs reset");
48 self.ctx.update_mut(msg);
49 }
50 fn result(&mut self, slice: &mut [u8]) {
51 assert!(!self.computed, "context is already finalized, needs reset");
52 self.computed = true;
53 slice.copy_from_slice(&self.ctx.finalize_reset());
54 }
55 fn output_bits(&self) -> usize {
56 sha1::Sha1::OUTPUT_BITS
57 }
58 fn block_size(&self) -> usize {
59 sha1::Sha1::BLOCK_BYTES
60 }
61}