Skip to content

Commit c6d572a

Browse files
committed
rotate_left, Inline Helper Functions, Start on Padding ✔
1 parent 1a1eb9a commit c6d572a

1 file changed

Lines changed: 40 additions & 10 deletions

File tree

src/hashing/sha256.rs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/*
2-
Specification Sheet
3-
===================
4-
|> https://csrc.nist.gov/csrc/media/publications/fips/180/2/archive/2002-08-01/documents/fips180-2.pdf
2+
Resources
3+
=========
4+
-- Resources for learning and implementing the algorithm.
5+
|> Specification Sheet: https://csrc.nist.gov/csrc/media/publications/fips/180/2/archive/2002-08-01/documents/fips180-2.pdf
6+
|> Wikipedia: https://en.wikipedia.org/wiki/SHA-2
57
*/
68

79

@@ -24,37 +26,65 @@ const K: [u32; 64] = [
2426

2527
// TODO: The actual function
2628
pub fn sha256(message: String) -> String {
29+
let mut message_vec = message.chars().collect::<Vec<char>>();
30+
31+
/*
32+
Padding
33+
-- Pads the input message for it to be evenly split into 512-Bit Chunks.
34+
|> TODO: This currently presents an assertion error, fix incorrect padding process.
35+
*/
36+
let message_length = message_vec.len() * 8;
37+
message_vec.push(0x80 as char);
38+
while (message_vec.len() * 8 + 64) % 512 != 0 { message_vec.push(0x00 as char); }
39+
for b in (message_length as u64).to_be_bytes() { message_vec.push(b as char); }
40+
assert_eq!((message.len() * 8) % 512, 0, "Message was not properly padded");
41+
2742
return String::new();
2843
}
2944

3045

31-
/* Helper Functions */
32-
fn rotr(x: u32, n: u32) -> u32 {
33-
return (x >> n) | (x << u32::BITS - n)
46+
/*
47+
Helper Functions
48+
-- Simple functions for ease of use, operations defined in specification sheet.
49+
*/
50+
#[inline]
51+
fn rotate_right(x: u32, n: u32) -> u32 {
52+
return (x >> n) | (x << u32::BITS - n);
53+
}
54+
55+
#[inline]
56+
fn rotate_left(x: u32, n: u32) -> u32 {
57+
return (x << n) | (x >> u32::BITS - n);
3458
}
3559

60+
#[inline]
3661
fn ch(x: u32, y: u32, z: u32) -> u32 {
3762
return (x & y) ^ (x & z);
3863
}
3964

65+
#[inline]
4066
fn maj(x: u32, y: u32, z: u32) -> u32 {
4167
return (x & y) ^ (x & z) ^ (y & z);
4268
}
4369

70+
#[inline]
4471
fn sigma_0(x: u32) -> u32 {
45-
return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22);
72+
return rotate_right(x, 2) ^ rotate_right(x, 13) ^ rotate_right(x, 22);
4673
}
4774

75+
#[inline]
4876
fn sigma_1(x: u32) -> u32 {
49-
return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25);
77+
return rotate_right(x, 6) ^ rotate_right(x, 11) ^ rotate_right(x, 25);
5078
}
5179

80+
#[inline]
5281
fn lc_sigma_0(x: u32) -> u32 {
53-
return rotr(x, 7) ^ rotr(x, 18) ^ (x >> 3);
82+
return rotate_right(x, 7) ^ rotate_right(x, 18) ^ (x >> 3);
5483
}
5584

85+
#[inline]
5686
fn lc_sigma_1(x: u32) -> u32 {
57-
return rotr(x, 17) ^ rotr(x, 19) ^ (x >> 10);
87+
return rotate_right(x, 17) ^ rotate_right(x, 19) ^ (x >> 10);
5888
}
5989

6090

0 commit comments

Comments
 (0)