Update hashing functions to ref (#68)

* Package upgrades

* Use h64 by default

* Remove AsNumber distinction

* xxhash32/64 & blake2b/2s

* Add xxhashAsHex{128,256}

* Add blake2AsHex{128,256,512}

* Update documentation

* Update docs
This commit is contained in:
Jaco Greeff
2018-01-30 11:55:32 +01:00
committed by GitHub
parent 725a4571ed
commit 523dc1c48d
55 changed files with 1040 additions and 135 deletions
+1 -1
View File
@@ -10,7 +10,7 @@
"test": "jest --coverage"
},
"devDependencies": {
"@polkadot/dev": "^0.15.1",
"@polkadot/dev": "^0.15.2",
"lerna": "^2.5.1"
}
}
+5 -1
View File
@@ -2,10 +2,14 @@
Utility methods for this package are split into groups
- [blake2](blake2.md) Create blake2b values with specified bitlengths
- [blake2b](blake2b.md) Create [Blake2b](https://blake2.net/) values as hex, Buffer & Uint8Array output
- [blake2s](blake2s.md) Create [Blake2s](https://blake2.net/) values as hex, Buffer & Uint8Array output
- [keccak](keccak.md) Create Keccak256 values as hex, string & buffer output
- [nacl](nacl.md) Implements [NaCl](http://nacl.cr.yp.to/) secret-key authenticated encryption, public-key authenticated encryption, hashing, and public-key signatures
- [random](random.md) Returns a sequence of secure random bytes in a variety of formats
- [sha512](sha512.md) Implements Sha-512 hashing functions for a variety of input and outputs
- [xxhash](xxhash.md) Create [XXHash](http://cyan4973.github.io/xxHash/) values as BN, hex & number output
- [xxhash](xxhash.md) Create xxhash64 values with specified bitlengths
- [xxhash32](xxhash32.md) Create [XXHash](http://cyan4973.github.io/xxHash/) values as BN, hex & number output
- [xxhash64](xxhash64.md) Create [XXHash](http://cyan4973.github.io/xxHash/) values as BN, hex & number output
+76
View File
@@ -0,0 +1,76 @@
# blake2
Create blake2b values with specified bitlengths
- [blake2AsHex](#blake2ashex) Creates a blake2b hex from the input.
- [blake2AsHex128](#blake2ashex128) Creates a blake2b hex with 128-bits from the input.
- [blake2AsHex256](#blake2ashex256) Creates a blake2b hex with 256-bits from the input.
- [blake2AsHex512](#blake2ashex512) Creates a blake2b hex with 512-bits from the input.
## blake2AsHex
Creates a blake2b hex from the input.
```js
blake2AsHex (data: Uint8Array, bitLenght: number = 64): string
```
From a `Uint8Array` input, create the blake2b and return the result as a hex string with the specified `bitLength`.
```js
import { blake2AsHex } from '@polkadot/util-crypto';
blake2AsHex('abc')) // => 0xba80a53f981c4d0d
```
## blake2AsHex128
Creates a blake2b hex with 128-bits from the input.
```js
blake2AsHex128 (data: Buffer): string
```
From a `Uint8Array` input, create the blake2b and return the result as a hex string with 128 bits.
```js
import { blake2AsHex128 } from '@polkadot/util-crypto';
blake2AsHex128('abc')) // => 0x44bc2cf5ad770999
```
## blake2AsHex256
Creates a blake2b hex with 256-bits from the input.
```js
blake2AsHex256 (data: Buffer): string
```
From a `Uint8Array` input, create the blake2b and return the result as a hex string with 256 bits.
```js
import { blake2AsHex256 } from '@polkadot/util-crypto';
blake2AsHex256('abc')) // => 0x44bc2cf5ad770999
```
## blake2AsHex512
Creates a blake2b hex with 512-bits from the input.
```js
blake2AsHex512 (data: Buffer): string
```
From a `Uint8Array` input, create the blake2b and return the result as a hex string with 512 bits.
```js
import { blake2AsHex512 } from '@polkadot/util-crypto';
blake2AsHex512('abc')) // => 0x44bc2cf5ad770999
```
+58
View File
@@ -0,0 +1,58 @@
# blake2b
Create [Blake2b](https://blake2.net/) values as hex, Buffer & Uint8Array output
- [blake2bAsBuffer](#blake2basbuffer) Creates a blake2b Buffer from the input.
- [blake2bAsHex](#blake2bashex) Creates a blake2b hex string from the input.
- [blake2bAsU8a](#blake2basu8a) Creates a blake2b Uint8Array from the input.
## blake2bAsBuffer
Creates a blake2b Buffer from the input.
```js
blake2bAsBuffer (value: Uint8Array): Buffer
```
From a `Uint8Array` input, create the blake2b and return the result as a `Buffer`.
```js
import { blake2bAsBuffer } from '@polkadot/util-crypto';
blake2bAsBuffer('abc') // => Buffer.from('ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923', 'hex')
```
## blake2bAsHex
Creates a blake2b hex string from the input.
```js
blake2bAsHex (value: Uint8Array): string
```
From a `Uint8Array` input, create the blake2b and return the result as a hex string.
```js
import { blake2bAsHex } from '@polkadot/util-crypto';
blake2bAsHex('abc') // => '0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923'
```
## blake2bAsU8a
Creates a blake2b Uint8Array from the input.
```js
blake2bAsU8a (value: Uint8Array): Uint8Array
```
From a `Uint8Array` input, create the blake2b and return the result as a `Uint8Array`.
```js
import { blake2bAsU8a } from '@polkadot/util-crypto';
blake2bAsU8a('abc') // => Uint8Array('508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982')
```
+31 -31
View File
@@ -1,58 +1,58 @@
# xxhash
Create [XXHash](http://cyan4973.github.io/xxHash/) values as BN, hex & number output
Create xxhash64 values with specified bitlengths
- [xxhashAsBn](#xxhashasbn) Creates a xxhash BN from the input.
- [xxhashAsHex](#xxhashashex) Creates a xxhash hex from the input.
- [xxhashAsNumber](#xxhashasnumber) Creates a xxhash number from the input.
## xxhashAsBn
Creates a xxhash BN from the input.
```js
xxhashAsBn (data: Buffer | Uint8Array | string, seed: number): BN
```
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a BN.
```js
import { xxhashAsNumber } from '@polkadot/util-crypto';
xxhashAsBn('abcd', 0xabcd)) // => new BN(0xcda8fae4)
```
- [xxhashAsHex](#xxhashashex) Creates a xxhash64 hex from the input.
- [xxhashAsHex128](#xxhashashex128) Creates a xxhash64 hex with 128-bits from the input.
- [xxhashAsHex256](#xxhashashex256) Creates a xxhash64 hex with 256-bits from the input.
## xxhashAsHex
Creates a xxhash hex from the input.
Creates a xxhash64 hex from the input.
```js
xxhashAsHex (data: Buffer | Uint8Array | string, seed: number): string
xxhashAsHex (data: Buffer | Uint8Array | string, bitLenght: number = 64): string
```
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a hex string.
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash64 and return the result as a hex string with the specified `bitLength`.
```js
import { xxhashAsHex } from '@polkadot/util-crypto';
xxhashAsHex('abcd', 0xabcd)) // => 0xcda8fae4
xxhashAsHex('abc')) // => 0x44bc2cf5ad770999
```
## xxhashAsNumber
## xxhashAsHex128
Creates a xxhash number from the input.
Creates a xxhash64 hex with 128-bits from the input.
```js
xxhashAsNumber (data: Buffer | Uint8Array | string, seed: number): number
xxhashAsHex128 (data: Buffer | Uint8Array | string): string
```
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a number.
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash64 and return the result as a hex string with 128 bits.
```js
import { xxhashAsNumber } from '@polkadot/util-crypto';
import { xxhashAsHex128 } from '@polkadot/util-crypto';
xxhashAsNumber('abcd', 0xabcd)) // => 0xcda8fae4
xxhashAsHex128('abc')) // => 0x44bc2cf5ad770999
```
## xxhashAsHex256
Creates a xxhash64 hex with 256-bits from the input.
```js
xxhashAsHex256 (data: Buffer | Uint8Array | string): string
```
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash64 and return the result as a hex string with 256 bits.
```js
import { xxhashAsHex256 } from '@polkadot/util-crypto';
xxhashAsHex256('abc')) // => 0x44bc2cf5ad770999bea9ca8199328908
```
+58
View File
@@ -0,0 +1,58 @@
# xxhash32
Create [XXHash](http://cyan4973.github.io/xxHash/) values as BN, hex & number output
- [xxhash32AsBn](#xxhash32asbn) Creates a xxhash BN from the input.
- [xxhash32AsHex](#xxhash32ashex) Creates a xxhash hex from the input.
- [xxhash32AsRaw](#xxhash32asraw) Creates a xxhash non-prefixed hex from the input.
## xxhash32AsBn
Creates a xxhash BN from the input.
```js
xxhash32AsBn (data: Buffer | Uint8Array | string, seed: number): BN
```
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a BN.
```js
import { xxhash32AsNumber } from '@polkadot/util-crypto';
xxhash32AsBn('abcd', 0xabcd)) // => new BN(0xcda8fae4)
```
## xxhash32AsHex
Creates a xxhash hex from the input.
```js
xxhash32AsHex (data: Buffer | Uint8Array | string, seed: number): string
```
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a hex string.
```js
import { xxhash32AsHex } from '@polkadot/util-crypto';
xxhash32AsHex('abcd', 0xabcd)) // => 0xcda8fae4
```
## xxhash32AsRaw
Creates a xxhash non-prefixed hex from the input.
```js
xxhash32AsRaw (data: Buffer | Uint8Array | string, seed: number): string
```
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a non-prefixed hex string.
```js
import { xxhash32AsRaw } from '@polkadot/util-crypto';
xxhash32AsRaw('abcd', 0xabcd)) // => cda8fae4
```
+58
View File
@@ -0,0 +1,58 @@
# xxhash64
Create [XXHash](http://cyan4973.github.io/xxHash/) values as BN, hex & number output
- [xxhash64AsBn](#xxhash64asbn) Creates a xxhash BN from the input.
- [xxhash64AsHex](#xxhash64ashex) Creates a xxhash hex from the input.
- [xxhash64AsRaw](#xxhash64asraw) Creates a xxhash non-prefixed hex from the input.
## xxhash64AsBn
Creates a xxhash BN from the input.
```js
xxhash64AsBn (data: Buffer | Uint8Array | string, seed: number): BN
```
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a BN.
```js
import { xxhash64AsNumber } from '@polkadot/util-crypto';
xxhash64AsBn('abcd', 0xabcd)) // => new BN(0xe29f70f8b8c96df7)
```
## xxhash64AsHex
Creates a xxhash hex from the input.
```js
xxhash64AsHex (data: Buffer | Uint8Array | string, seed: number): string
```
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a hex string.
```js
import { xxhash64AsHex } from '@polkadot/util-crypto';
xxhash64AsHex('abcd', 0xabcd)) // => 0xe29f70f8b8c96df7
```
## xxhash64AsRaw
Creates a xxhash non-prefixed hex from the input.
```js
xxhash64AsRaw (data: Buffer | Uint8Array | string, seed: number): string
```
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a non-prefixed hex string.
```js
import { xxhash64AsRaw } from '@polkadot/util-crypto';
xxhash64AsRaw('abcd', 0xabcd)) // => e29f70f8b8c96df7
```
+29
View File
@@ -0,0 +1,29 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const hexAddPrefix = require('@polkadot/util/hex/addPrefix');
const blake2bAsBuffer = require('../blake2b/asBuffer');
/**
@name blake2AsHex
@signature blake2AsHex (data: Uint8Array, bitLenght: number = 64): string
@summary Creates a blake2b hex from the input.
@description
From a `Uint8Array` input, create the blake2b and return the result as a hex string with the specified `bitLength`.
@example
import { blake2AsHex } from '@polkadot/util-crypto';
blake2AsHex('abc')) // => 0xba80a53f981c4d0d
*/
module.exports = function blake2AsHex (data: Uint8Array, bitLength: number = 64): string {
const byteLength = Math.ceil(bitLength / 8);
return hexAddPrefix(
blake2bAsBuffer(data)
.slice(0, byteLength)
.toString('hex')
);
};
@@ -0,0 +1,31 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const { blake2AsHex } = require('./index');
describe('blake2AsHex', () => {
it('returns a 64-bit value by default', () => {
expect(
blake2AsHex('abc')
).toEqual('0xba80a53f981c4d0d');
});
it('returns a 128-bit value (as specified)', () => {
expect(
blake2AsHex('abc', 128)
).toEqual('0xba80a53f981c4d0d6a2797b69f12f6e9');
});
it('returns a 256-bit value (as specified)', () => {
expect(
blake2AsHex('abc', 256)
).toEqual('0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1');
});
it('returns a 512-bit value (as specified)', () => {
expect(
blake2AsHex('abc', 512)
).toEqual('0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923');
});
});
@@ -0,0 +1,21 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const blake2AsHex = require('./asHex');
/**
@name blake2AsHex128
@signature blake2AsHex128 (data: Buffer): string
@summary Creates a blake2b hex with 128-bits from the input.
@description
From a `Uint8Array` input, create the blake2b and return the result as a hex string with 128 bits.
@example
import { blake2AsHex128 } from '@polkadot/util-crypto';
blake2AsHex128('abc')) // => 0x44bc2cf5ad770999
*/
module.exports = function blake2AsHex128 (data: Uint8Array): string {
return blake2AsHex(data, 128);
};
@@ -0,0 +1,13 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const { blake2AsHex128 } = require('./index');
describe('blake2AsHex128', () => {
it('returns a 128-bit value (as specified)', () => {
expect(
blake2AsHex128('abc')
).toEqual('0xba80a53f981c4d0d6a2797b69f12f6e9');
});
});
@@ -0,0 +1,21 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const blake2AsHex = require('./asHex');
/**
@name blake2AsHex256
@signature blake2AsHex256 (data: Buffer): string
@summary Creates a blake2b hex with 256-bits from the input.
@description
From a `Uint8Array` input, create the blake2b and return the result as a hex string with 256 bits.
@example
import { blake2AsHex256 } from '@polkadot/util-crypto';
blake2AsHex256('abc')) // => 0x44bc2cf5ad770999
*/
module.exports = function blake2AsHex256 (data: Uint8Array): string {
return blake2AsHex(data, 256);
};
@@ -0,0 +1,13 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const { blake2AsHex256 } = require('./index');
describe('blake2AsHex256', () => {
it('returns a 256-bit value (as specified)', () => {
expect(
blake2AsHex256('abc')
).toEqual('0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1');
});
});
@@ -0,0 +1,21 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const blake2AsHex = require('./asHex');
/**
@name blake2AsHex512
@signature blake2AsHex512 (data: Buffer): string
@summary Creates a blake2b hex with 512-bits from the input.
@description
From a `Uint8Array` input, create the blake2b and return the result as a hex string with 512 bits.
@example
import { blake2AsHex512 } from '@polkadot/util-crypto';
blake2AsHex512('abc')) // => 0x44bc2cf5ad770999
*/
module.exports = function blake2AsHex512 (data: Uint8Array): string {
return blake2AsHex(data, 512);
};
@@ -0,0 +1,13 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const { blake2AsHex512 } = require('./index');
describe('blake2AsHex512', () => {
it('returns a 512-bit value (as specified)', () => {
expect(
blake2AsHex512('abc')
).toEqual('0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923');
});
});
+19
View File
@@ -0,0 +1,19 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const blake2AsHex = require('./asHex');
const blake2AsHex128 = require('./asHex128');
const blake2AsHex256 = require('./asHex256');
const blake2AsHex512 = require('./asHex512');
/**
@summary Create blake2b values with specified bitlengths
*/
module.exports = {
blake2AsHex,
blake2AsHex128,
blake2AsHex256,
blake2AsHex512
};
@@ -0,0 +1,24 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const u8aToBuffer = require('@polkadot/util/u8a/toBuffer');
const blake2bAsU8a = require('./asU8a');
/**
@name blake2bAsBuffer
@signature blake2bAsBuffer (value: Uint8Array): Buffer
@summary Creates a blake2b Buffer from the input.
@description
From a `Uint8Array` input, create the blake2b and return the result as a `Buffer`.
@example
import { blake2bAsBuffer } from '@polkadot/util-crypto';
blake2bAsBuffer('abc') // => Buffer.from('ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923', 'hex')
*/
module.exports = function blake2bAsBuffer (data: Uint8Array): Buffer {
return u8aToBuffer(
blake2bAsU8a(data)
);
};
@@ -0,0 +1,15 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const { blake2bAsBuffer } = require('./index');
describe('blake2bAsBuffer', () => {
it('creates the correct hash', () => {
expect(
blake2bAsBuffer('abc')
).toEqual(
Buffer.from('ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923', 'hex')
);
});
});
+24
View File
@@ -0,0 +1,24 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const bufferToHex = require('@polkadot/util/buffer/toHex');
const blake2bAsBuffer = require('./asBuffer');
/**
@name blake2bAsHex
@signature blake2bAsHex (value: Uint8Array): string
@summary Creates a blake2b hex string from the input.
@description
From a `Uint8Array` input, create the blake2b and return the result as a hex string.
@example
import { blake2bAsHex } from '@polkadot/util-crypto';
blake2bAsHex('abc') // => '0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923'
*/
module.exports = function blake2bAsHex (data: Uint8Array): string {
return bufferToHex(
blake2bAsBuffer(data)
);
};
@@ -0,0 +1,13 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const { blake2bAsHex } = require('./index');
describe('blake2bAsHex', () => {
it('creates the correct hash', () => {
expect(
blake2bAsHex('abc')
).toEqual('0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923');
});
});
+22
View File
@@ -0,0 +1,22 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
// TODO: For Node we can also use node-blake2 (npm blake2)
const blakejs = require('blakejs');
/**
@name blake2bAsU8a
@signature blake2bAsU8a (value: Uint8Array): Uint8Array
@summary Creates a blake2b Uint8Array from the input.
@description
From a `Uint8Array` input, create the blake2b and return the result as a `Uint8Array`.
@example
import { blake2bAsU8a } from '@polkadot/util-crypto';
blake2bAsU8a('abc') // => Uint8Array('508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982')
*/
module.exports = function blake2bAsU8a (data: Uint8Array): Uint8Array {
return blakejs.blake2b(data);
};
@@ -0,0 +1,24 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const { blake2bAsU8a } = require('./index');
describe('blake2bAsU8a', () => {
it('creates the correct hash', () => {
expect(
blake2bAsU8a('abc')
).toEqual(
Uint8Array.from([
0xba, 0x80, 0xa5, 0x3f, 0x98, 0x1c, 0x4d, 0x0d,
0x6a, 0x27, 0x97, 0xb6, 0x9f, 0x12, 0xf6, 0xe9,
0x4c, 0x21, 0x2f, 0x14, 0x68, 0x5a, 0xc4, 0xb7,
0x4b, 0x12, 0xbb, 0x6f, 0xdb, 0xff, 0xa2, 0xd1,
0x7d, 0x87, 0xc5, 0x39, 0x2a, 0xab, 0x79, 0x2d,
0xc2, 0x52, 0xd5, 0xde, 0x45, 0x33, 0xcc, 0x95,
0x18, 0xd3, 0x8a, 0xa8, 0xdb, 0xf1, 0x92, 0x5a,
0xb9, 0x23, 0x86, 0xed, 0xd4, 0x00, 0x99, 0x23
])
);
});
});
+17
View File
@@ -0,0 +1,17 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const blake2bAsBuffer = require('./asBuffer');
const blake2bAsHex = require('./asHex');
const blake2bAsU8a = require('./asU8a');
/**
@summary Create [Blake2b](https://blake2.net/) values as hex, Buffer & Uint8Array output
*/
module.exports = {
blake2bAsBuffer,
blake2bAsHex,
blake2bAsU8a
};
@@ -10,7 +10,10 @@ describe('blake2sAsU8a', () => {
blake2sAsU8a('abc')
).toEqual(
Uint8Array.from([
0x50, 0x8c, 0x5e, 0x8c, 0x32, 0x7c, 0x14, 0xe2, 0xe1, 0xa7, 0x2b, 0xa3, 0x4e, 0xeb, 0x45, 0x2f, 0x37, 0x45, 0x8b, 0x20, 0x9e, 0xd6, 0x3a, 0x29, 0x4d, 0x99, 0x9b, 0x4c, 0x86, 0x67, 0x59, 0x82
0x50, 0x8c, 0x5e, 0x8c, 0x32, 0x7c, 0x14, 0xe2,
0xe1, 0xa7, 0x2b, 0xa3, 0x4e, 0xeb, 0x45, 0x2f,
0x37, 0x45, 0x8b, 0x20, 0x9e, 0xd6, 0x3a, 0x29,
0x4d, 0x99, 0x9b, 0x4c, 0x86, 0x67, 0x59, 0x82
])
);
});
+5 -1
View File
@@ -3,16 +3,20 @@
// of the ISC license. See the LICENSE file for details.
// @flow
const blake2 = require('./blake2');
const blake2s = require('./blake2s');
const blake2b = require('./blake2b');
const keccak = require('./keccak');
const nacl = require('./nacl');
const random = require('./random');
const sha512 = require('./sha512');
const xxhash = require('./xxhash');
const xxhash32 = require('./xxhash32');
const xxhash64 = require('./xxhash64');
/**
@summary Utility methods for this package are split into groups
*/
module.exports = Object.assign(
{}, blake2s, keccak, nacl, random, sha512, xxhash
{}, blake2, blake2b, blake2s, keccak, nacl, random, sha512, xxhash, xxhash32, xxhash64
);
-25
View File
@@ -1,25 +0,0 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const BN = require('bn.js');
const xxhashAsValue = require('./asValue');
/**
@name xxhashAsBn
@signature xxhashAsBn (data: Buffer | Uint8Array | string, seed: number): BN
@summary Creates a xxhash BN from the input.
@description
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a BN.
@example
import { xxhashAsNumber } from '@polkadot/util-crypto';
xxhashAsBn('abcd', 0xabcd)) // => new BN(0xcda8fae4)
*/
module.exports = function xxhashAsBn (data: Buffer | Uint8Array | string, seed: number): BN {
return new BN(
xxhashAsValue(data, seed).toNumber()
);
};
+15 -7
View File
@@ -5,21 +5,29 @@
const hexAddPrefix = require('@polkadot/util/hex/addPrefix');
const xxhashAsValue = require('./asValue');
const xxhash64AsRaw = require('../xxhash64/asRaw');
/**
@name xxhashAsHex
@signature xxhashAsHex (data: Buffer | Uint8Array | string, seed: number): string
@summary Creates a xxhash hex from the input.
@signature xxhashAsHex (data: Buffer | Uint8Array | string, bitLenght: number = 64): string
@summary Creates a xxhash64 hex from the input.
@description
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a hex string.
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash64 and return the result as a hex string with the specified `bitLength`.
@example
import { xxhashAsHex } from '@polkadot/util-crypto';
xxhashAsHex('abcd', 0xabcd)) // => 0xcda8fae4
xxhashAsHex('abc')) // => 0x44bc2cf5ad770999
*/
module.exports = function xxhashAsHex (data: Buffer | Uint8Array | string, seed: number): string {
module.exports = function xxhashAsHex (data: Buffer | Uint8Array | string, bitLength: number = 64): string {
const strLength = Math.ceil(bitLength / 4);
const iterations = Math.ceil(strLength / 8);
let result = xxhash64AsRaw(data, 0);
for (let seed = 1; seed < iterations; seed++) {
result += xxhash64AsRaw(data, seed);
}
return hexAddPrefix(
xxhashAsValue(data, seed).toString(16)
result.substr(0, strLength)
);
};
+15 -3
View File
@@ -5,9 +5,21 @@
const { xxhashAsHex } = require('./index');
describe('xxhashAsHex', () => {
it('creates the correct hex output', () => {
it('returns a 64-bit value by default', () => {
expect(
xxhashAsHex('abcd', 0xabcd)
).toEqual('0xcda8fae4');
xxhashAsHex('abc')
).toEqual('0x44bc2cf5ad770999');
});
it('returns a 128-bit value (as specified)', () => {
expect(
xxhashAsHex('abc', 128)
).toEqual('0x44bc2cf5ad770999bea9ca8199328908');
});
it('returns a 256-bit value (as specified)', () => {
expect(
xxhashAsHex('abc', 256)
).toEqual('0x44bc2cf5ad770999bea9ca819932890853a0b8b27057daf72bd60d36955db703');
});
});
@@ -0,0 +1,21 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const xxhashAsHex = require('./asHex');
/**
@name xxhashAsHex128
@signature xxhashAsHex128 (data: Buffer | Uint8Array | string): string
@summary Creates a xxhash64 hex with 128-bits from the input.
@description
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash64 and return the result as a hex string with 128 bits.
@example
import { xxhashAsHex128 } from '@polkadot/util-crypto';
xxhashAsHex128('abc')) // => 0x44bc2cf5ad770999
*/
module.exports = function xxhashAsHex128 (data: Buffer | Uint8Array | string): string {
return xxhashAsHex(data, 128);
};
@@ -0,0 +1,13 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const { xxhashAsHex128 } = require('./index');
describe('xxhashAsHex128', () => {
it('returns a 128-bit value (as specified)', () => {
expect(
xxhashAsHex128('abc')
).toEqual('0x44bc2cf5ad770999bea9ca8199328908');
});
});
@@ -0,0 +1,21 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const xxhashAsHex = require('./asHex');
/**
@name xxhashAsHex256
@signature xxhashAsHex256 (data: Buffer | Uint8Array | string): string
@summary Creates a xxhash64 hex with 256-bits from the input.
@description
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash64 and return the result as a hex string with 256 bits.
@example
import { xxhashAsHex256 } from '@polkadot/util-crypto';
xxhashAsHex256('abc')) // => 0x44bc2cf5ad770999bea9ca8199328908
*/
module.exports = function xxhashAsHex256 (data: Buffer | Uint8Array | string): string {
return xxhashAsHex(data, 256);
};
@@ -0,0 +1,13 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const { xxhashAsHex256 } = require('./index');
describe('xxhashAsHex256', () => {
it('returns a 256-bit value (as specified)', () => {
expect(
xxhashAsHex256('abc', 256)
).toEqual('0x44bc2cf5ad770999bea9ca819932890853a0b8b27057daf72bd60d36955db703');
});
});
@@ -1,21 +0,0 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const xxhashAsValue = require('./asValue');
/**
@name xxhashAsNumber
@signature xxhashAsNumber (data: Buffer | Uint8Array | string, seed: number): number
@summary Creates a xxhash number from the input.
@description
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a number.
@example
import { xxhashAsNumber } from '@polkadot/util-crypto';
xxhashAsNumber('abcd', 0xabcd)) // => 0xcda8fae4
*/
module.exports = function xxhashAsNumber (data: Buffer | Uint8Array | string, seed: number): number {
return xxhashAsValue(data, seed).toNumber();
};
@@ -1,30 +0,0 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const { xxhashAsNumber } = require('./index');
describe('xxhashAsNumber', () => {
const DATA = 'abcd';
const DATA_ARR = [0x61, 0x62, 0x63, 0x64];
const SEED = 0xabcd;
const RESULT = 0xCDA8FAE4;
it('creates the correct number output (string)', () => {
expect(
xxhashAsNumber(DATA, SEED)
).toEqual(RESULT);
});
it('creates the correct number output (Buffer)', () => {
expect(
xxhashAsNumber(Buffer.from(DATA_ARR), SEED)
).toEqual(RESULT);
});
it('creates the correct number output (Uint8Array)', () => {
expect(
xxhashAsNumber(Uint8Array.from(DATA_ARR), SEED)
).toEqual(RESULT);
});
});
+5 -5
View File
@@ -3,15 +3,15 @@
// of the ISC license. See the LICENSE file for details.
// @flow
const xxhashAsBn = require('./asBn');
const xxhashAsHex = require('./asHex');
const xxhashAsNumber = require('./asNumber');
const xxhashAsHex128 = require('./asHex128');
const xxhashAsHex256 = require('./asHex256');
/**
@summary Create [XXHash](http://cyan4973.github.io/xxHash/) values as BN, hex & number output
@summary Create xxhash64 values with specified bitlengths
*/
module.exports = {
xxhashAsBn,
xxhashAsHex,
xxhashAsNumber
xxhashAsHex128,
xxhashAsHex256
};
+26
View File
@@ -0,0 +1,26 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const BN = require('bn.js');
const xxhash32AsValue = require('./asValue');
/**
@name xxhash32AsBn
@signature xxhash32AsBn (data: Buffer | Uint8Array | string, seed: number): BN
@summary Creates a xxhash BN from the input.
@description
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a BN.
@example
import { xxhash32AsNumber } from '@polkadot/util-crypto';
xxhash32AsBn('abcd', 0xabcd)) // => new BN(0xcda8fae4)
*/
module.exports = function xxhash32AsBn (data: Buffer | Uint8Array | string, seed: number): BN {
return new BN(
xxhash32AsValue(data, seed).toString(16),
16
);
};
@@ -2,12 +2,12 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const { xxhashAsBn } = require('./index');
const { xxhash32AsBn } = require('./index');
describe('xxhashAsBn', () => {
describe('xxhash32AsBn', () => {
it('creates the correct BN output', () => {
expect(
xxhashAsBn('abcd', 0xabcd).toString(16)
xxhash32AsBn('abcd', 0xabcd).toString(16)
).toEqual('cda8fae4');
});
});
@@ -0,0 +1,25 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const hexAddPrefix = require('@polkadot/util/hex/addPrefix');
const xxhash32AsRaw = require('./asRaw');
/**
@name xxhash32AsHex
@signature xxhash32AsHex (data: Buffer | Uint8Array | string, seed: number): string
@summary Creates a xxhash hex from the input.
@description
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a hex string.
@example
import { xxhash32AsHex } from '@polkadot/util-crypto';
xxhash32AsHex('abcd', 0xabcd)) // => 0xcda8fae4
*/
module.exports = function xxhash32AsHex (data: Buffer | Uint8Array | string, seed: number): string {
return hexAddPrefix(
xxhash32AsRaw(data, seed)
);
};
@@ -0,0 +1,13 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const { xxhash32AsHex } = require('./index');
describe('xxhash32AsHex', () => {
it('creates the correct hex output', () => {
expect(
xxhash32AsHex('abcd', 0xabcd)
).toEqual('0xcda8fae4');
});
});
@@ -0,0 +1,21 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const xxhash32AsValue = require('./asValue');
/**
@name xxhash32AsRaw
@signature xxhash32AsRaw (data: Buffer | Uint8Array | string, seed: number): string
@summary Creates a xxhash non-prefixed hex from the input.
@description
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a non-prefixed hex string.
@example
import { xxhash32AsRaw } from '@polkadot/util-crypto';
xxhash32AsRaw('abcd', 0xabcd)) // => cda8fae4
*/
module.exports = function xxhash32AsRaw (data: Buffer | Uint8Array | string, seed: number): string {
return xxhash32AsValue(data, seed).toString(16);
};
@@ -0,0 +1,13 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const { xxhash32AsRaw } = require('./index');
describe('xxhash32AsRaw', () => {
it('creates the correct non-prefixed hex output', () => {
expect(
xxhash32AsRaw('abcd', 0xabcd)
).toEqual('cda8fae4');
});
});
@@ -9,7 +9,7 @@ const isBuffer = require('@polkadot/util/is/buffer');
const isString = require('@polkadot/util/is/string');
const xxhashjs = require('xxhashjs');
module.exports = function xxhashAsValue (data: Buffer | Uint8Array | string, seed: number): xxhashjs$Result {
module.exports = function xxhash32AsValue (data: Buffer | Uint8Array | string, seed: number): xxhashjs$Result {
if (isBuffer(data) || isString(data)) {
// $FlowFixMe we have determined the type
return xxhashjs.h32(data, seed);
@@ -0,0 +1,25 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const xxhash64AsValue = require('./asValue');
describe('xxhash64AsValue', () => {
it('creates the correct output (string input)', () => {
expect(
xxhash64AsValue('abcd', 0xabcd).toString(16)
).toEqual('cda8fae4');
});
it('creates the correct output (Buffer input)', () => {
expect(
xxhash64AsValue(Buffer.from([0x61, 0x62, 0x63, 0x64]), 0xabcd).toString(16)
).toEqual('cda8fae4');
});
it('creates the correct output (Uint8Array input)', () => {
expect(
xxhash64AsValue(new Uint8Array([0x61, 0x62, 0x63, 0x64]), 0xabcd).toString(16)
).toEqual('cda8fae4');
});
});
@@ -0,0 +1,17 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const xxhash32AsBn = require('./asBn');
const xxhash32AsHex = require('./asHex');
const xxhash32AsRaw = require('./asRaw');
/**
@summary Create [XXHash](http://cyan4973.github.io/xxHash/) values as BN, hex & number output
*/
module.exports = {
xxhash32AsBn,
xxhash32AsHex,
xxhash32AsRaw
};
+26
View File
@@ -0,0 +1,26 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const BN = require('bn.js');
const xxhash64AsValue = require('./asValue');
/**
@name xxhash64AsBn
@signature xxhash64AsBn (data: Buffer | Uint8Array | string, seed: number): BN
@summary Creates a xxhash BN from the input.
@description
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a BN.
@example
import { xxhash64AsNumber } from '@polkadot/util-crypto';
xxhash64AsBn('abcd', 0xabcd)) // => new BN(0xe29f70f8b8c96df7)
*/
module.exports = function xxhash64AsBn (data: Buffer | Uint8Array | string, seed: number): BN {
return new BN(
xxhash64AsValue(data, seed).toString(16),
16
);
};
@@ -0,0 +1,13 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const { xxhash64AsBn } = require('./index');
describe('xxhash64AsBn', () => {
it('creates the correct BN output', () => {
expect(
xxhash64AsBn('abcd', 0xabcd).toString(16)
).toEqual('e29f70f8b8c96df7');
});
});
@@ -0,0 +1,25 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const hexAddPrefix = require('@polkadot/util/hex/addPrefix');
const xxhash64AsRaw = require('./asRaw');
/**
@name xxhash64AsHex
@signature xxhash64AsHex (data: Buffer | Uint8Array | string, seed: number): string
@summary Creates a xxhash hex from the input.
@description
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a hex string.
@example
import { xxhash64AsHex } from '@polkadot/util-crypto';
xxhash64AsHex('abcd', 0xabcd)) // => 0xe29f70f8b8c96df7
*/
module.exports = function xxhash64AsHex (data: Buffer | Uint8Array | string, seed: number): string {
return hexAddPrefix(
xxhash64AsRaw(data, seed)
);
};
@@ -0,0 +1,13 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const { xxhash64AsHex } = require('./index');
describe('xxhash64AsHex', () => {
it('creates the correct hex output', () => {
expect(
xxhash64AsHex('abcd', 0xabcd)
).toEqual('0xe29f70f8b8c96df7');
});
});
@@ -0,0 +1,21 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const xxhash64AsValue = require('./asValue');
/**
@name xxhash64AsRaw
@signature xxhash64AsRaw (data: Buffer | Uint8Array | string, seed: number): string
@summary Creates a xxhash non-prefixed hex from the input.
@description
From either a `string`, `Uint8Array` or a `Buffer` input, create the xxhash and return the result as a non-prefixed hex string.
@example
import { xxhash64AsRaw } from '@polkadot/util-crypto';
xxhash64AsRaw('abcd', 0xabcd)) // => e29f70f8b8c96df7
*/
module.exports = function xxhash64AsRaw (data: Buffer | Uint8Array | string, seed: number): string {
return xxhash64AsValue(data, seed).toString(16);
};
@@ -0,0 +1,13 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const { xxhash64AsRaw } = require('./index');
describe('xxhash64AsRaw', () => {
it('creates the correct non-prefixed hex output', () => {
expect(
xxhash64AsRaw('abcd', 0xabcd)
).toEqual('e29f70f8b8c96df7');
});
});
@@ -0,0 +1,20 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { xxhashjs$Result } from 'xxhashjs';
const isBuffer = require('@polkadot/util/is/buffer');
const isString = require('@polkadot/util/is/string');
const xxhashjs = require('xxhashjs');
module.exports = function xxhash64AsValue (data: Buffer | Uint8Array | string, seed: number): xxhashjs$Result {
if (isBuffer(data) || isString(data)) {
// $FlowFixMe we have determined the type
return xxhashjs.h64(data, seed);
}
// $FlowFixMe we have determined the type
return xxhashjs.h64(data.buffer, seed);
};
@@ -0,0 +1,25 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const xxhash64AsValue = require('./asValue');
describe('xxhash64AsValue', () => {
it('creates the correct output (string input)', () => {
expect(
xxhash64AsValue('abcd', 0xabcd).toString(16)
).toEqual('e29f70f8b8c96df7');
});
it('creates the correct output (Buffer input)', () => {
expect(
xxhash64AsValue(Buffer.from([0x61, 0x62, 0x63, 0x64]), 0xabcd).toString(16)
).toEqual('e29f70f8b8c96df7');
});
it('creates the correct output (Uint8Array input)', () => {
expect(
xxhash64AsValue(new Uint8Array([0x61, 0x62, 0x63, 0x64]), 0xabcd).toString(16)
).toEqual('e29f70f8b8c96df7');
});
});
@@ -0,0 +1,17 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
const xxhash64AsBn = require('./asBn');
const xxhash64AsHex = require('./asHex');
const xxhash64AsRaw = require('./asRaw');
/**
@summary Create [XXHash](http://cyan4973.github.io/xxHash/) values as BN, hex & number output
*/
module.exports = {
xxhash64AsBn,
xxhash64AsHex,
xxhash64AsRaw
};
+1 -2
View File
@@ -29,8 +29,7 @@
"test": "echo \"Tests only available from root wrapper\""
},
"devDependencies": {
"@polkadot/api-jsonrpc": "^0.7.1",
"@polkadot/dev": "^0.15.1"
"@polkadot/api-jsonrpc": "^0.7.1"
},
"dependencies": {
"babel-runtime": "^6.26.0",
+3 -3
View File
@@ -69,9 +69,9 @@
dependencies:
babel-runtime "^6.26.0"
"@polkadot/dev@^0.15.1":
version "0.15.1"
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.15.1.tgz#74b764552ae7c11cbf35797abd389148033fe562"
"@polkadot/dev@^0.15.2":
version "0.15.2"
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.15.2.tgz#496350e1ca3dfa30d73bd64206c221fa6e96b3ec"
dependencies:
babel-cli "^6.26.0"
babel-core "^6.26.0"