blake2_rfc_bellman_edition/
blake2b.rs

1// Copyright 2015 blake2-rfc Developers
2// Copyright 2017 Google Inc.
3//
4// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
5// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
6// http://opensource.org/licenses/MIT>, at your option. This file may not be
7// copied, modified, or distributed except according to those terms.
8
9//! The BLAKE2b hash function.
10//!
11//! # Examples
12//!
13//! ```
14//! use blake2_rfc::blake2b::{Blake2b, blake2b};
15//!
16//! // Using the convenience function.
17//! let hash = blake2b(64, &[], b"The quick brown fox jumps over the lazy dog");
18//!
19//! // Using the state context.
20//! let mut context = Blake2b::new(64);
21//! context.update(b"The quick brown fox jumps over the lazy dog");
22//! let hash = context.finalize();
23//!
24//! // Using the convenience function, with a key.
25//! let hash = blake2b(64, b"key", b"The quick brown fox jumps over the lazy dog");
26//!
27//! // Using the state context, with a key.
28//! let mut context = Blake2b::with_key(64, b"key");
29//! context.update(b"The quick brown fox jumps over the lazy dog");
30//! let hash = context.finalize();
31//! ```
32//!
33//! The returned hash is a `Blake2bResult`, which can be compared with
34//! a byte string (the comparison will take constant time), or converted
35//! into a byte string.
36
37#![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))]
38
39blake2_impl!(
40    Blake2b, Blake2bResult, blake2b, u64,
41    u64x4, read_u64, 64, 32, 24, 16, 63, [
42    0x6A09E667F3BCC908, 0xBB67AE8584CAA73B,
43    0x3C6EF372FE94F82B, 0xA54FF53A5F1D36F1,
44    0x510E527FADE682D1, 0x9B05688C2B3E6C1F,
45    0x1F83D9ABFB41BD6B, 0x5BE0CD19137E2179,
46]);
47
48blake2_selftest_impl!(Blake2b, blake2b, [
49    0xC2, 0x3A, 0x78, 0x00, 0xD9, 0x81, 0x23, 0xBD,
50    0x10, 0xF5, 0x06, 0xC6, 0x1E, 0x29, 0xDA, 0x56,
51    0x03, 0xD7, 0x63, 0xB8, 0xBB, 0xAD, 0x2E, 0x73,
52    0x7F, 0x5E, 0x76, 0x5A, 0x7B, 0xCC, 0xD4, 0x75,
53], [ 20, 32, 48, 64 ], [ 0, 3, 128, 129, 255, 1024 ]);
54
55#[cfg(test)]
56mod tests {
57    #![cfg_attr(feature = "cargo-clippy", allow(result_unwrap_used))]
58
59    extern crate data_encoding;
60    use self::data_encoding::HEXUPPER;
61
62    use blake2::selftest_seq;
63    use super::{Blake2b, blake2b};
64
65    #[test]
66    fn test_empty() {
67        assert_eq!(&blake2b(64, &[], b""), &HEXUPPER.decode(
68            b"786A02F742015903C6C6FD852552D272912F4740E15847618A86E217F71F5419D25E1031AFEE585313896444934EB04B903A685B1448B755D56F701AFE9BE2CE")
69            .unwrap()[..]);
70    }
71
72    #[test]
73    fn test_default() {
74        assert_eq!(&Blake2b::default().finalize(), &HEXUPPER.decode(
75            b"786A02F742015903C6C6FD852552D272912F4740E15847618A86E217F71F5419D25E1031AFEE585313896444934EB04B903A685B1448B755D56F701AFE9BE2CE")
76            .unwrap()[..]);
77    }
78
79    #[test]
80    fn selftest() {
81        super::selftest();
82    }
83
84    #[test]
85    fn test_split() {
86        let data = selftest_seq(512);
87
88        let mut ctx = Blake2b::new(64);
89        ctx.update(&data[..32]);
90        ctx.update(&data[32..64]);
91        ctx.update(&data[64..448]);
92        ctx.update(&data[448..]);
93
94        assert_eq!(&ctx.finalize(), &blake2b(64, &[], &data));
95    }
96
97    #[cfg(feature = "std")]
98    #[test]
99    fn test_write() {
100        use std::io::prelude::*;
101
102        let data = selftest_seq(1024);
103
104        let mut ctx = Blake2b::new(64);
105        ctx.update(&data[..]);
106
107        let mut writer = Blake2b::new(64);
108        writer.write_all(&data[..]).unwrap();
109
110        assert_eq!(&writer.finalize(), &ctx.finalize());
111    }
112
113    #[cfg_attr(debug_assertions, ignore)]
114    #[test]
115    fn test_4g() {
116        const ZEROS: [u8; 4096] = [0; 4096];
117
118        let mut state = Blake2b::new(64);
119        for _ in 0..1048576 {
120            state.update(&ZEROS);
121        }
122        assert_eq!(&state.finalize(), &HEXUPPER.decode(
123            b"645572CA5756F9104329ED543735FC11904F0C18C4DF8ADF930F22D07F3094919A519FF34FD240AE3F5D5B4C8042225C109FB951036FDC99E7D2CD0C1D36B267")
124            .unwrap()[..]);
125    }
126}