1 unstable release

0.1.0 Dec 28, 2018

#19 in #trait-object

MPL-2.0 license

13KB
278 lines

Simple dynamic trait-object based hashing library

This library allows you to define a thin non-generic interface, over which you can pass various hashes and hash functions. This is done using Trait Objects.

While every crate can define and implement custom Hash / HashType structs, this library has built-in support for the

  • sha3
  • sha2

crates.

These can be enabled by the "sha2" or "sha3" features respectively.

Example

Let's suppose you're writing a web-server library that accepts a hash from a client. The hash and hash type are passed as a byte slice and &str respectively. The web-server shouldn't have any knowledge about the allowed hash types, these can be passed in at runtime:

use dynhash::{Hash, HashType};

fn new_hash(
    available_types: &[&'static HashType],
    digest: &[u8],
    hash_type: &str,
) -> Option<Box<Hash>> {
    for h in available_types.iter() {
        if h.as_string() == hash_type {
            return h.new_with_content(digest).ok();
        }
    }

    return None;
}

This allows the web-server library to support all kinds of hashes, that the higher-level crate chooses to support.

Dependencies

~21–345KB