From 65c4b220ae6909d07b90eee46f29a88f7c96f310 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Mon, 29 Dec 2025 11:26:18 -0700 Subject: [PATCH] Minor doc, format, and clippy fixes. --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/bin/sha256.rs | 19 +++++++++---------- src/common.rs | 2 +- src/hexstr.rs | 11 +++++++---- src/lib.rs | 2 +- 6 files changed, 20 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 63896af..589a317 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,4 +4,4 @@ version = 4 [[package]] name = "emsha" -version = "1.0.2" +version = "1.0.3" diff --git a/Cargo.toml b/Cargo.toml index a51ac72..d154b0e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ repository = "https://git.wntrmute.dev/wntrmute/emsha-rs" categories = ["cryptography", "no-std", "embedded"] keywords = ["sha256", "hmac", "hash", "embedded", "no_std"] license-file = "LICENSE" -version = "1.0.2" +version = "1.0.3" edition = "2024" publish = ["kellnr"] diff --git a/src/bin/sha256.rs b/src/bin/sha256.rs index 53e6f4e..739a9c7 100644 --- a/src/bin/sha256.rs +++ b/src/bin/sha256.rs @@ -1,7 +1,7 @@ //! Demonstrator of SHA256 hashing. -use emsha::{self, sha256, Hash}; use emsha::hexstr::to_hex; +use emsha::{self, Hash, sha256}; use std::fs::File; use std::io::Read; use std::io::{self, BufReader}; @@ -11,10 +11,9 @@ const CHUNK_SIZE: usize = 512; fn transmute_error(res: emsha::Result<()>) -> io::Result<()> { match res { Ok(_) => Ok(()), - Err(e) => return Err( - io::Error::new( - io::ErrorKind::Other, - format!("{:}", e), /* error */)), + Err(e) => { + Err(io::Error::other(format!("{:}", e) /* error */)) + } } } @@ -33,7 +32,7 @@ fn hash_file(path: &str) -> io::Result<()> { break; // EOF reached } - transmute_error( ctx.update(&buffer[..bytes_read]))? + transmute_error(ctx.update(&buffer[..bytes_read]))? } let mut d: [u8; sha256::SIZE] = [0; sha256::SIZE]; @@ -46,12 +45,12 @@ fn hash_file(path: &str) -> io::Result<()> { Ok(()) } -fn main() -> io::Result<()>{ +fn main() -> io::Result<()> { let args: Vec = std::env::args().collect(); - for i in 1..args.len() { - hash_file(args[i].as_str())?; + for arg in args.iter().skip(1) { + hash_file(arg.as_str())?; } Ok(()) -} \ No newline at end of file +} diff --git a/src/common.rs b/src/common.rs index 399e9dd..71b19ac 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,6 +1,6 @@ #[inline] pub(crate) fn rotr32(x: u32, n: u8) -> u32 { - (x >> n) | (x << (32 - n)) + x.rotate_right(n as u32) } #[allow(non_snake_case)] // the name is taken from the RFC diff --git a/src/hexstr.rs b/src/hexstr.rs index 02f3541..3084ff7 100644 --- a/src/hexstr.rs +++ b/src/hexstr.rs @@ -1,4 +1,5 @@ //! Various utilities for encoding to and from hex strings. + const LUT: [&str; 256] = [ "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0a", "0b", "0c", "0d", "0e", "0f", "10", "11", "12", "13", "14", "15", @@ -26,7 +27,8 @@ const LUT: [&str; 256] = [ "fd", "fe", "ff", ]; -/// Convert the binary array in `input` to a hex-encoded array in `output`. +/// Convert the binary array in `input` to a hex-encoded array in +/// `output`. pub fn to_hex( input: &[u8; N], output: &mut [u8; M], @@ -44,7 +46,8 @@ pub fn to_hex( } } -/// +/// Convert a hex string array in `input` to a binary array in +/// `output`. pub fn from_hex( input: &[u8; M], output: &mut [u8; N], @@ -55,9 +58,9 @@ pub fn from_hex( while i < input.len() { let s = LUT[input[i] as usize].as_bytes(); - output[o] = s[0] << 4 + s[1]; + output[o] = (s[0] << 4) + s[1]; i += 2; o += 1; } -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index 18c0d1a..433230f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -116,6 +116,6 @@ pub trait Hash { } mod common; +pub mod hexstr; pub mod hmac; pub mod sha256; -pub mod hexstr; \ No newline at end of file