Skip to content

Commit

Permalink
impl clippy suggestions, example fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mimanshu-maheshwari committed Apr 5, 2024
1 parent 99d7912 commit d028ace
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 183 deletions.
40 changes: 8 additions & 32 deletions examples/file-hasher.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,21 @@
use ahsah::{
hashes::AhsahHasher,
hashes::AhsahBufferedHasher,
sha256::Sha256,
sha512::Sha512,
utils::{Args, HasherKind},
};
use clap::Parser;
use std::{
fs::File,
io::{BufReader, Read},
};
use std::fs::File ;

fn main() {
let args = Args::parse();
if let Some(path) = &args.path {
let file = File::open(path).expect("Unable to open file");
let mut buf_reader = BufReader::new(file);
let mut buffer = [0; 1024];
match &args.kind {
HasherKind::Sha512 => {
let mut hasher = Sha512::new();
while let Ok(n) = buf_reader.read(&mut buffer) {
if n == 0 {
break;
}
hasher.digest(&buffer[..n]);
}
println!("Hashing {} bytes", hasher.len());
println!("{}", hasher.finish());
}
HasherKind::Sha256 => {
let mut hasher = Sha256::new();
while let Ok(n) = buf_reader.read(&mut buffer) {
if n == 0 {
break;
}
hasher.digest(&buffer[..n]);
}
println!("Hashing {} bytes", hasher.len());
println!("{}", hasher.finish());
}
}
let mut handle = Box::new(File::open(path).expect("Unable to open file"));
let mut hasher: Box<dyn AhsahBufferedHasher> = match &args.kind {
HasherKind::Sha512 => Box::new(Sha512::new()),
HasherKind::Sha256 => Box::new(Sha256::new())
};
println!("{}", hasher.hash_bufferd(&mut handle));
} else {
panic!("File path not provided");
}
Expand Down
2 changes: 1 addition & 1 deletion examples/stdin-hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ fn main() {
let line = line.expect("Unable to read line from stdin");
hasher.digest(&line.as_bytes());
}
println!("Hashing {} bytes.", hasher.len());
println!("Hashing {} bytes.", hasher.consumed_len());
println!("{}", hasher.finish());
}
2 changes: 1 addition & 1 deletion examples/string-hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ fn main() {
};
let mut hasher = Sha256::new();
hasher.digest(&message.as_bytes());
println!("Hashing {} bytes.", hasher.len());
println!("Hashing {} bytes.", hasher.consumed_len());
println!("{}", hasher.finish());
}
22 changes: 13 additions & 9 deletions src/hashes.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
use std::io::Read;

struct Bufferd;
struct NotBufferd;
// struct Bufferd;
// struct NotBufferd;

pub trait AhsahHasher {
fn digest(self: &mut Self, data: &[u8]);
fn finish(self: &mut Self) -> String;
fn new() -> Self where Self: Sized;
fn len(self: &Self) -> usize;
fn digest(&mut self, data: &[u8]);
fn finish(&mut self) -> String;
fn new() -> Self
where
Self: Sized;
fn consumed_len(&self) -> usize;
}

pub trait AhsahBufferedHasher {
fn new() -> Self where Self: Sized;
fn new() -> Self
where
Self: Sized;
fn hash_bufferd(&mut self, handle: &mut dyn Read) -> String;
fn len(self: &Self) -> usize;

fn consumed_len(&self) -> usize;
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[allow(unused)]
pub mod hashes;
pub mod sha256;
pub mod sha512;
Expand Down
3 changes: 0 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use std::{
};

fn main() {

let args = Args::parse();

let mut handle: Box<dyn Read> = match args.path {
Expand All @@ -29,6 +28,4 @@ fn main() {
};

println!("{}", hasher.hash_bufferd(&mut handle));
// println!("{}", hasher.finish());

}
60 changes: 27 additions & 33 deletions src/sha256.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::hashes::{AhsahHasher, AhsahBufferedHasher};
use super::utils::{ch, maj, sigma_0, sigma_1, sum_0, sum_1, k_value};
use super::hashes::{AhsahBufferedHasher, AhsahHasher};
use super::utils::{ch, k_value, maj, sigma_0, sigma_1, sum_0, sum_1};
use std::io::prelude::Read;

/// Message buffer size in bits
Expand Down Expand Up @@ -40,8 +40,6 @@ pub struct Sha256 {
}

impl Sha256 {


fn compression(
w: &[u32; MESSAGE_SCHEDULE_SIZE],
(a, b, c, d, e, f, g, h): (
Expand All @@ -53,8 +51,8 @@ impl Sha256 {
&mut u32,
&mut u32,
&mut u32,
),
) {
),
) {
for i in 0..MESSAGE_SCHEDULE_SIZE {
let sum_1 = sum_1(*e, (6, 11, 25));
let ch = ch(*e, *f, *g);
Expand Down Expand Up @@ -108,30 +106,30 @@ impl Sha256 {
val = (u8_block.len() - start)
);
for i in 0..BUFFER_SIZE_U32 {
u32_block[i] = (u8_block[start + (i * 4) + 0] as u32) << 24
u32_block[i] = (u8_block[start + (i * 4)] as u32) << 24
| (u8_block[start + (i * 4) + 1] as u32) << 16
| (u8_block[start + (i * 4) + 2] as u32) << 8
| (u8_block[start + (i * 4) + 3]) as u32;
}
}

fn copy_len_to_buf(temp_block_buf: &mut Vec<u8>, len: usize) {
temp_block_buf.push((len >> 56) as u8 & 0xff);
temp_block_buf.push((len >> 48) as u8 & 0xff);
temp_block_buf.push((len >> 40) as u8 & 0xff);
temp_block_buf.push((len >> 32) as u8 & 0xff);
temp_block_buf.push((len >> 24) as u8 & 0xff);
temp_block_buf.push((len >> 16) as u8 & 0xff);
temp_block_buf.push((len >> 8) as u8 & 0xff);
temp_block_buf.push((len >> 0) as u8 & 0xff);
temp_block_buf.push((len >> 56) as u8 );
temp_block_buf.push((len >> 48) as u8 );
temp_block_buf.push((len >> 40) as u8 );
temp_block_buf.push((len >> 32) as u8 );
temp_block_buf.push((len >> 24) as u8 );
temp_block_buf.push((len >> 16) as u8 );
temp_block_buf.push((len >> 8) as u8 );
temp_block_buf.push((len) as u8 );
}

fn hash_algo(&mut self) {
// initialize registers
// message schedule array
let mut w = [0; MESSAGE_SCHEDULE_SIZE];

w[0..16].copy_from_slice(&mut self.chunk[..]);
w[0..16].copy_from_slice(&self.chunk[..]);
for i in 16..MESSAGE_SCHEDULE_SIZE {
let sigma_0 = sigma_0(w[i - 15], (7, 18, 3));
let sigma_1 = sigma_1(w[i - 2], (17, 19, 10));
Expand All @@ -145,8 +143,8 @@ impl Sha256 {
&w,
(
&mut a, &mut b, &mut c, &mut d, &mut e, &mut f, &mut g, &mut h,
),
);
),
);
self.hashes[0] = a.wrapping_add(self.hashes[0]);
self.hashes[1] = b.wrapping_add(self.hashes[1]);
self.hashes[2] = c.wrapping_add(self.hashes[2]);
Expand All @@ -157,7 +155,7 @@ impl Sha256 {
self.hashes[7] = h.wrapping_add(self.hashes[7]);
}

fn to_string(&self) -> String {
fn get_hash_string(&self) -> String {
format!(
"{:08x}{:08x}{:08x}{:08x}{:08x}{:08x}{:08x}{:08x}",
self.hashes[0],
Expand All @@ -173,21 +171,18 @@ impl Sha256 {
}

impl AhsahBufferedHasher for Sha256 {

fn new() -> Self {
Self {
data: Vec::new(),
hashes: H.clone(),
hashes: H,
bytes_len: 0,
chunk: [0; BUFFER_SIZE_U32],
}
}

fn len(&self) -> usize {
fn consumed_len(&self) -> usize {
self.bytes_len
}


}
fn hash_bufferd(&mut self, handle: &mut dyn Read) -> String {
let mut buffer = [0; BUFFER_SIZE_U8];
while let Ok(n) = handle.read(&mut buffer) {
Expand All @@ -202,35 +197,34 @@ impl AhsahBufferedHasher for Sha256 {
for d in &buffer[..n] {
data.push(*d);
}
Self::add_padding(&mut data, Some(self.bytes_len));
for i in (0..data.len()).step_by(BUFFER_SIZE_U8) {
Self::copy_buf_u8_to_u32(&data, &mut self.chunk, i);
Self::add_padding(&mut data, Some(self.bytes_len));
self.hash_algo();
}
}
}
self.to_string()
self.get_hash_string()
}
}

impl AhsahHasher for Sha256 {

fn new() -> Self {
Self {
data: Vec::new(),
hashes: H.clone(),
hashes: H,
bytes_len: 0,
chunk: [0; BUFFER_SIZE_U32],
}
}

fn len(&self) -> usize {
fn consumed_len(&self) -> usize {
self.data.len()
}

fn digest(&mut self, data: &[u8]) {
for byte in data {
self.data.push(byte.clone());
self.data.push(*byte);
}
}

Expand All @@ -241,7 +235,7 @@ impl AhsahHasher for Sha256 {
for i in (0..self.data.len()).step_by(BUFFER_SIZE_U8) {
Self::copy_buf_u8_to_u32(&self.data, &mut self.chunk, i);
self.hash_algo();
}
self.to_string()
}
self.get_hash_string()
}
}
Loading

0 comments on commit d028ace

Please sign in to comment.