diff --git a/CHANGELOG.md b/CHANGELOG.md index f719e2ae..fd1abffb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # CHANGELOG +## master + +Contributed: + +- Updated package.json to include repo (Thanks to https://github.com/v-zhzhou) + +Changes: + +- Add `keccak512` function +- Add `sha256` function + + ## 4.2.1 Aug 28, 2021 Contributed: diff --git a/packages/wasm-crypto/src/bundle.ts b/packages/wasm-crypto/src/bundle.ts index 9b617e7f..b50ecb85 100644 --- a/packages/wasm-crypto/src/bundle.ts +++ b/packages/wasm-crypto/src/bundle.ts @@ -217,6 +217,16 @@ export const keccak256 = withWasm((wasm) => } ); +export const keccak512 = withWasm((wasm) => + (data: Uint8Array): Uint8Array => { + const [ptr0, len0] = allocU8a(data); + + wasm.ext_keccak512(8, ptr0, len0); + + return resultU8a(); + } +); + export const pbkdf2 = withWasm((wasm) => (data: Uint8Array, salt: Uint8Array, rounds: number): Uint8Array => { const [ptr0, len0] = allocU8a(data); @@ -239,6 +249,16 @@ export const scrypt = withWasm((wasm) => } ); +export const sha256 = withWasm((wasm) => + (data: Uint8Array): Uint8Array => { + const [ptr0, len0] = allocU8a(data); + + wasm.ext_sha256(8, ptr0, len0); + + return resultU8a(); + } +); + export const sha512 = withWasm((wasm) => (data: Uint8Array): Uint8Array => { const [ptr0, len0] = allocU8a(data); diff --git a/packages/wasm-crypto/src/rs/hashing.rs b/packages/wasm-crypto/src/rs/hashing.rs index 6e5d84a3..dc9cca78 100644 --- a/packages/wasm-crypto/src/rs/hashing.rs +++ b/packages/wasm-crypto/src/rs/hashing.rs @@ -6,7 +6,7 @@ use byteorder::{ByteOrder, LittleEndian}; use hmac::Hmac; use pbkdf2::pbkdf2; use scrypt::{ScryptParams, scrypt}; -use sha2::{Digest, Sha512}; +use sha2::{Digest, Sha256, Sha512}; use tiny_keccak::{Hasher, Keccak}; use twox_hash::XxHash; use wasm_bindgen::prelude::*; @@ -57,6 +57,22 @@ pub fn ext_keccak256(data: &[u8]) -> Vec { result.to_vec() } +/// Create a keccak512 hash for the specified input +/// +// * data: Arbitrary data to be hashed +/// +/// Returns the hash as a vector +#[wasm_bindgen] +pub fn ext_keccak512(data: &[u8]) -> Vec { + let mut keccak = Keccak::v512(); + let mut result = [0u8; 64]; + + keccak.update(data); + keccak.finalize(&mut result); + + result.to_vec() +} + /// pbkdf2 kdf from an input, salt for the number of specified rounds /// /// * data: Arbitrary data to be hashed @@ -94,6 +110,22 @@ pub fn ext_scrypt(password: &[u8], salt: &[u8], log2_n: u8, r: u32, p: u32) -> V } } +/// sha256 hash for the specified input +/// +/// * data: Arbitrary data to be hashed +/// +/// Returns a vector with the hash result +#[wasm_bindgen] +pub fn ext_sha256(data: &[u8]) -> Vec { + let mut hasher = Sha256::new(); + + hasher.input(data); + + hasher + .result() + .to_vec() +} + /// sha512 hash for the specified input /// /// * data: Arbitrary data to be hashed @@ -154,6 +186,15 @@ pub mod tests { assert_eq!(hash[..], expected[..]); } + #[test] + fn can_keccak512() { + let data = b"test"; + let expected = hex!("1e2e9fc2002b002d75198b7503210c05a1baac4560916a3c6d93bcce3a50d7f00fd395bf1647b9abb8d1afcc9c76c289b0c9383ba386a956da4b38934417789e"); + let hash = ext_keccak512(data); + + assert_eq!(hash[..], expected[..]); + } + #[test] fn can_pbkdf2() { let salt = b"this is a salt"; @@ -174,6 +215,15 @@ pub mod tests { assert_eq!(hash[..], expected[..]); } + #[test] + fn can_sha256() { + let data = b"hello world"; + let expected = hex!("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"); + let hash = ext_sha256(data); + + assert_eq!(hash[..], expected[..]); + } + #[test] fn can_sha512() { let data = b"hello world"; diff --git a/packages/wasm-crypto/src/types.ts b/packages/wasm-crypto/src/types.ts index 308a19b7..4dee46c5 100644 --- a/packages/wasm-crypto/src/types.ts +++ b/packages/wasm-crypto/src/types.ts @@ -22,8 +22,10 @@ export interface WasmCryptoInstance { ext_ed_verify(a: number, b: number, c: number, d: number, e: number, f: number): number; ext_blake2b(a: number, b: number, c: number, d: number, e: number, f: number): void; ext_keccak256(a: number, b: number, c: number): void; + ext_keccak512(a: number, b: number, c: number): void; ext_pbkdf2(a: number, b: number, c: number, d: number, e: number, f: number): void; ext_scrypt(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number): void; + ext_sha256(a: number, b: number, c: number): void; ext_sha512(a: number, b: number, c: number): void; ext_twox(a: number, b: number, c: number, d: number): void; ext_sr_derive_keypair_hard(a: number, b: number, c: number, d: number, e: number): void; diff --git a/packages/wasm-crypto/test/all/hashing.cjs b/packages/wasm-crypto/test/all/hashing.cjs index 7e85faf1..70a94238 100644 --- a/packages/wasm-crypto/test/all/hashing.cjs +++ b/packages/wasm-crypto/test/all/hashing.cjs @@ -19,6 +19,14 @@ function keccak256 (wasm) { assert(hash === '0x2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e', 'ERROR: keccak256 does not match'); } +function keccak512 (wasm) { + const hash = u8aToHex(wasm.keccak512(stringToU8a('test'))); + + console.log('\tRES', hash); + + assert(hash === '0x1e2e9fc2002b002d75198b7503210c05a1baac4560916a3c6d93bcce3a50d7f00fd395bf1647b9abb8d1afcc9c76c289b0c9383ba386a956da4b38934417789e', 'ERROR: keccak512 does not match'); +} + function pbkdf2Hash (wasm) { const hash = u8aToHex(wasm.pbkdf2(stringToU8a('hello world'), stringToU8a('this is a salt'), 2048)); @@ -35,6 +43,14 @@ function scryptHash (wasm) { assert(hash === '0x745731af4484f323968969eda289aeee005b5903ac561e64a5aca121797bf7734ef9fd58422e2e22183bcacba9ec87ba0c83b7a2e788f03ce0da06463433cda6', 'ERROR: scryptHash does not match'); } +function sha256Hash (wasm) { + const hash = u8aToHex(wasm.sha256(stringToU8a('hello world'))); + + console.log('\tRES', hash); + + assert(hash === '0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9', 'ERROR: sha256Hash does not match'); +} + function sha512Hash (wasm) { const hash = u8aToHex(wasm.sha512(stringToU8a('hello world'))); @@ -56,8 +72,10 @@ function twoxHash (wasm) { module.exports = { blake2bHash, keccak256, + keccak512, pbkdf2Hash, scryptHash, + sha256Hash, sha512Hash, twoxHash };