File hashes
SHA-256, SHA-512 and BLAKE3 of files read as bytes: checksums for downloads, dedupe and cache keys.
Version 1.0.0 · 4 functions · 3 crates
Functions
What use file_hash.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use sha256_file, sha512_file from file_hash (Quillo 0.6).
sha256_file(path: str) → stror an errorThe SHA-256 of a file's bytes, as hex: what sha256sum prints.
sha512_file(path: str) → stror an errorThe SHA-512 of a file's bytes, as hex.
blake3_file(path: str) → stror an errorThe BLAKE3 of a file's bytes, as hex: several times faster than SHA-256, for big files.
same_file_contents(a: str, b: str) → boolor an errorWhether two files hold the same bytes.
Get it
$ quillo install file_hash # file_hash.qwer into this folder$ quillo install file_hash --example # and its example, file_hash.qweOr with curl:
$ curl -O https://quillolang.org/plugins/files/file_hash.qwer$ curl -O https://quillolang.org/plugins/files/file_hash.qweThen, in your program, all of it or only what you call:
use file_hash.qweruse sha256_file, sha512_file from file_hash // Quillo 0.6: only theseIt uses these crates, which cargo fetches the first time the plugin is built:
sha2 = "0.10"blake3 = "1"hex = "0.4"Example
// A checksum for a download, and an ETag that changes with the file.//// quillo run file_hash.qwe then open http://localhost:8080/sumuse "file_hash.qwer"route GET /sum { write_file("release.txt", "Quillo 0.5.0") { sha256: sha256_file("release.txt")?, blake3: blake3_file("release.txt")? }}$ quillo run file_hash.qwe$ curl 'localhost:8080/sum'Source
//! Hashes of files, read as bytes: SHA-256, SHA-512 and BLAKE3, for checksums, dedupe and cache keys.//! version: 1.0.0//!//! [dependencies]//! sha2 = "0.10"//! blake3 = "1"//! hex = "0.4"use sha2::{Digest, Sha256, Sha512};use std::io::Read;fn each_chunk(path: &str, mut f: impl FnMut(&[u8])) -> Result<(), String> { let mut file = std::fs::File::open(path).map_err(|e| format!("{}: {}", path, e))?; let mut buf = vec![0u8; 1 << 16]; loop { let n = file.read(&mut buf).map_err(|e| e.to_string())?; if n == 0 { return Ok(()); } f(&buf[..n]); }}/// The SHA-256 of a file's bytes, as hex: what `sha256sum` prints.pub fn sha256_file(path: &str) -> Result<String, String> { let mut h = Sha256::new(); each_chunk(path, |b| h.update(b))?; Ok(hex::encode(h.finalize()))}/// The SHA-512 of a file's bytes, as hex.pub fn sha512_file(path: &str) -> Result<String, String> { let mut h = Sha512::new(); each_chunk(path, |b| h.update(b))?; Ok(hex::encode(h.finalize()))}/// The BLAKE3 of a file's bytes, as hex: several times faster than SHA-256, for big files.pub fn blake3_file(path: &str) -> Result<String, String> { let mut h = blake3::Hasher::new(); each_chunk(path, |b| { h.update(b); })?; Ok(h.finalize().to_hex().to_string())}/// Whether two files hold the same bytes.pub fn same_file_contents(a: &str, b: &str) -> Result<bool, String> { let (ma, mb) = (std::fs::metadata(a).map_err(|e| e.to_string())?, std::fs::metadata(b).map_err(|e| e.to_string())?); Ok(ma.len() == mb.len() && blake3_file(a)? == blake3_file(b)?)}Related
hashes.qwer · 1.0.0MD5, SHA-1, SHA-2, SHA-3, BLAKE2, xxHash and CRC-32 of text and files, and Subresource Integrity values for script tags.7 views · 6 downloadsBencode and torrents bencode.qwer · 1.0.0Bencode, BitTorrent's format, encoded and decoded; .torrent files read (files, sizes, trackers, info hash, magnet link) and made from a file.7 views · 10 downloadsage age.qwer · 1.0.0age encryption: text and files encrypted to people's public keys and opened only with their identity, compatible with the age command.7 views · 8 downloadsEncrypt encrypt.qwer · 1.0.0Encrypt and decrypt text and files with a secret key (AES-256-GCM): API tokens, notes and uploads kept unreadable at rest.7 views · 7 downloadsPKCS#12 files pkcs12.qwer · 1.0.0Read .p12 and .pfx files in pure Rust, key and chain out in order, MAC and password checked, modern PBES2 with AES, and write ones OpenSSL opens. The old RC2, RC4 and 3DES ciphers are refused by name, with how to convert the file.2 views · 8 downloadsUpload checks upload_guard.qwer · 1.0.0Uploads checked before they are kept: the real type by magic bytes against the name and an allow list, size, double extensions, programs, zip bombs and zip slips, SVGs and PDFs that run scripts, macros and polyglots.7 views · 7 downloadsThread 0 posts
On File hashes: questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.