refactor(algc): understand digest memory layout better
This commit is contained in:
parent
347e752e37
commit
32643ad085
5 changed files with 18 additions and 10 deletions
|
@ -1,2 +1,10 @@
|
||||||
|
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
void print_digest(const SHA2Digest digest) {
|
||||||
|
for (size_t i = 0; i < SHA2_256_HashParts; i++) {
|
||||||
|
fprintf(stderr, "%08x", digest[i]);
|
||||||
|
}
|
||||||
|
fprintf(stderr, "\n");
|
||||||
|
}
|
||||||
|
|
|
@ -3,4 +3,6 @@
|
||||||
|
|
||||||
#include "sha2.h"
|
#include "sha2.h"
|
||||||
|
|
||||||
|
extern void print_digest(const SHA2Digest);
|
||||||
|
|
||||||
#endif // HASH_H
|
#endif // HASH_H
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include "hash.h"
|
||||||
#include "sha2.h"
|
#include "sha2.h"
|
||||||
#include "trace.h"
|
#include "trace.h"
|
||||||
#include "unity.h"
|
#include "unity.h"
|
||||||
|
@ -44,11 +45,12 @@ void test_sha2_check(void) {
|
||||||
// NOTE: If it segfaults here, then the sha2_oneshot function is at fault
|
// NOTE: If it segfaults here, then the sha2_oneshot function is at fault
|
||||||
// anyways!!!!!!!!!!!!
|
// anyways!!!!!!!!!!!!
|
||||||
TRACELN("Hash should be:");
|
TRACELN("Hash should be:");
|
||||||
dump_data(test_values[i][1], SHA2_256_HashBytes);
|
print_digest(test_values[i][1]); // idk how to fix this warning
|
||||||
|
|
||||||
TRACELN("Hash is:");
|
TRACELN("Hash is:");
|
||||||
dump_data((uint8_t *)digest, SHA2_256_HashBytes);
|
print_digest(digest);
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_MEMORY(test_values[i][1], digest, SHA2_256_HashBytes);
|
TEST_ASSERT_EQUAL_HEX32_ARRAY(test_values[i][1], digest,
|
||||||
|
SHA2_256_HashParts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,11 +14,7 @@ fn main() {
|
||||||
} else {
|
} else {
|
||||||
algorithms::hash::sha2_256_oneshot(raw_in).expect("could not hash your input data")
|
algorithms::hash::sha2_256_oneshot(raw_in).expect("could not hash your input data")
|
||||||
};
|
};
|
||||||
println!("{}", format_digest(&bytes(&hash)));
|
println!("{}", format_digest(&hash));
|
||||||
}
|
|
||||||
|
|
||||||
fn bytes(data: &[u32]) -> Vec<u8> {
|
|
||||||
data.iter().flat_map(|x| x.to_ne_bytes()).collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage(call: &str, code: i32) -> ! {
|
fn usage(call: &str, code: i32) -> ! {
|
||||||
|
|
|
@ -6,10 +6,10 @@ pub use sha2::*;
|
||||||
pub const HASH_EXAMPLE_DATA: &[u8] = b"lalilolela";
|
pub const HASH_EXAMPLE_DATA: &[u8] = b"lalilolela";
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
pub fn format_digest(digest: &[u8]) -> String {
|
pub fn format_digest(digest: &[u32]) -> String {
|
||||||
digest
|
digest
|
||||||
.iter()
|
.iter()
|
||||||
.map(|b| format!("{b:02x}"))
|
.map(|b| format!("{b:08x}"))
|
||||||
.collect::<String>()
|
.collect::<String>()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue