From b3c72d1e4554b5fd58cbf3ba2fbf56275446447b Mon Sep 17 00:00:00 2001 From: skdltmxn Date: Wed, 17 Jun 2020 06:12:50 +0900 Subject: feat(std/hash): reimplement all hashes in WASM (#6292) --- std/hash/_wasm/src/lib.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 std/hash/_wasm/src/lib.rs (limited to 'std/hash/_wasm/src/lib.rs') diff --git a/std/hash/_wasm/src/lib.rs b/std/hash/_wasm/src/lib.rs new file mode 100644 index 000000000..5ada7e68a --- /dev/null +++ b/std/hash/_wasm/src/lib.rs @@ -0,0 +1,51 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +use digest::{Digest, DynDigest}; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub struct DenoHash { + inner: Box, +} + +#[wasm_bindgen] +pub fn create_hash(algorithm: &str) -> Result { + let hash: Option> = match algorithm { + "md2" => Some(Box::new(md2::Md2::new())), + "md4" => Some(Box::new(md4::Md4::new())), + "md5" => Some(Box::new(md5::Md5::new())), + "ripemd160" => Some(Box::new(ripemd160::Ripemd160::new())), + "ripemd320" => Some(Box::new(ripemd320::Ripemd320::new())), + "sha1" => Some(Box::new(sha1::Sha1::new())), + "sha224" => Some(Box::new(sha2::Sha224::new())), + "sha256" => Some(Box::new(sha2::Sha256::new())), + "sha384" => Some(Box::new(sha2::Sha384::new())), + "sha512" => Some(Box::new(sha2::Sha512::new())), + "sha3-224" => Some(Box::new(sha3::Sha3_224::new())), + "sha3-256" => Some(Box::new(sha3::Sha3_256::new())), + "sha3-384" => Some(Box::new(sha3::Sha3_384::new())), + "sha3-512" => Some(Box::new(sha3::Sha3_512::new())), + "keccak224" => Some(Box::new(sha3::Keccak224::new())), + "keccak256" => Some(Box::new(sha3::Keccak256::new())), + "keccak384" => Some(Box::new(sha3::Keccak384::new())), + "keccak512" => Some(Box::new(sha3::Keccak512::new())), + _ => None, + }; + + if let Some(h) = hash { + Ok(DenoHash { inner: h }) + } else { + let err_msg = format!("unsupported hash algorithm: {}", algorithm); + Err(JsValue::from_str(&err_msg)) + } +} + +#[wasm_bindgen] +pub fn update_hash(hash: &mut DenoHash, data: &[u8]) { + hash.inner.input(data) +} + +#[wasm_bindgen] +pub fn digest_hash(hash: &mut DenoHash) -> Box<[u8]> { + hash.inner.result_reset() +} -- cgit v1.2.3