To create a Blake2b hash from a string in Rust, you can use the blake2_rfc crate which provides a simple interface to the Blake2 family of hash functions.
First, add blake2_rfc as a dependency in your Cargo.toml file:
37 chars3 linesThen, in your Rust code, import the crate by adding this to the top of your file:
main.rs34 chars2 lines
To hash a string using Blake2b, you can create a new Blake2b object and pass the string to the update method:
main.rs174 chars7 linesThe new method creates a new Blake2b object with a 64-byte output size (i.e. a 512-bit hash). The update method adds the input string to the hash. Finally, the finalize method completes the hash and returns the resulting hash value as a byte array.
You can then call the hash_string function with a string input to get the corresponding Blake2b hash:
main.rs85 chars5 linesThis will output the following hash:
main.rs129 chars2 lines
gistlibby LogSnag