Skip to content

Commit 0edbcea

Browse files
authored
Add twox hash (#20)
1 parent 8465eab commit 0edbcea

File tree

6 files changed

+63
-9
lines changed

6 files changed

+63
-9
lines changed

Cargo.lock

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dtool"
3-
version = "0.9.0"
3+
version = "0.10.0"
44
authors = ["GB <[email protected]>"]
55
description = "A command-line tool collection to assist development"
66
categories = ["command-line-utilities"]
@@ -41,3 +41,5 @@ signatory-secp256k1 = "0.17.0"
4141
secp256k1 = "0.15"
4242
untrusted = "0.7.0"
4343
schnorrkel = "0.8.5"
44+
twox-hash = "1.5.0"
45+
byteorder = "1.3.1"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Now `dtool` supports:
2323
- [Hex / base64 conversion](./docs/Usage.md#hex--base64-conversion)
2424
- [URL encode / decode](./docs/Usage.md#url-encode--decode)
2525
- [Number codec](./docs/Usage.md#number-codec)
26-
- [Hash (MD5, SHA-1, SHA-2, SHA-3, RIPEMD, CRC, Blake2b, SM3)](./docs/Usage.md#hash-md5-sha-1-sha-2-sha-3-ripemd-crc-blake2b-sm3)
26+
- [Hash (MD5, SHA-1, SHA-2, SHA-3, RIPEMD, CRC, Blake2b, SM3, Twox)](./docs/Usage.md#hash-md5-sha-1-sha-2-sha-3-ripemd-crc-blake2b-sm3-twox)
2727
- [UTF-8 string / unicode conversion](./docs/Usage.md#utf-8-string--unicode-conversion)
2828
- [HTML entity encode / decode](./docs/Usage.md#html-entity-encode--decode)
2929
- [Regex match](./docs/Usage.md#regex-match)

docs/Usage.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- [Hex / base64 conversion](#hex--base64-conversion)
99
- [URL encode / decode](#url-encode--decode)
1010
- [Number codec](#number-codec)
11-
- [Hash (MD5, SHA-1, SHA-2, SHA-3, RIPEMD, CRC, Blake2b, SM3)](#hash-md5-sha-1-sha-2-sha-3-ripemd-crc-blake2b-sm3)
11+
- [Hash (MD5, SHA-1, SHA-2, SHA-3, RIPEMD, CRC, Blake2b, SM3, Twox)](#hash-md5-sha-1-sha-2-sha-3-ripemd-crc-blake2b-sm3-twox)
1212
- [UTF-8 string / unicode conversion](#utf-8-string--unicode-conversion)
1313
- [HTML entity encode / decode](#html-entity-encode--decode)
1414
- [Regex match](#regex-match)
@@ -100,7 +100,7 @@
100100
| nd |Number decode<br>Compact<br>v0.1.0| $ dtool nd -tc 0xed03<br>251 |
101101

102102

103-
## Hash (MD5, SHA-1, SHA-2, SHA-3, RIPEMD, CRC, Blake2b, SM3)
103+
## Hash (MD5, SHA-1, SHA-2, SHA-3, RIPEMD, CRC, Blake2b, SM3, Twox)
104104

105105
|Sub command| Desc | Example |
106106
|-----------|-----------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
@@ -127,6 +127,7 @@
127127
| hash | Hex to hash<br>Blake2b 384<br>v0.5.0 | $ dtool hash -a blake2b_384 0x616263<br>0x6f56a82c8e7ef526dfe182eb5212f7db9df1317e57815dbda46083fc30\\<br>f54ee6c66ba83be64b302d7cba6ce15bb556f4 |
128128
| hash | Hex to hash<br>Blake2b 512<br>v0.5.0 |$ dtool hash -a blake2b_512 0x616263<br>0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdb\\<br>ffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386\\<br>edd4009923|
129129
| hash | Hex to hash<br>SM3<br>v0.7.0 | $ dtool hash -a sm3 0x616263<br>0x66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f\\<br>4ba8e0 |
130+
| hash | Hex to hash<br>TwoX<br>v0.10.0 | $ dtool hash -a twox -s 1 0x616263<br>0x0889329981caa9be |
130131

131132

132133
## UTF-8 string / unicode conversion

src/modules/ecdsa/sm2.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ fn vec_to_signature(vec: Vec<u8>) -> Result<Signature, String> {
180180
fn slice_to_u64x4(slice: &[u8]) -> U64x4 {
181181
U64x4 {
182182
value: [
183-
u64::from_be_bytes({ slice_to_arr(&slice[24..32]) }),
183+
u64::from_be_bytes(slice_to_arr(&slice[24..32])),
184184
u64::from_be_bytes(slice_to_arr(&slice[16..24])),
185185
u64::from_be_bytes(slice_to_arr(&slice[8..16])),
186186
u64::from_be_bytes(slice_to_arr(&slice[0..8])),
@@ -223,8 +223,8 @@ pub fn cases() -> LinkedHashMap<&'static str, Vec<Case>> {
223223
Case {
224224
desc: "SM2".to_string(),
225225
input: vec!["-c", "sm2", "-p", "0x044b2dd8bf6dbbfb14db3e4d17bd7a3e8758eb4232049bec931d1038f4afaae46ac3c771f929bbf35a28b0363789fb19127cea3318f4c8902a0034ca5f1b7667d1", "-S",
226-
"0x0a4d089d3177234ed34aa7f30c6a7a7954539f68825bedbe82be65aefdb733c921207be31b8071bbfd5c99044ebde49d3c38e9972063b844f65f4acfc7d6dff2",
227-
"0x616263"].into_iter().map(Into::into).collect(),
226+
"0x0a4d089d3177234ed34aa7f30c6a7a7954539f68825bedbe82be65aefdb733c921207be31b8071bbfd5c99044ebde49d3c38e9972063b844f65f4acfc7d6dff2",
227+
"0x616263"].into_iter().map(Into::into).collect(),
228228
output: vec!["true"].into_iter().map(Into::into).collect(),
229229
is_example: true,
230230
is_test: true,

src/modules/hash.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use yogcrypt::sm3::sm3_enc;
1111

1212
pub fn module<'a, 'b>() -> Module<'a, 'b> {
1313
Module {
14-
desc: "Hash (MD5, SHA-1, SHA-2, SHA-3, RIPEMD, CRC, Blake2b, SM3)".to_string(),
14+
desc: "Hash (MD5, SHA-1, SHA-2, SHA-3, RIPEMD, CRC, Blake2b, SM3, Twox)".to_string(),
1515
commands: commands(),
1616
get_cases: cases::cases,
1717
}
@@ -26,6 +26,7 @@ struct Algorithm {
2626
enum AlgorithmF {
2727
Normal(fn(data: Vec<u8>) -> Result<Vec<u8>, String>),
2828
WithKey(fn(data: Vec<u8>, key: Vec<u8>) -> Result<Vec<u8>, String>),
29+
WithSeed(fn(data: Vec<u8>, seed: u64) -> Result<Vec<u8>, String>),
2930
}
3031

3132
lazy_static! {
@@ -145,6 +146,11 @@ lazy_static! {
145146
help: "Chinese National Standard SM3",
146147
f: AlgorithmF::Normal(sm3),
147148
},
149+
Algorithm {
150+
name: "twox",
151+
help: "TwoX",
152+
f: AlgorithmF::WithSeed(twox),
153+
},
148154
];
149155
static ref ALGORITHMS: HashMap<&'static str, &'static Algorithm> =
150156
RAW_ALGORITHMS.iter().map(|x| (x.name, x)).collect();
@@ -176,6 +182,14 @@ pub fn commands<'a, 'b>() -> Vec<Command<'a, 'b>> {
176182
.takes_value(true)
177183
.required(false),
178184
)
185+
.arg(
186+
Arg::with_name("SEED")
187+
.long("seed")
188+
.short("s")
189+
.help("Seed for twox")
190+
.takes_value(true)
191+
.required(false),
192+
)
179193
.arg(Arg::with_name("INPUT").required(false).index(1)),
180194
f: hash,
181195
}]
@@ -197,6 +211,13 @@ fn hash(matches: &ArgMatches) -> Result<Vec<String>, String> {
197211
None => vec![],
198212
};
199213
(f)(input, key)?
214+
},
215+
AlgorithmF::WithSeed(f) => {
216+
let seed = match matches.value_of("SEED") {
217+
Some(seed) => seed.parse::<u64>().map_err(|_| "Invalid seed")?.into(),
218+
None => 0,
219+
};
220+
(f)(input, seed)?
200221
}
201222
},
202223
None => return Err("Invalid algorithm".to_string()),
@@ -366,6 +387,17 @@ fn sm3(data: Vec<u8>) -> Result<Vec<u8>, String> {
366387
Ok(result)
367388
}
368389

390+
fn twox(data: Vec<u8>, seed: u64) -> Result<Vec<u8>, String> {
391+
use ::core::hash::Hasher;
392+
let mut h = twox_hash::XxHash::with_seed(seed);
393+
h.write(&data);
394+
let r = h.finish();
395+
use byteorder::{ByteOrder, LittleEndian};
396+
let mut dest = vec![0u8; 8];
397+
LittleEndian::write_u64(&mut dest[0..8], r);
398+
Ok(dest)
399+
}
400+
369401
mod cases {
370402
use crate::modules::Case;
371403
use linked_hash_map::LinkedHashMap;
@@ -590,6 +622,14 @@ mod cases {
590622
is_test: true,
591623
since: "0.7.0".to_string(),
592624
},
625+
Case {
626+
desc: "TwoX".to_string(),
627+
input: vec!["-a", "twox", "-s", "1", "0x616263"].into_iter().map(Into::into).collect(),
628+
output: vec!["0x0889329981caa9be"].into_iter().map(Into::into).collect(),
629+
is_example: true,
630+
is_test: true,
631+
since: "0.10.0".to_string(),
632+
},
593633
]),
594634
].into_iter().collect()
595635
}

0 commit comments

Comments
 (0)