forked from CodeChain-io/codechain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.rs
99 lines (89 loc) · 2.72 KB
/
hash.rs
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2018 Kodebox, Inc.
// This file is part of CodeChain.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use primitives::{H160, H256};
use rcrypto::digest::Digest;
use rcrypto::ripemd160::Ripemd160;
use rcrypto::sha1::Sha1;
use rcrypto::sha2::Sha256;
use rcrypto::sha3::Sha3;
/// RIPEMD160
#[inline]
pub fn ripemd160<T: AsRef<[u8]>>(s: T) -> H160 {
let input = s.as_ref();
let mut result = H160::default();
let mut hasher = Ripemd160::new();
hasher.input(input);
hasher.result(&mut *result);
result
}
/// SHA-1
#[inline]
pub fn sha1<T: AsRef<[u8]>>(s: T) -> H160 {
let input = s.as_ref();
let mut result = H160::default();
let mut hasher = Sha1::new();
hasher.input(input);
hasher.result(&mut *result);
result
}
/// SHA-256
#[inline]
pub fn sha256<T: AsRef<[u8]>>(s: T) -> H256 {
let input = s.as_ref();
let mut result = H256::default();
let mut hasher = Sha256::new();
hasher.input(input);
hasher.result(&mut *result);
result
}
/// KECCAK256
#[inline]
pub fn keccak256<T: AsRef<[u8]>>(s: T) -> H256 {
let input = s.as_ref();
let mut result = H256::default();
let mut hasher = Sha3::keccak256();
hasher.input(input);
hasher.result(&mut result);
result
}
#[cfg(test)]
mod tests {
use super::{keccak256, ripemd160, sha1, sha256};
#[test]
fn _ripemd160() {
let expected = "108f07b8382412612c048d07d13f814118445acd".into();
let result = ripemd160(b"hello");
assert_eq!(result, expected);
}
#[test]
fn _sha1() {
let expected = "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d".into();
let result = sha1(b"hello");
assert_eq!(result, expected);
}
#[test]
fn _sha256() {
let expected = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824".into();
let result = sha256(b"hello");
assert_eq!(result, expected);
}
#[test]
fn _keccak256() {
let expected = "1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8".into();
let result = keccak256(b"hello");
assert_eq!(result, expected);
}
}