Crates.io | sha3-rust |
lib.rs | sha3-rust |
version | 0.1.1 |
source | src |
created_at | 2024-06-05 16:33:42.445317 |
updated_at | 2024-06-05 16:49:41.638291 |
description | This crate provides an implementation of the Keccak (SHA-3) cryptographic hash function family |
homepage | https://github.com/jensutradhar/keccak-sha3-rust |
repository | https://github.com/jensutradhar/keccak-sha3-rust |
max_upload_size | |
id | 1262953 |
size | 23,018 |
This crate provides an implementation of the Keccak (SHA-3) cryptographic hash function family.
The Sha3 crate is a Rust library that implements the Keccak (SHA-3) cryptographic hash function family. It offers several hash functions with different output lengths, including 224, 256, 384, and 512 bits. These hash functions are created using the sha3!
macro, which defines the hash function with a specific output length. The crate also provides utility functions for converting bytes to bits and vice versa, which are used internally by the hash functions.
To use this crate in your Rust project, add the following line to your Cargo.toml
file:
[dependencies]
sha3-rust = "0.1.1"
Then, in your Rust code, you can import and use the SHA-3 hash functions as follows:
use sha3_rust::*;
fn main() {
let input = "Hello, world!";
let hash = sha3_256(input.as_bytes());
println!("SHA3-256 hash of '{}': {:?}", input, hash);
}
SHA3-256 hash of 'Hello, world!': [172, 79, 176, 238 ... 139, 93, 150]
// Create a string to hash.
let input_str = "Hello, world!";
// Compute the SHA3-256 hash of the string.
let hash_256 = sha3_256(input_str.as_bytes());
println!("SHA3-256 hash of '{}': {:?}", input_str, hash_256);
// Path to the file to hash.
let file_path = "example.txt";
// Read the contents of the file.
let file_contents = std::fs::read(file_path).expect("Failed to read file");
// Compute the SHA3-512 hash of the file.
let hash_512 = sha3_512(&file_contents);
println!("SHA3-512 hash of file '{}': {:?}", file_path, hash_512);
// An array of byte slices representing the inputs to hash.
let inputs: [&[u8]; 3] = [&[1, 2, 3], &[4, 5, 6, 7], &[8, 9]];
// Compute the SHA3-224 hash of each input.
for input in &inputs {
let hash = sha3_224(input);
// Print the hash of each input.
println!("SHA3-224 hash of {:?}: {:?}", input, hash);
}
// Password to hash.
let user_password = "s3cr3t_p@ssw0rd";
// Compute the SHA3-384 hash of the password.
let hash_384 = sha3_384(user_password.as_bytes());
println!("SHA3-384 hash of user password: {:?}", hash_384);
// Sensitive data to hash.
let sensitive_data = b"0123456789abcdef";
// Compute the SHA3-256 hash of the sensitive data.
let hash_256_secure = sha3_256(sensitive_data);
println!("Secure SHA3-256 hash of sensitive data: {:?}", hash_256_secure);
The crate's codebase consists of various functions and macros that implement the Keccak permutation and the SHA-3 hash functions. Here's a brief explanation of some key components:
State
type represents the state array used in the Keccak permutation. It is a 3-dimensional array of boolean values.theta
, rho
, pi
, chi
, and iota
implement the different steps of the Keccak permutation, as specified in the SHA-3 standard.rc
function calculates the round constants used in the iota
step of the permutation.round
function applies a single round of the Keccak permutation.sponge
function implements the sponge construction used in the Keccak hash function. It takes a sponge function f
, a padding function pad
, a block size r
, an input n
, and a desired output size d
.pad101
function implements the padding scheme specified in the Keccak specification.sha3!
macro defines SHA-3 hash functions with different output lengths, using the sponge construction and padding functions.In addition to hashing strings and files, the Sha3 crate can be used in various cryptographic applications, such as:
Contributions are welcome! If you encounter any bugs or have suggestions for improvements, please open an issue or submit a pull request on GitHub.
This project is licensed under the MIT License - see the LICENSE file for details.
Feel free to further customize and expand upon this README.md as needed for your project!