Skip to content

Commit

Permalink
add blake3 algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
jiabochao committed Jan 8, 2023
1 parent 91e8857 commit 592b0c6
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 52 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ sha3 = "0.10.6"
sm3 = "0.4.1"
belt-hash = "0.1.0"
blake2 = "0.10.6"
blake3 = "1.3.3"
fsb = "0.1.3"
gost94 = "0.10.4"
groestl = "0.10.1"
Expand Down
48 changes: 25 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ Hashes utils for node.js based on [RustCrypto](https://github.com/RustCrypto/has
npm i @jthinking/hashes-node
```

## Usage

```js
// import hashes-node
import { md5, blake3, sm3 } = require('@jthinking/hashes-node')

// convert data to Buffer
const buf = Buffer.from('123', 'UTF-8')

// md5 hex
const md5Hex = md5(buf)

// blake3 hex
const blake3Hex = blake3(buf)

// sm3 hex
const sm3Hex = sm3(buf)

// other algorithms
// ...
```

## Supported Algorithms

- md2
Expand All @@ -22,6 +44,7 @@ npm i @jthinking/hashes-node
- belt_hash
- blake2s256
- blake2b512
- blake3
- fsb512
- fsb256
- gost94
Expand All @@ -47,28 +70,7 @@ npm i @jthinking/hashes-node
- Windows x64
- Windows arm64
- macOS x64
- macOS aarch64
- macOS arm64
- Linux x64
- Linux aarch64
- Linux arm64

## Usage

```js
// import hashes-node
import { md5, sha256, sm3 } = require('@jthinking/hashes-node')

// convert data to Buffer
const buf = Buffer.from('123', 'UTF-8')

// md5 hex
const md5Hex = md5(buf)

// sha256 hex
const sha256Hex = sha256(buf)

// sm3 hex
const sm3Hex = sm3(buf)

// other algorithms
// ...
```
48 changes: 24 additions & 24 deletions __test__/index.spec.mjs
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import {
md2,
md4,
md5,
sha1,
sha256,
sha512,
sha2_256,
sha2_512,
sha3_256,
sha3_512,
sm3,
belt_hash,
blake2s256,
blake2b512,
fsb512,
blake2s256,
blake3,
fsb256,
fsb512,
gost94,
groestl512,
groestl256,
groestl512,
md2,
md4,
md5,
ripemd128,
ripemd160,
ripemd256,
ripemd320,
sha1,
sha256,
sha2_256,
sha2_512,
sha3_256,
sha3_512,
sha512,
shabal192,
shabal224,
shabal256,
shabal384,
shabal512,
sm3,
streebog256,
streebog512,
tiger,
Expand Down Expand Up @@ -108,7 +109,6 @@ test("belt_hash from native", (t) => {
);
});


test("blake2s256 from native", (t) => {
t.is(
blake2s256(Buffer.from("123")),
Expand All @@ -123,6 +123,13 @@ test("blake2b512 from native", (t) => {
);
});

test("blake3 from native", (t) => {
t.is(
blake3(Buffer.from("123")),
"b3d4f8803f7e24b8f389b072e75477cdbcfbe074080fb5e500e53e26e054158e"
);
});

test("fsb512 from native", (t) => {
t.is(
fsb512(Buffer.from("123")),
Expand All @@ -137,23 +144,20 @@ test("fsb256 from native", (t) => {
);
});


test("gost94 from native", (t) => {
t.is(
gost94(Buffer.from("123")),
"5ef18489617ba2d8d2d7e0da389aaa4ff022ad01a39512a4fea1a8c45e439148"
);
});


test("groestl512 from native", (t) => {
t.is(
groestl512(Buffer.from("123")),
"659c4a2cfaa26c49bcd2c6d45b2749e74aac80b6a51d110d31845eacbaf6c2e7ae4cc1a164a9ac825079039f5ef00f97d2add48a31b0c24c307bced3c4a36ec6"
);
});


test("groestl256 from native", (t) => {
t.is(
groestl256(Buffer.from("123")),
Expand All @@ -162,13 +166,9 @@ test("groestl256 from native", (t) => {
});

test("ripemd128 from native", (t) => {
t.is(
ripemd128(Buffer.from("123")),
"781f357c35df1fef3138f6d29670365a"
);
t.is(ripemd128(Buffer.from("123")), "781f357c35df1fef3138f6d29670365a");
});


test("ripemd160 from native", (t) => {
t.is(
ripemd160(Buffer.from("123")),
Expand Down Expand Up @@ -258,4 +258,4 @@ test("whirlpool from native", (t) => {
whirlpool(Buffer.from("123")),
"344907e89b981caf221d05f597eb57a6af408f15f4dd7895bbd1b96a2938ec24a7dcf23acb94ece0b6d7b0640358bc56bdb448194b9305311aff038a834a079f"
);
});
});
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function sm3(buf: Buffer): string
export function belt_hash(buf: Buffer): string
export function blake2s256(buf: Buffer): string
export function blake2b512(buf: Buffer): string
export function blake3(buf: Buffer): string
export function fsb512(buf: Buffer): string
export function fsb256(buf: Buffer): string
export function gost94(buf: Buffer): string
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { md2, md4, md5, sha1, sha256, sha512, sha2_256, sha2_512, sha3_256, sha3_512, sm3, belt_hash, blake2s256, blake2b512, fsb512, fsb256, gost94, groestl256, groestl512, ripemd128, ripemd160, ripemd256, ripemd320, shabal192, shabal224, shabal256, shabal384, shabal512, streebog256, streebog512, tiger, tiger2, whirlpool } = nativeBinding
const { md2, md4, md5, sha1, sha256, sha512, sha2_256, sha2_512, sha3_256, sha3_512, sm3, belt_hash, blake2s256, blake2b512, blake3, fsb512, fsb256, gost94, groestl256, groestl512, ripemd128, ripemd160, ripemd256, ripemd320, shabal192, shabal224, shabal256, shabal384, shabal512, streebog256, streebog512, tiger, tiger2, whirlpool } = nativeBinding

module.exports.md2 = md2
module.exports.md4 = md4
Expand All @@ -262,6 +262,7 @@ module.exports.sm3 = sm3
module.exports.belt_hash = belt_hash
module.exports.blake2s256 = blake2s256
module.exports.blake2b512 = blake2b512
module.exports.blake3 = blake3
module.exports.fsb512 = fsb512
module.exports.fsb256 = fsb256
module.exports.gost94 = gost94
Expand Down
18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,23 @@
"repository": "https://github.com/jthinking/hashes-node.git",
"description": "Hashes utils for node.js based on napi-rs.",
"keywords": [
"belt-hash",
"blake2",
"blake3",
"fsb",
"gost94",
"groestl",
"md2",
"md4",
"md5",
"ripemd",
"sha1",
"sha256",
"sha512",
"sha3_256",
"sm3"
"sha2",
"sha3",
"shabal",
"sm3",
"streebog",
"tiger",
"whirlpool"
]
}
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,21 @@ pub fn blake2b512(buf: Buffer) -> String {
format!("{:x}", result)
}

#[napi]
pub fn blake3(buf: Buffer) -> String {
// Hash an input incrementally.
let mut hasher = blake3::Hasher::new();

// write input message
hasher.update(&buf);

// read hash digest and consume hasher
let result = hasher.finalize();

// convert to hex string
result.to_hex().to_ascii_lowercase()
}

#[napi]
pub fn fsb512(buf: Buffer) -> String {
use fsb::{Fsb512, Digest};
Expand Down

0 comments on commit 592b0c6

Please sign in to comment.