Skip to content

Commit

Permalink
Merge pull request #34 from iotaledger/feat/sha-384
Browse files Browse the repository at this point in the history
Add SHA384
  • Loading branch information
l1h3r authored Jan 7, 2021
2 parents 6a9a6c5 + aa1734e commit c251496
Show file tree
Hide file tree
Showing 9 changed files with 2,745 additions and 1,647 deletions.
5 changes: 5 additions & 0 deletions .changes/add-sha-384.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"iota-crypto": minor
---

Add SHA384 hash function.
1,655 changes: 10 additions & 1,645 deletions src/hashes/sha.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/blake2b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod test {
fn blake2b_simd_lib() {
// uses blake2b testvectors from the official testvectors at https://github.com/BLAKE2/BLAKE2/tree/master/testvectors
// out_256 was generated with b2sum on inputs without key
let test_vectors: Vec<TestVector> = serde_json::from_str(include_str!("blake2b.json")).unwrap();
let test_vectors: Vec<TestVector> = serde_json::from_str(include_str!("fixtures/blake2b.json")).unwrap();
let mut test_num = 0u64;
for vector in test_vectors.iter() {
test_num += 1;
Expand All @@ -48,7 +48,7 @@ mod test {

#[test]
fn iota_cypto_blake2b_256() {
let test_vectors: Vec<TestVector> = serde_json::from_str(include_str!("blake2b.json")).unwrap();
let test_vectors: Vec<TestVector> = serde_json::from_str(include_str!("fixtures/blake2b.json")).unwrap();
let mut test_num = 0u64;
for vector in test_vectors.iter().filter(|v| v.key.is_empty() && v.out_256.is_some()) {
test_num += 1;
Expand Down
File renamed without changes.
545 changes: 545 additions & 0 deletions tests/fixtures/sha2_256.rs

Large diffs are not rendered by default.

1,057 changes: 1,057 additions & 0 deletions tests/fixtures/sha2_384.rs

Large diffs are not rendered by default.

1,056 changes: 1,056 additions & 0 deletions tests/fixtures/sha2_512.rs

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions tests/sha.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2020 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

#![cfg(feature = "sha")]

use crypto::hashes::sha::{SHA256, SHA256_LEN, SHA384, SHA384_LEN, SHA512, SHA512_LEN};

struct TestVector {
msg: &'static str,
digest: &'static str,
}

#[test]
fn test_sha256() {
let tvs = include!("fixtures/sha2_256.rs");

for tv in tvs.iter() {
let msg = hex::decode(tv.msg).unwrap();

let mut expected_digest = [0; SHA256_LEN];
hex::decode_to_slice(tv.digest, &mut expected_digest as &mut [u8]).unwrap();

let mut digest = [0; SHA256_LEN];
SHA256(&msg, &mut digest);

assert_eq!(&digest, &expected_digest);
}
}

#[test]
fn test_sha384() {
let tvs = include!("fixtures/sha2_384.rs");

for tv in tvs.iter() {
let msg = hex::decode(tv.msg).unwrap();

let mut expected_digest = [0; SHA384_LEN];
hex::decode_to_slice(tv.digest, &mut expected_digest as &mut [u8]).unwrap();

let mut digest = [0; SHA384_LEN];
SHA384(&msg, &mut digest);

assert_eq!(&digest, &expected_digest);
}
}

#[test]
fn test_sha512() {
let tvs = include!("fixtures/sha2_512.rs");

for tv in tvs.iter() {
let msg = hex::decode(tv.msg).unwrap();

let mut expected_digest = [0; SHA512_LEN];
hex::decode_to_slice(tv.digest, &mut expected_digest as &mut [u8]).unwrap();

let mut digest = [0; SHA512_LEN];
SHA512(&msg, &mut digest);

assert_eq!(&digest, &expected_digest);
}
}
8 changes: 8 additions & 0 deletions utils/test_vectors/py/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def print_sha256_test_vector(n=None):
print(f" digest: \"{digest.hex()}\",")
print("},")

def print_sha384_test_vector(n=None):
msg = fresh_bytes(n=n, bound=1024)
digest = hashlib.sha384(msg).digest()
print("TestVector {")
print(f" msg: \"{msg.hex()}\",")
print(f" digest: \"{digest.hex()}\",")
print("},")

def print_sha512_test_vector(n=None):
msg = fresh_bytes(n=n, bound=1024)
digest = hashlib.sha512(msg).digest()
Expand Down

0 comments on commit c251496

Please sign in to comment.