Skip to content

Commit 883958d

Browse files
committed
SHA256 Type Aliases & Constants ✔
1 parent 028655f commit 883958d

1 file changed

Lines changed: 26 additions & 17 deletions

File tree

src/hashing/sha256.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,21 @@ use std::string::String;
1212
use std::vec::Vec;
1313

1414

15-
const H: [u32; 8] = [
15+
/// Words represented as a `u32` for SHA256, unsigned integer size coresponds to the individual algorithm.
16+
pub type Word = u32;
17+
18+
/// Chunk of bytes, normally asserted to be of length `CHUNK_SIZE`.
19+
pub type Chunk = Vec<u8>;
20+
21+
/// Size of Chunks in bytes.
22+
const CHUNK_SIZE: usize = 64;
23+
24+
const H: [Word; 8] = [
1625
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
1726
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
1827
];
1928

20-
const K: [u32; 64] = [
29+
const K: [Word; 64] = [
2130
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
2231
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
2332
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
@@ -50,20 +59,20 @@ pub fn sha256(message: String) -> String {
5059
=======
5160
-- Parse the padded message into 512-Bit Chunks.
5261
*/
53-
let mut chunks: Vec<Vec<u8>> = Vec::new();
62+
let mut chunks: Vec<Chunk> = Vec::new();
5463
while message_vec.len() != 0 {
55-
let mut new_chunk = Vec::with_capacity(64);
64+
let mut new_chunk = Chunk::with_capacity(CHUNK_SIZE);
5665

57-
for _ in 0 .. 64 {
66+
for _ in 0 .. CHUNK_SIZE {
5867
new_chunk.push(message_vec.pop().unwrap());
5968
}
6069

70+
assert_eq!(new_chunk.len(), CHUNK_SIZE, "Could not parse message into chunks properly.");
71+
6172
chunks.push(new_chunk);
6273
}
6374

6475
assert_ne!(chunks.len(), 0, "Could not split message into any chunks.");
65-
for c in chunks { assert_eq!(c.len(), 64, "Could not parse message into chunks properly."); }
66-
6776

6877
return String::new();
6978
}
@@ -75,42 +84,42 @@ pub fn sha256(message: String) -> String {
7584
-- Simple functions for ease of use, operations defined in specification sheet.
7685
*/
7786
#[inline(always)]
78-
fn rotate_right(x: u32, n: u32) -> u32 {
79-
return (x >> n) | (x << u32::BITS - n);
87+
fn rotate_right(x: Word, n: Word) -> Word {
88+
return (x >> n) | (x << Word::BITS - n);
8089
}
8190

8291
#[inline(always)]
83-
fn rotate_left(x: u32, n: u32) -> u32 {
84-
return (x << n) | (x >> u32::BITS - n);
92+
fn rotate_left(x: Word, n: Word) -> Word {
93+
return (x << n) | (x >> Word::BITS - n);
8594
}
8695

8796
#[inline(always)]
88-
fn ch(x: u32, y: u32, z: u32) -> u32 {
97+
fn ch(x: Word, y: Word, z: Word) -> Word {
8998
return (x & y) ^ (x & z);
9099
}
91100

92101
#[inline(always)]
93-
fn maj(x: u32, y: u32, z: u32) -> u32 {
102+
fn maj(x: Word, y: Word, z: Word) -> Word {
94103
return (x & y) ^ (x & z) ^ (y & z);
95104
}
96105

97106
#[inline(always)]
98-
fn sigma_0(x: u32) -> u32 {
107+
fn sigma_0(x: Word) -> Word {
99108
return rotate_right(x, 2) ^ rotate_right(x, 13) ^ rotate_right(x, 22);
100109
}
101110

102111
#[inline(always)]
103-
fn sigma_1(x: u32) -> u32 {
112+
fn sigma_1(x: Word) -> Word {
104113
return rotate_right(x, 6) ^ rotate_right(x, 11) ^ rotate_right(x, 25);
105114
}
106115

107116
#[inline(always)]
108-
fn lc_sigma_0(x: u32) -> u32 {
117+
fn lc_sigma_0(x: Word) -> Word {
109118
return rotate_right(x, 7) ^ rotate_right(x, 18) ^ (x >> 3);
110119
}
111120

112121
#[inline(always)]
113-
fn lc_sigma_1(x: u32) -> u32 {
122+
fn lc_sigma_1(x: Word) -> Word {
114123
return rotate_right(x, 17) ^ rotate_right(x, 19) ^ (x >> 10);
115124
}
116125

0 commit comments

Comments
 (0)