Skip to content

Commit

Permalink
feat: add key derivation
Browse files Browse the repository at this point in the history
Fixes #9, fixes #2
  • Loading branch information
connor4312 committed Jan 29, 2020
1 parent 5a707e6 commit 3bcfd0d
Show file tree
Hide file tree
Showing 18 changed files with 565 additions and 38 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 2.1.0 - 2019-01-23

- add keyed hash and key derivation support (fixes [#2](https://github.com/connor4312/blake3/issues/2), [#9](https://github.com/connor4312/blake3/issues/9))
- fixed a bug in hex encoding in the browser

## 2.0.1 - 2019-01-23

- fix browser bundle to use pure es modules (fixes [#8](https://github.com/connor4312/blake3/issues/8))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"prepack": "make clean && make MODE=release && npm test && rimraf dist/native.node",
"test": "mocha --require source-map-support/register --recursive \"dist/**/*.test.js\" --timeout 5000",
"fmt": "make fmt",
"compile": "tsc",
"compile": "tsc && tsc -p tsconfig.esm.json",
"watch": "tsc --watch"
},
"repository": {
Expand Down
56 changes: 52 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
- [API](#api)
- [Node.js](#nodejs)
- [`hash(data: BinaryLike, options?: { length: number }): Buffer`](#hashdata-binarylike-options--length-number--buffer)
- [`keyedHash(key: Buffer, data: BinaryLike, options?: { length: number }): Buffer`](#keyedhashkey-buffer-data-binarylike-options--length-number--buffer)
- [`deriveKey(context: string, material: BinaryLike, options?: { length: number }): Buffer`](#derivekeycontext-string-material-binarylike-options--length-number--buffer)
- [`createHash(): Hasher`](#createhash-hasher)
- [`createKeyed(key: Buffer): Hasher`](#createkeyedkey-buffer-hasher)
- [`createDeriveKey(key: Buffer): Hasher`](#createderivekeykey-buffer-hasher)
- [`hasher.update(data: BinaryLike): this`](#hasherupdatedata-binarylike-this)
- [`hasher.digest(encoding?: string, options?: { length: number, dispose: boolean })): Buffer | string`](#hasherdigestencoding-string-options--length-number-dispose-boolean--buffer--string)
- [`hasher.reader(options?: { dispose: boolean }): HashReader`](#hasherreaderoptions--dispose-boolean--hashreader)
Expand All @@ -20,15 +24,19 @@
- [`reader.readInto(target: Buffer): void`](#readerreadintotarget-buffer-void)
- [`reader.read(bytes: number): Buffer`](#readerreadbytes-number-buffer)
- [`reader.toString([encoding]): string`](#readertostringencoding-string)
- [`reade.toBuffer(): Buffer`](#readetobuffer-buffer)
- [`reader.toBuffer(): Buffer`](#readertobuffer-buffer)
- [`reader.dispose()`](#readerdispose)
- [`using(disposable: IDisposable, fn: disposable => T): T`](#usingdisposable-idisposable-fn-disposable--t-t)
- [Browser](#browser)
- [`hash(data: BinaryLike, options?: { length: number }): Hash`](#hashdata-binarylike-options--length-number--hash)
- [`keyedHash(key: Buffer, data: BinaryLike, options?: { length: number }): Hash`](#keyedhashkey-buffer-data-binarylike-options--length-number--hash)
- [`deriveKey(context: string, material: BinaryLike, options?: { length: number }): Hash`](#derivekeycontext-string-material-binarylike-options--length-number--hash)
- [`Hash`](#hash)
- [`hash.equals(other: Uint8Array)`](#hashequalsother-uint8array)
- [`hash.toString(encoding: 'hex' | 'base64' | 'utf8'): string`](#hashtostringencoding-hex--base64--utf8-string)
- [`createHash(): Hasher`](#createhash-hasher-1)
- [`createKeyed(key: Buffer): Hasher`](#createkeyedkey-buffer-hasher-1)
- [`createDeriveKey(key: Buffer): Hasher`](#createderivekeykey-buffer-hasher-1)
- [`hasher.update(data: BinaryLike): this`](#hasherupdatedata-binarylike-this-1)
- [`hasher.digest(encoding?: 'hex' | 'base64' | 'utf8', options?: { length: number, dispose: boolean })): Hash | string`](#hasherdigestencoding-hex--base64--utf8-options--length-number-dispose-boolean--hash--string)
- [`hasher.reader(options?: { dispose: boolean }): HashReader`](#hasherreaderoptions--dispose-boolean--hashreader-1)
Expand Down Expand Up @@ -98,6 +106,18 @@ The Node API can be imported via `require('blake3')`.

Returns a hash for the given data. The data can be a string, buffer, typedarray, array buffer, or array. By default, it creates a hash with the first 32 bytes of data, but this is configurable. It returns a Buffer.

#### `keyedHash(key: Buffer, data: BinaryLike, options?: { length: number }): Buffer`

Returns keyed a hash for the given data. The key must be exactly 32 bytes. The data can be a string, buffer, typedarray, array buffer, or array. By default, it creates a hash with the first 32 bytes of data, but this is configurable. It returns a Buffer.

For more information, see [the blake3 docs](https://docs.rs/blake3/0.1.3/blake3/fn.keyed_hash.html).

#### `deriveKey(context: string, material: BinaryLike, options?: { length: number }): Buffer`

The key derivation function. The data can be a string, buffer, typedarray, array buffer, or array. By default, it creates a hash with the first 32 bytes of data, but this is configurable. It returns a Buffer.

For more information, see [the blake3 docs](https://docs.rs/blake3/0.1.3/blake3/fn.derive_key.html).

#### `createHash(): Hasher`

Creates a new hasher instance. In Node.js, this is also a transform stream.
Expand All @@ -108,6 +128,14 @@ createReadStream('file.txt')
.on('data', hash => console.log(hash.toString('hex')));
```

#### `createKeyed(key: Buffer): Hasher`

Creates a new hasher instance for a keyed hash. For more information, see [the blake3 docs](https://docs.rs/blake3/0.1.3/blake3/fn.keyed_hash.html).

#### `createDeriveKey(key: Buffer): Hasher`

Creates a new hasher instance for the key derivation function. For more information, see [the blake3 docs](https://docs.rs/blake3/0.1.3/blake3/fn.derive_key.html).

##### `hasher.update(data: BinaryLike): this`

Adds data to a hash. The data can be a string, buffer, typedarray, array buffer, or array. This will throw if called after `digest()` or `dispose()`.
Expand Down Expand Up @@ -152,7 +180,7 @@ Reads and returns the given number of bytes from the reader, and advances the po

Converts first 32 bytes of the hash to a string with the given encoding. Defaults to hex encoding.

##### `reade.toBuffer(): Buffer`
##### `reader.toBuffer(): Buffer`

Converts first 32 bytes of the hash to a Buffer.

Expand Down Expand Up @@ -182,7 +210,19 @@ The browser API can be imported via `import('blake3/browser')`.

#### `hash(data: BinaryLike, options?: { length: number }): Hash`

Returns a hash for the given data. The data can be a string, buffer, typedarray, array buffer, or array. By default, it creates a hash with the first 32 bytes of data, but this is configurable. It returns a [Hash](#Hash) instance.
Returns a hash for the given data. The data can be a string, typedarray, array buffer, or array. By default, it creates a hash with the first 32 bytes of data, but this is configurable. It returns a [Hash](#Hash) instance.

#### `keyedHash(key: Buffer, data: BinaryLike, options?: { length: number }): Hash`

Returns keyed a hash for the given data. The key must be exactly 32 bytes. The data can be a string, typedarray, array buffer, or array. By default, it creates a hash with the first 32 bytes of data, but this is configurable. It returns a [Hash](#Hash) instance.

For more information, see [the blake3 docs](https://docs.rs/blake3/0.1.3/blake3/fn.keyed_hash.html).

#### `deriveKey(context: string, material: BinaryLike, options?: { length: number }): Hash`

The key derivation function. The data can be a string, typedarray, array buffer, or array. By default, it creates a hash with the first 32 bytes of data, but this is configurable. It returns a [Hash](#Hash) instance.

For more information, see [the blake3 docs](https://docs.rs/blake3/0.1.3/blake3/fn.derive_key.html).

#### `Hash`

Expand All @@ -198,7 +238,15 @@ Converts the hash to a string with the given encoding.

#### `createHash(): Hasher`

Creates a new hasher instance:
Creates a new hasher instance.

#### `createKeyed(key: Buffer): Hasher`

Creates a new hasher instance for a keyed hash. For more information, see [the blake3 docs](https://docs.rs/blake3/0.1.3/blake3/fn.keyed_hash.html).

#### `createDeriveKey(key: Buffer): Hasher`

Creates a new hasher instance for the key derivation function. For more information, see [the blake3 docs](https://docs.rs/blake3/0.1.3/blake3/fn.derive_key.html).

##### `hasher.update(data: BinaryLike): this`

Expand Down
26 changes: 24 additions & 2 deletions rs/native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,31 @@ pub struct Blake3Hash {

declare_types! {
pub class JsHash for Blake3Hash {
init(_) {
// Constructing is awkward in neon, so this is how this works:
// 0 args = new regular hash
// 1 args = use the first arg (a Buffer) as the key
// 2 args = use the second arg (a String) to derive a key
init(mut cx) {
let hasher = match cx.len() {
0 => blake3::Hasher::new(),
1 => {
let key_buffer = cx.argument::<JsBuffer>(0)?;
let key_bytes = cx.borrow(&key_buffer, |data| {
let mut key = [0; 32];
key.copy_from_slice(data.as_slice::<u8>());
key
});
blake3::Hasher::new_keyed(&key_bytes)
},
2 => {
let context_data = cx.argument::<JsString>(1)?;
blake3::Hasher::new_derive_key(&context_data.value())
},
_ => panic!("unexpected number of arguments"),
};

Ok(Blake3Hash {
hasher: blake3::Hasher::new(),
hasher: hasher,
})
}

Expand Down
9 changes: 2 additions & 7 deletions rs/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ pub fn hash(data: &[u8], out: &mut [u8]) {
reader.fill(out);
}

#[wasm_bindgen]
pub fn derive_key(context: String, key_material: &[u8], out: &mut [u8]) {
blake3::derive_key(&context, key_material, out)
}

#[wasm_bindgen]
pub fn create_hasher() -> Blake3Hash {
Blake3Hash {
Expand All @@ -31,9 +26,9 @@ pub fn create_keyed(key_slice: &[u8]) -> Blake3Hash {
}

#[wasm_bindgen]
pub fn create_derived(key: String) -> Blake3Hash {
pub fn create_derive(context: String) -> Blake3Hash {
Blake3Hash {
hasher: blake3::Hasher::new_derive_key(&key),
hasher: blake3::Hasher::new_derive_key(&context),
}
}

Expand Down
Loading

0 comments on commit 3bcfd0d

Please sign in to comment.