Minor doc, format, and clippy fixes.
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -4,4 +4,4 @@ version = 4
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "emsha"
|
name = "emsha"
|
||||||
version = "1.0.2"
|
version = "1.0.3"
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ repository = "https://git.wntrmute.dev/wntrmute/emsha-rs"
|
|||||||
categories = ["cryptography", "no-std", "embedded"]
|
categories = ["cryptography", "no-std", "embedded"]
|
||||||
keywords = ["sha256", "hmac", "hash", "embedded", "no_std"]
|
keywords = ["sha256", "hmac", "hash", "embedded", "no_std"]
|
||||||
license-file = "LICENSE"
|
license-file = "LICENSE"
|
||||||
version = "1.0.2"
|
version = "1.0.3"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
publish = ["kellnr"]
|
publish = ["kellnr"]
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//! Demonstrator of SHA256 hashing.
|
//! Demonstrator of SHA256 hashing.
|
||||||
|
|
||||||
use emsha::{self, sha256, Hash};
|
|
||||||
use emsha::hexstr::to_hex;
|
use emsha::hexstr::to_hex;
|
||||||
|
use emsha::{self, Hash, sha256};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::io::{self, BufReader};
|
use std::io::{self, BufReader};
|
||||||
@@ -11,10 +11,9 @@ const CHUNK_SIZE: usize = 512;
|
|||||||
fn transmute_error(res: emsha::Result<()>) -> io::Result<()> {
|
fn transmute_error(res: emsha::Result<()>) -> io::Result<()> {
|
||||||
match res {
|
match res {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(e) => return Err(
|
Err(e) => {
|
||||||
io::Error::new(
|
Err(io::Error::other(format!("{:}", e) /* error */))
|
||||||
io::ErrorKind::Other,
|
}
|
||||||
format!("{:}", e), /* error */)),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +32,7 @@ fn hash_file(path: &str) -> io::Result<()> {
|
|||||||
break; // EOF reached
|
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];
|
let mut d: [u8; sha256::SIZE] = [0; sha256::SIZE];
|
||||||
@@ -46,12 +45,12 @@ fn hash_file(path: &str) -> io::Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> io::Result<()>{
|
fn main() -> io::Result<()> {
|
||||||
let args: Vec<String> = std::env::args().collect();
|
let args: Vec<String> = std::env::args().collect();
|
||||||
|
|
||||||
for i in 1..args.len() {
|
for arg in args.iter().skip(1) {
|
||||||
hash_file(args[i].as_str())?;
|
hash_file(arg.as_str())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn rotr32(x: u32, n: u8) -> u32 {
|
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
|
#[allow(non_snake_case)] // the name is taken from the RFC
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
//! Various utilities for encoding to and from hex strings.
|
//! Various utilities for encoding to and from hex strings.
|
||||||
|
|
||||||
const LUT: [&str; 256] = [
|
const LUT: [&str; 256] = [
|
||||||
"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0a",
|
"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0a",
|
||||||
"0b", "0c", "0d", "0e", "0f", "10", "11", "12", "13", "14", "15",
|
"0b", "0c", "0d", "0e", "0f", "10", "11", "12", "13", "14", "15",
|
||||||
@@ -26,7 +27,8 @@ const LUT: [&str; 256] = [
|
|||||||
"fd", "fe", "ff",
|
"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<const N: usize, const M: usize>(
|
pub fn to_hex<const N: usize, const M: usize>(
|
||||||
input: &[u8; N],
|
input: &[u8; N],
|
||||||
output: &mut [u8; M],
|
output: &mut [u8; M],
|
||||||
@@ -44,7 +46,8 @@ pub fn to_hex<const N: usize, const M: usize>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
/// Convert a hex string array in `input` to a binary array in
|
||||||
|
/// `output`.
|
||||||
pub fn from_hex<const N: usize, const M: usize>(
|
pub fn from_hex<const N: usize, const M: usize>(
|
||||||
input: &[u8; M],
|
input: &[u8; M],
|
||||||
output: &mut [u8; N],
|
output: &mut [u8; N],
|
||||||
@@ -55,9 +58,9 @@ pub fn from_hex<const N: usize, const M: usize>(
|
|||||||
|
|
||||||
while i < input.len() {
|
while i < input.len() {
|
||||||
let s = LUT[input[i] as usize].as_bytes();
|
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;
|
i += 2;
|
||||||
o += 1;
|
o += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,6 +116,6 @@ pub trait Hash {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mod common;
|
mod common;
|
||||||
|
pub mod hexstr;
|
||||||
pub mod hmac;
|
pub mod hmac;
|
||||||
pub mod sha256;
|
pub mod sha256;
|
||||||
pub mod hexstr;
|
|
||||||
Reference in New Issue
Block a user