Skip to content

Commit

Permalink
Merge pull request #9 from SheIITear/hash
Browse files Browse the repository at this point in the history
Basic hash implementations.
  • Loading branch information
AlenVelocity authored Mar 17, 2022
2 parents 86a7955 + 68c2405 commit ef3170f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ rusty_express = "^0.3.0"
rand = "0.8.5"
reqwest = { version = "0.11.0", features = ["blocking", "json"] }
serde_json = "1.0"
rust-crypto = "0.2.36"

[lib]
crate-type = ["cdylib", "rlib"]
Expand Down
83 changes: 83 additions & 0 deletions src/std_library/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use std::{collections::HashMap};
use crypto::md5::Md5;
use crypto::sha1::Sha1;
use crypto::sha2;
use crypto::sha3::Sha3;
use crypto::whirlpool::Whirlpool;
use crypto::digest::Digest;

use crate::evaluation::object::Object;

use super::Res;

/// Adds the standard library to the global environment.
pub fn add_globals() -> Res {
let mut globals = HashMap::new();
globals.insert(String::from("hasher"), Object::Inbuilt(hasher));
Res {
globals,
raw: None,
}
}

pub fn hasher(args: Vec<Object>) -> Object {

if args.len() != 2 {
return Object::Error(format!(
"Wrong number of arguments. Got {}. Expected mode and string",
args.len()
));
}

match &args[0] {
Object::String(s) => {
let algo: &str = &s;
match algo {
"md5" => {
let mut hasher = Md5::new();
hasher.input_str(&args[1].to_string());
let result = hasher.result_str();
return Object::String(result);
},
"sha1" => {
let mut hasher = Sha1::new();
hasher.input_str(&args[1].to_string());
let result = hasher.result_str();
return Object::String(result);
},
"sha256" => {
let mut hasher = sha2::Sha256::new();
hasher.input_str(&args[1].to_string());
let result = hasher.result_str();
return Object::String(result);
},
"sha512" => {
let mut hasher = sha2::Sha512::new();
hasher.input_str(&args[1].to_string());
let result = hasher.result_str();
return Object::String(result);
},
"sha3_256" => {
let mut hasher = Sha3::sha3_256();
hasher.input_str(&args[1].to_string());
let result = hasher.result_str();
return Object::String(result);
},
"sha3_512" => {
let mut hasher = Sha3::sha3_512();
hasher.input_str(&args[1].to_string());
let result = hasher.result_str();
return Object::String(result);
},
"whirlpool" => {
let mut hasher = Whirlpool::new();
hasher.input_str(&args[1].to_string());
let result = hasher.result_str();
return Object::String(result);
},
_ => Object::Error("Algorithm not supported".to_string())
}
}
o => Object::Error(format!("First argument must be a string. Got {}", o)),
}
}
4 changes: 3 additions & 1 deletion src/std_library/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod string;
pub mod math;
pub mod json;
pub mod http;
pub mod hash;
/// Function to load a standard library
/// # Arguments
/// * `lib` - The name of the library to load.
Expand All @@ -27,6 +28,7 @@ pub fn get_std_lib(lib: String) -> Option<Res> {
"std:math" => Some(math::add_globals()),
"std:json" => Some(json::add_globals()),
"std:http" => Some(http::add_globals()),
"std:hash" => Some(hash::add_globals()),
_ => None,
}
}
}

0 comments on commit ef3170f

Please sign in to comment.