Expand description
Digest hashing and validation. The Digest module is mostly a thin
wrapper around the digest crate and a selection of hashes provided
by the RustCrypto project, with some additional features for a pkgsrc
context.
§Examples
use pkgsrc::digest::{Digest, DigestResult};
use std::fs::File;
use std::path::PathBuf;
use std::str::FromStr;
fn main() -> DigestResult<()> {
/* Select digest using an explicit type. */
let d = Digest::BLAKE2s;
let h = d.hash_str("hello world")?;
assert_eq!(h, "9aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b");
/* Set digest from an input string. */
let d = Digest::from_str("RMD160")?;
let h = d.hash_str("hello world")?;
assert_eq!(h, "98c615784ccb5fe5936fbc0cbe9dfdb408d92f0f");
/*
* Internally .finalize() is called on the underlying digest type, so
* state is reset each time and a new string can be hashed.
*/
let h = d.hash_str("hello again")?;
assert_eq!(h, "4240355d8422a9f6d7cca0aee38751fb287d2cc2");
/*
* Hash a file. The entire file contents are hashed with no special
* parsing.
*/
let mut file = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
file.push("tests/data/digest.txt");
let mut f = File::open(&file)?;
let h = d.hash_file(&mut f)?;
assert_eq!(h, "f20aa3e2ffd45a2915c663e46be79d97e10dd6a5");
/*
* Hash a patch. These have special handling to remove any lines that
* contain the string "$NetBSD", so that CVS expansion does not affect
* the hash.
*/
let d = Digest::from_str("SHA1")?;
let mut file = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
file.push("tests/data/patch-Makefile");
let mut f = File::open(&file)?;
let h = d.hash_patch(&mut f)?;
assert_eq!(h, "ab5ce8a374d3aca7948eecabc35386d8195e3fbf");
Ok(())
}Enums§
- Digest
- The
Digestenum contains an entry for every supported digest algorithm. All of the algorithms are from the RustCryptohashescollection. - Digest
Error - The
DigestErrorenum contains all of the possibleDigesterrors.
Type Aliases§
- Digest
Result - A type alias for the result from the creation of a
Digest, withDigestErrorreturned inErrvariants.