I want to convert a string of characters (a SHA256 hash) to hex in Rust:
extern crate crypto;
extern crate rustc_serialize;
use rustc_serialize::hex::ToHex;
use crypto::digest::Digest;
use crypto::sha2::Sha256;
fn gen_sha256(hashme: &str) -> String {
let mut sh = Sha256::new();
sh.input_str(hashme);
sh.result_str()
}
fn main() {
let hash = gen_sha256("example");
hash.to_hex()
}
The compiler says:
error[E0599]: no method named `to_hex` found for type `std::string::String` in the current scope
--> src/main.rs:18:10
|
18 | hash.to_hex()
| ^^^^^^
I can see this is true; it looks like it's only implemented for [u8]
.
What am I to do? Is there no method implemented to convert from a string to hex in Rust?
My Cargo.toml dependencies:
[dependencies]
rust-crypto = "0.2.36"
rustc-serialize = "0.3.24"
edit I just realized the string is already in hex format from the rust-crypto library. D'oh.