//! Contains contexts used for the digest algorithms.
//!
//! These contexts contain the digest name and the salt to be used with the digest.
//!
//! # Defaults
//! Both the digest name (`Algorithm`) and the `Context` itself implement `Default`.
//! The default algorithm is `SHA256Truncated`, and the default `Context` uses this algorithm along with the default salt (which is the library's hard-coded static salt.)
usecrate::*;
usestd::{
io::{
@ -7,14 +14,22 @@ use std::{
};
/// An algorithm to use for the context.
///
/// # CRC
/// `CRC32` and `CRC64` are only available if compiled with the default "crc" feature enabled.
/// If the library is compiled without this feature, but with the "ffi" feature (i.e. generates native libraries), then FFI requests for the CRC family of digests will instead use the default (`Sha256Truncated`).
#[derive(Clone,Debug,PartialEq,Eq,Hash)]
pubenumAlgorithm
{
#[cfg(feature="crc")]
/// The 32 bit CRC checksum (requires default feature `crc`)
Crc32,
#[cfg(feature="crc")]
/// The 64 bit CRC checksum (requires default feature `crc`)
Crc64,
/// The SHA256 hash
Sha256,
/// The SHA256 hash truncated to the first 64 bits
Sha256Truncated,
}
@ -27,6 +42,9 @@ impl Default for Algorithm
}
/// A kana-hash context containing it's salt and algorithm.
///
/// # Default
/// The default context contains the `SHA256Truncated` digest algorithm and the library's hard-coded static salt.
//! The salt (if any) is fed into the digest directly after all the data.
//! (See `ctx` and `salt` modules).
//!
//! ## Generating kana mnemonics from arbitrary data
//! To use the mnemonic generation algorithm on any binary data instead of just hash outputs, the `Digest` iterator type is provided.
//! The `Digest` iterator can be created from any type implementing `std::io::Read` and produces a kana mnemonic reading from the stream until its end.
//! ```
//! # use khash::Digest;
//! let input = "Hello world!";
//! let mnemonic: String = Digest::new(&mut input.as_bytes()).collect(); // Read the bytes from the `input` string and collect the kana mnemonic into a `String`
//! Salt for the digest `Context` (see module `ctx`)
//!
//! # Salt kinds
//! * Hard-coded embedded 32 byte salt (default)
//! * Fixed compile time or global static 32 byte salt
//! * Fixed runtime 32 byte salt
//! * Dynamically sized runtime salt
//! * No salt
//!
//! You can also generate a random salt at runtime with `Salt::random()` which uses `getrandom`'s RNG to create a fixed 32 bytes salt.
//!
//! # Method of salting
//! The salt is fed into the hashing function directly after the data.
//!
//! # FFI note
//! When generating a dynamic salt from an FFI context, there is a hard limit of 1024 bytes for safety reasons. This is more than enough data for a salt.
#[cfg(feature="ffi")]usemalloc_array::*;
usegetrandom::{
@ -14,19 +31,35 @@ use std::{
convert::{TryInto,TryFrom},
};
/// The static salt size
/// The static salt size. (32 bytes.)
///
/// When providing a compile-time or otherwise global salt, it must be of this size.
pubconstSIZE: usize=32;
/// The default static salt
/// The default embedded static salt.
///
/// It is recommended to use your own salt instead of this one, but this is used as the default if a salt option is not provided.
///
/// # Guarantee
/// This salt is guaranteed stay the same throughout all versions and iterations of this library.
/// It was randomly generated as the 32 bytes hex literal `hex!("6787f049791466d5a31a3aa6f7138d8fbb907fd1785758298b5c97b0f3fb31ff")`.
/// This type can be used to generate kana mnemonics from any data.
/// It wraps a type implementing `std::io::Read` and produces a kana mnemonic reading from the stream until its end.
/// ```
/// # use khash::Digest;
/// let input = "Hello world!";
/// let mnemonic: String = Digest::new(&mut input.as_bytes()).collect(); // Read the bytes from the `input` string and collect the kana mnemonic into a `String`
/// ```
pubstructDigest<'a,T>
whereT: Read
{
@ -9,7 +17,7 @@ where T: Read
impl<'a,T: Read>Digest<'a,T>
{
/// Create a new stream digest from the input
/// Create a new stream digest iterator from the input stream.
pubfnnew(input: &'amutT)-> Self
{
Self{input}
@ -18,7 +26,7 @@ impl<'a, T: Read> Digest<'a, T>
impl<'a,T: Read>IteratorforDigest<'a,T>
{
typeItem=String;
typeItem=String;//TODO: Change this to `char` and keep an internal buffer that we `fmt::write!` the mnemonic digest to instead of `format!`ing it.