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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use bitcoin_hashes::{Hash as BitcoinHash, HashEngine, Hmac, HmacEngine, hash160, hex::ToHex, ripemd160, sha1, sha256, sha256d, sha512};
use wasm_bindgen::prelude::*;
use serde::*;
use crate::utils::{from_hex, to_hex};
#[wasm_bindgen]
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Hash(
#[serde(serialize_with = "to_hex", deserialize_with = "from_hex")]
pub(crate) Vec<u8>
);
#[wasm_bindgen]
impl Hash {
#[wasm_bindgen(js_name = toBytes)]
pub fn to_bytes(&self) -> Vec<u8> {
self.0.clone()
}
#[wasm_bindgen(js_name = toHex)]
pub fn to_hex(&self) -> String {
self.0.to_hex()
}
}
#[wasm_bindgen]
impl Hash {
#[wasm_bindgen(js_name = sha256d)]
pub fn sha_256d(input: &[u8]) -> Self {
Hash(bitcoin_hashes::sha256d::Hash::hash(input).to_vec())
}
#[wasm_bindgen(js_name = sha256)]
pub fn sha_256(input: &[u8]) -> Self {
Hash(bitcoin_hashes::sha256::Hash::hash(input).to_vec())
}
#[wasm_bindgen(js_name = sha1)]
pub fn sha_1(input: &[u8]) -> Self {
Hash(bitcoin_hashes::sha1::Hash::hash(input).to_vec())
}
#[wasm_bindgen(js_name = ripemd160)]
pub fn ripemd_160(input: &[u8]) -> Self {
Hash(bitcoin_hashes::ripemd160::Hash::hash(input).to_vec())
}
#[wasm_bindgen(js_name = hash160)]
pub fn hash_160(input: &[u8]) -> Self {
Hash(bitcoin_hashes::hash160::Hash::hash(input).to_vec())
}
#[wasm_bindgen(js_name = sha512)]
pub fn sha_512(input: &[u8]) -> Self {
Hash(bitcoin_hashes::sha512::Hash::hash(input).to_vec())
}
}
#[wasm_bindgen]
impl Hash {
fn hmac<T>(input: &[u8], key: &[u8]) -> Hmac<T> where T: BitcoinHash {
let mut engine = HmacEngine::<T>::new(key);
engine.input(input);
let hmac = Hmac::<T>::from_engine(engine);
hmac
}
#[wasm_bindgen(js_name = sha512Hmac)]
pub fn sha_512_hmac(input: &[u8], key: &[u8]) -> Self {
let hmac = Hash::hmac::<sha512::Hash>(input, key);
Self(hmac.as_inner().to_vec())
}
#[wasm_bindgen(js_name = sha256Hmac)]
pub fn sha_256_hmac(input: &[u8], key: &[u8]) -> Self {
let hmac = Hash::hmac::<sha256::Hash>(input, key);
Self(hmac.as_inner().to_vec())
}
#[wasm_bindgen(js_name = sha256dHmac)]
pub fn sha_256d_hmac(input: &[u8], key: &[u8]) -> Self {
let hmac = Hash::hmac::<sha256d::Hash>(input, key);
Self(hmac.as_inner().to_vec())
}
#[wasm_bindgen(js_name = sha1Hmac)]
pub fn sha_1_hmac(input: &[u8], key: &[u8]) -> Self {
let hmac = Hash::hmac::<sha1::Hash>(input, key);
Self(hmac.as_inner().to_vec())
}
#[wasm_bindgen(js_name = ripemd160Hmac)]
pub fn ripemd_160_hmac(input: &[u8], key: &[u8]) -> Self {
let hmac = Hash::hmac::<ripemd160::Hash>(input, key);
Self(hmac.as_inner().to_vec())
}
#[wasm_bindgen(js_name = hash160Hmac)]
pub fn hash_160_hmac(input: &[u8], key: &[u8]) -> Self {
let hmac = Hash::hmac::<hash160::Hash>(input, key);
Self(hmac.as_inner().to_vec())
}
}