Security

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

#files#checksums#sha256#blake3

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 error

The SHA-256 of a file's bytes, as hex: what sha256sum prints.

sha512_file(path: str) → stror an error

The SHA-512 of a file's bytes, as hex.

blake3_file(path: str) → stror an error

The 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 error

Whether 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.qwe

Or with curl:

$ curl -O https://quillolang.org/plugins/files/file_hash.qwer$ curl -O https://quillolang.org/plugins/files/file_hash.qwe

Then, in your program, all of it or only what you call:

main.qwe
use file_hash.qweruse sha256_file, sha512_file from file_hash      // Quillo 0.6: only these

It uses these crates, which cargo fetches the first time the plugin is built:

[dependencies]
sha2 = "0.10"blake3 = "1"hex = "0.4"

Example

file_hash.qwe
// 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

file_hash.qwer
//! 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)?)}
Thread
76Useful? Like it, or add to its thread below.

Thread 0 posts

On File hashes: questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.