refactor Sha256 methods
This commit is contained in:
parent
f23e0bc759
commit
c36e7d669f
40
src/main.rs
40
src/main.rs
|
@ -22,7 +22,7 @@ fn main() -> Result<(), std::io::Error> {
|
||||||
let mut s256 = Sha256::new();
|
let mut s256 = Sha256::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let read = s256.sum(&mut stdin)?;
|
let read = s256.update(&mut stdin)?;
|
||||||
if read != 64 {
|
if read != 64 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ fn main() -> Result<(), std::io::Error> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute hash
|
// Compute hash
|
||||||
for c in s256.state() {
|
for c in s256.sum() {
|
||||||
for b in c.to_be_bytes() {
|
for b in c.to_be_bytes() {
|
||||||
print!("{:02x?}", b);
|
print!("{:02x?}", b);
|
||||||
}
|
}
|
||||||
|
@ -40,10 +40,10 @@ fn main() -> Result<(), std::io::Error> {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Sha256 {
|
struct Sha256 {
|
||||||
state: [u32; 8],
|
state: [u32; 8],
|
||||||
message_length: usize,
|
message_length: usize,
|
||||||
|
|
||||||
buffer: [u8; 128],
|
buffer: [u8; 128],
|
||||||
block_read: usize,
|
block_read: usize,
|
||||||
}
|
}
|
||||||
|
@ -58,31 +58,25 @@ impl Sha256 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const fn state(&self) -> [u32; 8] {
|
fn update<R: Read>(&mut self, r: &mut R) -> Result<usize, std::io::Error> {
|
||||||
self.state
|
|
||||||
}
|
|
||||||
|
|
||||||
fn sum_existing_buffer(&mut self) {
|
|
||||||
if self.block_read != 64 {
|
|
||||||
self.pad();
|
|
||||||
}
|
|
||||||
|
|
||||||
self.compress();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn sum<R: Read>(&mut self, r: &mut R) -> Result<usize, std::io::Error> {
|
|
||||||
let read = r.read(&mut self.buffer[..64])?;
|
let read = r.read(&mut self.buffer[..64])?;
|
||||||
|
|
||||||
self.block_read = read;
|
self.block_read = read;
|
||||||
self.message_length += read;
|
self.message_length += read;
|
||||||
|
|
||||||
if read != 64 {
|
self.compress();
|
||||||
|
|
||||||
|
Ok(read)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sum(&mut self) -> [u32; 8] {
|
||||||
|
if self.block_read != 64 {
|
||||||
self.pad();
|
self.pad();
|
||||||
}
|
}
|
||||||
|
|
||||||
self.compress();
|
self.compress();
|
||||||
|
|
||||||
Ok(read)
|
self.state
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pad(&mut self) {
|
fn pad(&mut self) {
|
||||||
|
@ -109,7 +103,7 @@ impl Sha256 {
|
||||||
let mut state = self.state;
|
let mut state = self.state;
|
||||||
|
|
||||||
for i in 0..64 {
|
for i in 0..64 {
|
||||||
state = round(state, ROUND_CONSTANT[i], msg[i])
|
state = round(state, ROUND_CONSTANT[i], msg[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.state[0] = self.state[0].wrapping_add(state[0]);
|
self.state[0] = self.state[0].wrapping_add(state[0]);
|
||||||
|
@ -259,9 +253,7 @@ fn sha256_test() {
|
||||||
2018687061,
|
2018687061,
|
||||||
];
|
];
|
||||||
|
|
||||||
s256.sum_existing_buffer();
|
assert_eq!(expected_compression, s256.sum());
|
||||||
|
|
||||||
assert_eq!(expected_compression, s256.state());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -311,7 +303,5 @@ fn sha256_test2() {
|
||||||
3050570844, 1061580713, 534957162, 3195609898, 441961969, 83337838, 1903036958, 663347064,
|
3050570844, 1061580713, 534957162, 3195609898, 441961969, 83337838, 1903036958, 663347064,
|
||||||
];
|
];
|
||||||
|
|
||||||
s256.sum_existing_buffer();
|
assert_eq!(expected_compression, s256.sum());
|
||||||
|
|
||||||
assert_eq!(expected_compression, s256.state());
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user