Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

export function hash(input: Uint8Array): Uint8Array
export function hashInto(input: Uint8Array, output: Uint8Array): void
export function hashAsync(input: Uint8Array): Promise<Uint8Array>
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { hash, hashInto } = nativeBinding
const { hash, hashInto, hashAsync } = nativeBinding

module.exports.hash = hash
module.exports.hashInto = hashInto
module.exports.hashAsync = hashAsync
27 changes: 26 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![deny(clippy::all)]

use hashtree_rs::{hash as HASH, init};
use napi::{bindgen_prelude::Uint8Array, Error};
use napi::{bindgen_prelude::{AsyncTask, Uint8Array}, Env, Error, Task};
use std::sync::Once;

#[macro_use]
Expand Down Expand Up @@ -45,3 +45,28 @@ pub fn hash_into(input: Uint8Array, mut output: Uint8Array) -> Result<(), Error>
HASH(output.as_mut(), input.as_ref(), output_len / 32);
Ok(())
}

pub struct AsyncHash {
input: Uint8Array
}

#[napi]
impl Task for AsyncHash {
type Output = Uint8Array;
type JsValue = Uint8Array;
fn compute(&mut self) -> Result<Self::Output, Error> {
hash(self.input.clone())
}

fn resolve(&mut self, _env: Env, output: Self::Output) -> Result<Self::JsValue, Error> {
Ok(output)
}
}

#[napi]
pub fn hash_async(input: Uint8Array) -> AsyncTask<AsyncHash> {
INIT.call_once(|| {
init();
});
AsyncTask::new(AsyncHash { input })
}
44 changes: 25 additions & 19 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import util from "node:util";
import {createHash} from "node:crypto";
import {hash, hashInto} from "../index";
import {hash, hashInto, hashAsync} from "../index";

type BufferLike = string | Uint8Array | Buffer;

Expand Down Expand Up @@ -47,29 +48,34 @@ function nodeCryptoHashInto(input: Buffer, output: Buffer): void {
}
}

describe("should hash similarly to crypto.createHash('sha256')", () => {
it("should hash similarly to crypto.createHash('sha256')", () => {
for (let i = 1; i <= 16; i++) {
test(`No of Chunks=${i}`, () => {
for (let j = 0; j < 255; j++) {
const input = Buffer.alloc(CHUNK_SIZE * i, j);
expectEqualHex(hash(input), nodeCryptoHash(input));
}
});
for (let j = 0; j < 255; j++) {
const input = Buffer.alloc(CHUNK_SIZE * i, j);
expectEqualHex(hash(input), nodeCryptoHash(input));
}
}
});

describe("should hashInto similarly to crypto.createHash('sha256')", () => {
it("should asyncHash similarly to crypto.createHash('sha256')", async () => {
for (let i = 1; i <= 16; i++) {
test(`No of Chunks=${i}`, () => {
for (let j = 0; j < 255; j++) {
const input = Buffer.alloc(CHUNK_SIZE * i, j);
const output1 = Buffer.alloc((CHUNK_SIZE / 2) * i);
const output2 = Buffer.alloc((CHUNK_SIZE / 2) * i);
for (let j = 0; j < 255; j++) {
const input = Buffer.alloc(CHUNK_SIZE * i, j);
expectEqualHex(await hashAsync(input), nodeCryptoHash(input));
}
}
});
Comment on lines +60 to +67
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just split this into a separate test so there is only one expect per test. Also cleaned up the output, i added a describe on PR#3 to show the chunk size but seems unnecessary in retrospect


it("should hashInto similarly to crypto.createHash('sha256')", () => {
for (let i = 1; i <= 16; i++) {
for (let j = 0; j < 255; j++) {
const input = Buffer.alloc(CHUNK_SIZE * i, j);
const output1 = Buffer.alloc((CHUNK_SIZE / 2) * i);
const output2 = Buffer.alloc((CHUNK_SIZE / 2) * i);

nodeCryptoHashInto(input, output2);
hashInto(input, output1);
expectEqualHex(output1, output2);
}
});
nodeCryptoHashInto(input, output2);
hashInto(input, output1);
expectEqualHex(output1, output2);
}
}
});