1#![no_std]
11
12#[cfg(feature = "ckb-contract")]
13pub use blake2b_ref::{Blake2b, Blake2bBuilder};
14
15#[cfg(all(not(feature = "ckb-contract"), target_family = "wasm"))]
16pub use blake2b_ref::{Blake2b, Blake2bBuilder};
17
18#[cfg(all(not(feature = "ckb-contract"), not(target_family = "wasm")))]
19pub use blake2b_rs::{Blake2b, Blake2bBuilder};
20
21#[doc(hidden)]
22pub const BLAKE2B_KEY: &[u8] = &[];
23pub const BLAKE2B_LEN: usize = 32;
25pub const CKB_HASH_PERSONALIZATION: &[u8] = b"ckb-default-hash";
27pub const BLANK_HASH: [u8; 32] = [
37 68, 244, 198, 151, 68, 213, 248, 197, 93, 100, 32, 98, 148, 157, 202, 228, 155, 196, 231, 239,
38 67, 211, 136, 197, 161, 47, 66, 181, 99, 61, 22, 62,
39];
40
41pub fn new_blake2b() -> Blake2b {
57 Blake2bBuilder::new(32)
58 .personal(CKB_HASH_PERSONALIZATION)
59 .build()
60}
61
62pub fn blake2b_256<T: AsRef<[u8]>>(s: T) -> [u8; 32] {
74 if s.as_ref().is_empty() {
75 return BLANK_HASH;
76 }
77 inner_blake2b_256(s)
78}
79
80fn inner_blake2b_256<T: AsRef<[u8]>>(s: T) -> [u8; 32] {
81 let mut result = [0u8; 32];
82 let mut blake2b = new_blake2b();
83 blake2b.update(s.as_ref());
84 blake2b.finalize(&mut result);
85 result
86}
87
88#[test]
89fn empty_blake2b() {
90 let actual = inner_blake2b_256([]);
91 assert_eq!(actual, BLANK_HASH);
92}