Skip to content

Instantly share code, notes, and snippets.

@andydude
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andydude/6d9bfe5804350b326be3 to your computer and use it in GitHub Desktop.
Save andydude/6d9bfe5804350b326be3 to your computer and use it in GitHub Desktop.
dgst.rs
extern crate webclient;
use std::io::Reader;
use std::io::stdin;
use webclient::digest::types::HashAlgorithm;
pub fn hash_algorithm_from_lower(name: &str) -> Option<Box<HashAlgorithm+'static>> {
match name {
"-md5" => Some(box webclient::digest::md5::md5_new() as Box<HashAlgorithm>),
"-sha1" => Some(box webclient::digest::sha1::sha1_new() as Box<HashAlgorithm>),
"-sha224" => Some(box webclient::digest::sha2::sha224_new() as Box<HashAlgorithm>),
"-sha256" => Some(box webclient::digest::sha2::sha256_new() as Box<HashAlgorithm>),
"-sha384" => Some(box webclient::digest::sha2::sha384_new() as Box<HashAlgorithm>),
"-sha512" => Some(box webclient::digest::sha2::sha512_new() as Box<HashAlgorithm>),
"-sha512224" => Some(box webclient::digest::sha2::sha512224_new() as Box<HashAlgorithm>),
"-sha512256" => Some(box webclient::digest::sha2::sha512256_new() as Box<HashAlgorithm>),
_ => None
}
}
fn main() {
// get message
let mut reader = stdin();
let message: Vec<u8> = reader.read_to_end().unwrap();
// get hash algorithm
let args = std::os::args();
let command: &str = args[1].as_slice();
let mut hasher = hash_algorithm_from_lower(command).unwrap();
// compute hash
let bytes = hasher.hash(message);
for byte in bytes.into_iter() {
print!("{:02x}", byte);
}
println!("");
}
$ cargo build
Compiling webtools v0.0.1 (file:///Users/ajr/Workspaces/Rust/my/rust-webtools)
# :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment