Hashing
TODO
Calculate the SHA-256 digest of a file
Writes some data to a file, then calculates the SHA-256 digest::Digest
⮳ of the file's contents using digest::Context
⮳
use std::fs::File; use std::io::BufReader; use std::io::Read; use std::io::Write; use anyhow::Result; use data_encoding::HEXUPPER; use ring::digest::Context; use ring::digest::Digest; use ring::digest::SHA256; fn sha256_digest<R: Read>(mut reader: R) -> Result<Digest> { let mut context = Context::new(&SHA256); let mut buffer = [0; 1024]; loop { let count = reader.read(&mut buffer)?; if count == 0 { break; } context.update(&buffer[..count]); } Ok(context.finish()) } fn main() -> Result<()> { let path = "temp/file.txt"; let mut output = File::create(path)?; write!(output, "We will generate a digest of this text")?; let input = File::open(path)?; let reader = BufReader::new(input); let digest = sha256_digest(reader)?; println!("SHA-256 digest is {}", HEXUPPER.encode(digest.as_ref())); Ok(()) }
Sign and verify a message with HMAC digest
Uses ring::hmac
⮳ to creates a ring::signature::Signature
⮳ of a string then verifies the signature is correct.
use ring::error::Unspecified; use ring::hmac; use ring::rand; use ring::rand::SecureRandom; fn main() -> Result<(), Unspecified> { let mut key_value = [0u8; 48]; let rng = rand::SystemRandom::new(); rng.fill(&mut key_value)?; let key = hmac::Key::new(hmac::HMAC_SHA256, &key_value); let message = "Legitimate and important message."; let signature = hmac::sign(&key, message.as_bytes()); hmac::verify(&key, message.as_bytes(), signature.as_ref())?; Ok(()) }
TODO review password_hashing.md
review below from blessed.rs
General Purpose Hashing
For more algorithms, see Rust Crypto Hashes.
AEAD Encryption
For more algorithms, see Rust Crypto AEADs.
RSA
Digital Signatures
For more algorithms, see Rust Crypto Signatures.
ed25519 Use in conjunction with the ed25519-dalek crate.
Certificate Formats
For more formats, see Rust Crypto Formats.
TLS / SSL
rustls A portable pure-rust high-level implementation of TLS. Implements TLS 1.2 and higher.
native-tls Delegates to the system TLS implementations on windows and macOS, and uses OpenSSL on linux.