Skip to content

Commit 495a09f

Browse files
committed
Use sha-1 and sha3 instead of sha1 and tiny-keccak
Signed-off-by: koushiro <koushiro.cqx@gmail.com>
1 parent 3ec1d65 commit 495a09f

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ keywords = ["multihash", "ipfs"]
1313
[dependencies]
1414
blake2b_simd = { version = "0.5.9", default-features = false }
1515
blake2s_simd = { version = "0.5.9", default-features = false }
16-
sha1 = "0.5"
17-
sha2 = { version = "0.7", default-features = false }
18-
tiny-keccak = "1.4"
16+
digest = { version = "0.8", default-features = false }
17+
sha-1 = { version = "0.8", default-features = false }
18+
sha2 = { version = "0.8", default-features = false }
19+
sha3 = { version = "0.8", default-features = false }
1920
unsigned-varint = "0.3"
2021

2122
[dev-dependencies]

src/lib.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,42 @@
66
//! A `MultihashRef` is the same as a `Multihash`, except that it doesn't own its data.
77
//!
88
9+
#![deny(missing_docs)]
10+
911
mod errors;
1012
mod hashes;
1113
mod storage;
1214

15+
use std::cmp;
1316
use std::convert::TryFrom;
14-
use std::fmt::Debug;
17+
use std::fmt::{self, Debug};
1518
use std::hash;
1619

1720
use blake2b_simd::{blake2b, Params as Blake2bVariable};
1821
use blake2s_simd::{blake2s, Params as Blake2sVariable};
19-
use sha2::Digest;
20-
use tiny_keccak::Keccak;
22+
use digest::Digest;
2123
use unsigned_varint::{decode, encode};
2224

23-
pub use errors::{DecodeError, DecodeOwnedError, EncodeError};
24-
pub use hashes::Hash;
25-
use std::{cmp, fmt};
26-
use storage::Storage;
25+
pub use self::errors::{DecodeError, DecodeOwnedError, EncodeError};
26+
pub use self::hashes::Hash;
27+
use self::storage::Storage;
2728

28-
// Helper macro for encoding input into output using sha1, sha2, tiny_keccak, or blake2
29+
// Helper macro for encoding input into output using sha1, sha2, sha3, or blake2
2930
macro_rules! encode {
30-
(sha1, Sha1, $input:expr, $output:expr) => {{
31-
let mut hasher = sha1::Sha1::new();
32-
hasher.update($input);
33-
$output.copy_from_slice(&hasher.digest().bytes());
31+
(sha1, $algorithm:ident, $input:expr, $output:expr) => {{
32+
let mut hasher = sha1::$algorithm::default();
33+
hasher.input($input);
34+
$output.copy_from_slice(&hasher.result().as_ref());
3435
}};
3536
(sha2, $algorithm:ident, $input:expr, $output:expr) => {{
3637
let mut hasher = sha2::$algorithm::default();
3738
hasher.input($input);
3839
$output.copy_from_slice(hasher.result().as_ref());
3940
}};
40-
(tiny, $constructor:ident, $input:expr, $output:expr) => {{
41-
let mut kec = Keccak::$constructor();
42-
kec.update($input);
43-
kec.finalize($output);
41+
(sha3, $algorithm:ident, $input:expr, $output:expr) => {{
42+
let mut hasher = sha3::$algorithm::default();
43+
hasher.input($input);
44+
$output.copy_from_slice(hasher.result().as_ref());
4445
}};
4546
(blake2, $algorithm:ident, $input:expr, $output:expr) => {{
4647
let hash = $algorithm($input);
@@ -117,14 +118,14 @@ pub fn encode(hash: Hash, input: &[u8]) -> Result<Multihash, EncodeError> {
117118
SHA1 => sha1::Sha1,
118119
SHA2256 => sha2::Sha256,
119120
SHA2512 => sha2::Sha512,
120-
SHA3224 => tiny::new_sha3_224,
121-
SHA3256 => tiny::new_sha3_256,
122-
SHA3384 => tiny::new_sha3_384,
123-
SHA3512 => tiny::new_sha3_512,
124-
Keccak224 => tiny::new_keccak224,
125-
Keccak256 => tiny::new_keccak256,
126-
Keccak384 => tiny::new_keccak384,
127-
Keccak512 => tiny::new_keccak512,
121+
SHA3224 => sha3::Sha3_224,
122+
SHA3256 => sha3::Sha3_256,
123+
SHA3384 => sha3::Sha3_384,
124+
SHA3512 => sha3::Sha3_512,
125+
Keccak224 => sha3::Keccak224,
126+
Keccak256 => sha3::Keccak256,
127+
Keccak384 => sha3::Keccak384,
128+
Keccak512 => sha3::Keccak512,
128129
Blake2b512 => blake2::blake2b,
129130
Blake2b256 => blake2_256::Blake2bVariable,
130131
Blake2s256 => blake2::blake2s,

0 commit comments

Comments
 (0)