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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use crate::*;
use base58::{FromBase58, ToBase58};
use generic_array::typenum::{marker_traits::Unsigned, U32};
use generic_array::GenericArray;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::str::FromStr;
#[derive(Copy, Clone, PartialOrd, PartialEq, Ord, Eq)]
pub struct HashValue(GenericArray<u8, U32>);
pub const HASH_VALUE_LEN: usize = 32;
impl std::fmt::Debug for HashValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "HashValue: {}", hex::encode(self.0.as_slice()))
}
}
impl From<GenericArray<u8, U32>> for HashValue {
fn from(hash: GenericArray<u8, U32>) -> Self {
Self(hash)
}
}
impl From<HashValue> for GenericArray<u8, U32> {
fn from(hash: HashValue) -> Self {
hash.0
}
}
impl AsRef<GenericArray<u8, U32>> for HashValue {
fn as_ref(&self) -> &GenericArray<u8, U32> {
&self.0
}
}
impl Default for HashValue {
fn default() -> Self {
Self(GenericArray::default())
}
}
impl RawFixedBytes for HashValue {
fn raw_bytes() -> Option<usize> {
Some(U32::to_usize())
}
}
impl RawEncode for HashValue {
fn raw_measure(&self, _purpose: &Option<RawEncodePurpose>) -> BuckyResult<usize> {
Ok(U32::to_usize())
}
fn raw_encode<'a>(
&self,
buf: &'a mut [u8],
_purpose: &Option<RawEncodePurpose>,
) -> BuckyResult<&'a mut [u8]> {
let bytes = Self::raw_bytes().unwrap();
if buf.len() < bytes {
let msg = format!(
"not enough buffer for encode HashValue, except={}, got={}",
bytes,
buf.len()
);
error!("{}", msg);
return Err(BuckyError::new(BuckyErrorCode::OutOfLimit, msg));
}
unsafe {
std::ptr::copy(self.0.as_slice().as_ptr(), buf.as_mut_ptr(), bytes);
}
Ok(&mut buf[bytes..])
}
}
impl<'de> RawDecode<'de> for HashValue {
fn raw_decode(buf: &'de [u8]) -> BuckyResult<(Self, &'de [u8])> {
let bytes = Self::raw_bytes().unwrap();
if buf.len() < bytes {
let msg = format!(
"not enough buffer for decode HashValue, except={}, got={}",
bytes,
buf.len()
);
error!("{}", msg);
return Err(BuckyError::new(BuckyErrorCode::OutOfLimit, msg));
}
let mut hash = GenericArray::default();
unsafe {
std::ptr::copy(buf.as_ptr(), hash.as_mut_slice().as_mut_ptr(), bytes);
}
Ok((Self(hash), &buf[bytes..]))
}
}
impl From<&[u8; 32]> for HashValue {
fn from(hash: &[u8; 32]) -> Self {
Self(GenericArray::clone_from_slice(hash))
}
}
impl TryFrom<&[u8]> for HashValue {
type Error = BuckyError;
fn try_from(v: &[u8]) -> Result<Self, Self::Error> {
if v.len() != 32 {
let msg = format!(
"HashValue expected bytes of length {} but it was {}",
32,
v.len()
);
error!("{}", msg);
return Err(BuckyError::new(BuckyErrorCode::InvalidData, msg));
}
let ar: [u8; 32] = v.try_into().unwrap();
Ok(Self(GenericArray::from(ar)))
}
}
impl TryFrom<Vec<u8>> for HashValue {
type Error = BuckyError;
fn try_from(v: Vec<u8>) -> Result<Self, Self::Error> {
if v.len() != 32 {
let msg = format!(
"HashValue expected bytes of length {} but it was {}",
32,
v.len()
);
error!("{}", msg);
return Err(BuckyError::new(BuckyErrorCode::InvalidData, msg));
}
let ar: [u8; 32] = v.try_into().unwrap();
Ok(Self(GenericArray::from(ar)))
}
}
impl HashValue {
pub fn as_slice(&self) -> &[u8] {
self.0.as_slice()
}
pub fn as_mut_slice(&mut self) -> &mut [u8] {
self.0.as_mut_slice()
}
pub fn len() -> usize {
HASH_VALUE_LEN
}
pub fn to_hex_string(&self) -> String {
hex::encode(self.0.as_slice())
}
pub fn from_hex_string(s: &str) -> BuckyResult<Self> {
let ret = hex::decode(s).map_err(|e| {
let msg = format!("invalid hash value hex string: {}, {}", s, e);
error!("{}", msg);
BuckyError::new(BuckyErrorCode::InvalidFormat, msg)
})?;
Self::clone_from_slice(&ret)
}
pub fn to_base58(&self) -> String {
self.0.to_base58()
}
pub fn from_base58(s: &str) -> BuckyResult<Self> {
let buf = s.from_base58().map_err(|e| {
let msg = format!("convert base58 str to hashvalue failed, str={}, {:?}", s, e);
error!("{}", msg);
BuckyError::new(BuckyErrorCode::InvalidFormat, msg)
})?;
if buf.len() != 32 {
let msg = format!(
"convert base58 str to hashvalue failed, len unmatch: str={}",
s
);
return Err(BuckyError::new(BuckyErrorCode::InvalidFormat, msg));
}
Ok(Self::try_from(buf).unwrap())
}
pub fn clone_from_slice(hash: &[u8]) -> BuckyResult<Self> {
Self::try_from(hash)
}
}
impl std::fmt::Display for HashValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_hex_string())
}
}
impl FromStr for HashValue {
type Err = BuckyError;
fn from_str(s: &str) -> BuckyResult<Self> {
if s.len() == 64 {
Self::from_hex_string(s)
} else {
Self::from_base58(s)
}
}
}
impl Hash for HashValue {
fn hash<H: Hasher>(&self, state: &mut H) {
let mut buff = [0 as u8; 32];
let _ = self.raw_encode(buff.as_mut(), &None).unwrap();
state.write(buff.as_ref());
}
}
impl ProtobufTransform<HashValue> for Vec<u8> {
fn transform(value: HashValue) -> BuckyResult<Self> {
Ok(Vec::from(value.0.as_slice()))
}
}
impl ProtobufTransform<&HashValue> for Vec<u8> {
fn transform(value: &HashValue) -> BuckyResult<Self> {
Ok(Vec::from(value.0.as_slice()))
}
}
impl ProtobufTransform<Vec<u8>> for HashValue {
fn transform(value: Vec<u8>) -> BuckyResult<Self> {
if value.len() != HASH_VALUE_LEN {
return Err(BuckyError::new(
BuckyErrorCode::InvalidParam,
format!(
"try convert from vec<u8> to named object id failed, invalid len {}",
value.len()
),
));
}
let mut id = Self::default();
unsafe {
std::ptr::copy(value.as_ptr(), id.as_mut_slice().as_mut_ptr(), value.len());
}
Ok(id)
}
}
#[cfg(test)]
mod test {
use std::str::FromStr;
use crate::*;
#[test]
fn test() {
let hash = hash_data("xxxx".as_bytes());
let hex_id = hash.to_hex_string();
let base58_id = hash.to_base58();
println!("{}, {}", hex_id, base58_id);
let ret = HashValue::from_str(&hex_id).unwrap();
let ret2 = HashValue::from_str(&base58_id).unwrap();
assert_eq!(ret, ret2);
}
}