cryptoxide/
ripemd160.rs

1//! An implementation of the RIPEMD-160 cryptographic hash.
2//!
3//! RIPEMD (RIPE Message Digest) is a family of cryptographic hash functions
4//! developed in 1992 (the original RIPEMD) and 1996 (other variants). There are
5//! five functions in the family: RIPEMD, RIPEMD-128, RIPEMD-160, RIPEMD-256,
6//! and RIPEMD-320, of which RIPEMD-160 is the most common.
7//!
8//!
9//! ```
10//! use cryptoxide::{ripemd160::Ripemd160, digest::Digest};
11//!
12//! let mut digest = [0u8; 20];
13//! let mut context = Ripemd160::new();
14//!
15//! context.input(b"hello world");
16//! context.result(&mut digest);
17//! ```
18
19use crate::digest::Digest;
20use crate::hashing::ripemd160;
21
22/// Structure representing the state of a Ripemd160 computation
23#[derive(Clone)]
24pub struct Ripemd160 {
25    ctx: ripemd160::Context,
26    computed: bool,
27}
28
29impl Ripemd160 {
30    /// Construct a `sha` object
31    pub const fn new() -> Ripemd160 {
32        Ripemd160 {
33            ctx: ripemd160::Ripemd160::new(),
34            computed: false,
35        }
36    }
37}
38
39impl Digest for Ripemd160 {
40    fn reset(&mut self) {
41        self.ctx.reset();
42        self.computed = false;
43    }
44    fn input(&mut self, msg: &[u8]) {
45        assert!(!self.computed, "context is already finalized, needs reset");
46        self.ctx.update_mut(msg);
47    }
48    fn result(&mut self, slice: &mut [u8]) {
49        assert!(!self.computed, "context is already finalized, needs reset");
50        self.computed = true;
51        slice.copy_from_slice(&self.ctx.finalize_reset());
52    }
53    fn output_bits(&self) -> usize {
54        ripemd160::Ripemd160::OUTPUT_BITS
55    }
56    fn block_size(&self) -> usize {
57        ripemd160::Ripemd160::BLOCK_BYTES
58    }
59}