Skip to content

Commit 25ff995

Browse files
committed
add more tests for SHA‐1 vectors
1 parent cd96b64 commit 25ff995

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed
640 Bytes
Binary file not shown.
640 Bytes
Binary file not shown.

gix-hash/tests/object_id/mod.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ mod from_hex {
4242
}
4343
}
4444

45-
mod empty {
45+
mod sha1 {
46+
use std::str::FromStr as _;
47+
4648
use gix_features::hash::hasher;
4749
use gix_hash::{Kind, ObjectId};
4850

@@ -53,12 +55,56 @@ mod empty {
5355
}
5456

5557
#[test]
56-
fn blob() {
58+
fn empty_blob() {
5759
assert_eq!(ObjectId::empty_blob(Kind::Sha1), hash_contents(b"blob 0\0"));
5860
}
5961

6062
#[test]
61-
fn tree() {
63+
fn empty_tree() {
6264
assert_eq!(ObjectId::empty_tree(Kind::Sha1), hash_contents(b"tree 0\0"));
6365
}
66+
67+
/// Check the test vectors from RFC 3174.
68+
#[test]
69+
fn rfc_3174() {
70+
let fixtures: &[(&[u8], &str)] = &[
71+
(b"abc", "A9 99 3E 36 47 06 81 6A BA 3E 25 71 78 50 C2 6C 9C D0 D8 9D"),
72+
(
73+
b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
74+
"84 98 3E 44 1C 3B D2 6E BA AE 4A A1 F9 51 29 E5 E5 46 70 F1",
75+
),
76+
(
77+
&b"a".repeat(1000000),
78+
"34 AA 97 3C D4 C4 DA A4 F6 1E EB 2B DB AD 27 31 65 34 01 6F",
79+
),
80+
(
81+
&b"0123456701234567012345670123456701234567012345670123456701234567".repeat(10),
82+
"DE A3 56 A2 CD DD 90 C7 A7 EC ED C5 EB B5 63 93 4F 46 04 52",
83+
),
84+
];
85+
for (input, output) in fixtures {
86+
assert_eq!(
87+
hash_contents(input),
88+
ObjectId::from_str(&output.to_lowercase().replace(' ', "")).expect("RFC digests to be valid"),
89+
);
90+
}
91+
}
92+
93+
/// Check the “SHA‐1 is a Shambles” chosen‐prefix collision.
94+
///
95+
/// See <https://sha-mbles.github.io/>.
96+
///
97+
/// We test these and not the earlier SHAttered PDFs because they are much smaller.
98+
#[test]
99+
fn shambles() {
100+
let message_a = include_bytes!("../fixtures/shambles/messageA");
101+
let message_b = include_bytes!("../fixtures/shambles/messageB");
102+
assert_ne!(message_a, message_b);
103+
104+
// BUG: These should be detected as a collision attack.
105+
let expected =
106+
ObjectId::from_str("8ac60ba76f1999a1ab70223f225aefdc78d4ddc0").expect("Shambles digest to be valid");
107+
assert_eq!(hash_contents(message_a), expected);
108+
assert_eq!(hash_contents(message_b), expected);
109+
}
64110
}

0 commit comments

Comments
 (0)