1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#[cfg(target_arch = "wasm32")]
pub use blake2b_ref::{Blake2b, Blake2bBuilder};
#[cfg(not(target_arch = "wasm32"))]
pub use blake2b_rs::{Blake2b, Blake2bBuilder};
#[doc(hidden)]
pub const BLAKE2B_KEY: &[u8] = &[];
pub const BLAKE2B_LEN: usize = 32;
pub const CKB_HASH_PERSONALIZATION: &[u8] = b"ckb-default-hash";
pub const BLANK_HASH: [u8; 32] = [
68, 244, 198, 151, 68, 213, 248, 197, 93, 100, 32, 98, 148, 157, 202, 228, 155, 196, 231, 239,
67, 211, 136, 197, 161, 47, 66, 181, 99, 61, 22, 62,
];
pub fn new_blake2b() -> Blake2b {
Blake2bBuilder::new(32)
.personal(CKB_HASH_PERSONALIZATION)
.build()
}
pub fn blake2b_256<T: AsRef<[u8]>>(s: T) -> [u8; 32] {
if s.as_ref().is_empty() {
return BLANK_HASH;
}
inner_blake2b_256(s)
}
fn inner_blake2b_256<T: AsRef<[u8]>>(s: T) -> [u8; 32] {
let mut result = [0u8; 32];
let mut blake2b = new_blake2b();
blake2b.update(s.as_ref());
blake2b.finalize(&mut result);
result
}
#[test]
fn empty_blake2b() {
let actual = inner_blake2b_256([]);
assert_eq!(actual, BLANK_HASH);
}