-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
use fish_hash::Context; | ||
use napi::{bindgen_prelude::Buffer, JsBuffer}; | ||
use napi_derive::napi; | ||
|
||
#[napi] | ||
pub struct FishHashContext { | ||
inner: Context, | ||
} | ||
|
||
#[napi] | ||
impl FishHashContext { | ||
#[napi(constructor)] | ||
pub fn new(full: bool) -> Self { | ||
Self { | ||
inner: Context::new(full), | ||
} | ||
} | ||
|
||
#[napi] | ||
pub fn prebuild_dataset(&mut self, threads: u32) { | ||
self.inner.prebuild_dataset(threads as usize) | ||
} | ||
|
||
#[napi] | ||
pub fn hash(&mut self, header: JsBuffer) -> Buffer { | ||
let bytes = header.into_value().unwrap(); | ||
|
||
let mut output = [0u8; 32]; | ||
fish_hash::hash(&mut output, &mut self.inner, bytes.as_ref()); | ||
|
||
Buffer::from(output.to_vec()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
import { FishHashContext } from '..' | ||
|
||
describe('FishHashContext', () => { | ||
it('should be able to generate a new FishHash context', () => { | ||
const context = new FishHashContext(false) | ||
expect(context).toBeDefined() | ||
}) | ||
|
||
it('should be able to hash a buffer', () => { | ||
let data = Buffer.from('the quick brown fox jumps over the lazy dog') | ||
const context = new FishHashContext(false) | ||
|
||
const hash = context.hash(data) | ||
expect(hash.toString('hex')).toEqual('6f4429716dc009d5d3b9775a4d6a5d58bccd9f73386bf88da7d5afdf5deb50f1') | ||
}) | ||
}) |