Add Blake2s & XXHash utility functions (#66)

* Initial split of util & util-crypto

* Add initial XXHash version

* Blake2s wit tests & documentation

* Add sha512 (from NaCl)

* Add randomBytes (from NaCl)

* Flow fixes

* Update documentation

* Update latest @polkadot/dev
This commit is contained in:
Jaco Greeff
2018-01-25 14:13:43 +01:00
committed by GitHub
parent ade4c828af
commit b5c5734c7c
183 changed files with 1802 additions and 109 deletions
+14
View File
@@ -0,0 +1,14 @@
// 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 bufferFromHex = require('../hex/toBuffer');
/**
@name bufferFromHex
@signature bufferFromHex (value?: string): Buffer
@summary Creates a Buffer object from a hex string.
@alias hex/toBuffer
*/
module.exports = bufferFromHex;
+15
View File
@@ -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 isFunction = require('../is/function');
const { bufferFromHex } = require('./index');
describe('bufferFromHex', () => {
it('exists as a function', () => {
expect(
isFunction(bufferFromHex)
).toEqual(true);
});
});
+14
View File
@@ -0,0 +1,14 @@
// 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 bufferFromNumber = require('../number/toBuffer');
/**
@name bufferFromNumber
@signature bufferFromNumber (value?: number): Buffer
@summary Creates a Buffer object from a number.
@alias number/toBuffer
*/
module.exports = bufferFromNumber;
@@ -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 isFunction = require('../is/function');
const { bufferFromNumber } = require('./index');
describe('bufferFromNumber', () => {
it('exists as a function', () => {
expect(
isFunction(bufferFromNumber)
).toEqual(true);
});
});
+14
View File
@@ -0,0 +1,14 @@
// 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 bufferFromU8a = require('../u8a/toBuffer');
/**
@name bufferFromU8a
@signature bufferFromU8a (value?: string): Buffer
@summary Creates a Buffer object from a Uint8Array.
@alias u8a/toBuffer
*/
module.exports = bufferFromU8a;
+15
View File
@@ -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 isFunction = require('../is/function');
const { bufferFromHex } = require('./index');
describe('bufferFromHex', () => {
it('exists as a function', () => {
expect(
isFunction(bufferFromHex)
).toEqual(true);
});
});
+23
View File
@@ -0,0 +1,23 @@
// 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 bufferFromHex = require('./fromHex');
const bufferFromNumber = require('./fromNumber');
const bufferFromU8a = require('./fromU8a');
const bufferToHex = require('./toHex');
const bufferToNumber = require('./toNumber');
const bufferToU8a = require('./toU8a');
/**
@summary Utility methods to convert to and from `Buffer` objects
*/
module.exports = {
bufferFromHex,
bufferFromNumber,
bufferFromU8a,
bufferToHex,
bufferToNumber,
bufferToU8a
};
+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('../hex/addPrefix');
const ZERO_HEX = '0x00';
/**
@name bufferToHex
@signature bufferToHex (value?: Buffer): string
@summary Creates a hex value from a Buffer object.
@description
`null` inputs returns a `0x` result, `Buffer` values return the actual value as a `0x` prefixed hex value. Anything that is not a `Buffer` object throws an error.
@example
import { bufferToHex } from '@polkadot/util';
bufferToHex(Buffer.from([1, 2, 3]); // => 0x010203
*/
module.exports = function bufferToHex (value?: Buffer): string {
if (!value) {
return ZERO_HEX;
}
return hexAddPrefix(
value.toString('hex')
);
};
+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.
const { bufferToHex } = require('./index');
describe('bufferToHex', () => {
it('returns 0x00 when the buffer is null', () => {
expect(
bufferToHex(null)
).toEqual('0x00');
});
it('returns the hex value for the buffer', () => {
expect(
bufferToHex(Buffer.from([128, 0, 10]))
).toEqual('0x80000a');
});
});
+30
View File
@@ -0,0 +1,30 @@
// 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
/**
@name bufferToNumber
@signature bufferToNumber (value?: Buffer): number
@summary Creates a Number value from a Buffer object.
@description
`null` inputs returns an NaN result, `Buffer` values return the actual value as a `Number`.
@example
import { bufferToNumber } from '@polkadot/util';
bufferToNumber(Buffer.from([12, 34, 56])); // => 0x123456
*/
module.exports = function bufferToNumber (buffer?: Buffer): number {
if (!buffer || !buffer.length) {
return NaN;
}
const length = buffer.length;
let value = 0;
for (let index = 0; index < length; ++index) {
value = (value * 256) + buffer[index];
}
return value;
};
+25
View File
@@ -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 { bufferToNumber } = require('./index');
describe('bufferToNumber', () => {
it('converts nothing to NaN', () => {
expect(
bufferToNumber()
).toEqual(NaN);
});
it('converts empty buffer to NaN', () => {
expect(
bufferToNumber(Buffer.from([]))
).toEqual(NaN);
});
it('converts a buffer to a value', () => {
expect(
bufferToNumber(Buffer.from([0x12, 0x34]))
).toEqual(0x1234);
});
});
+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
/**
@name bufferToU8a
@signature bufferToU8a (value?: Buffer): string
@summary Creates a Uint8Array value from a Buffer object.
@description
`null` inputs returns an empty result, `Buffer` values return the actual value as a `Uint8Array`. Anything that is not a `Buffer` object throws an error.
@example
import { bufferToU8a } from '@polkadot/util';
bufferToU8a(Buffer.from([1, 2, 3]));
*/
module.exports = function bufferToU8a (buffer?: Buffer): Uint8Array {
if (!buffer) {
return new Uint8Array([]);
}
const array = new Uint8Array(buffer.length);
for (let index = 0; index < buffer.length; ++index) {
array[index] = buffer[index];
}
return array;
};
+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.
const { bufferToU8a } = require('./index');
describe('bufferToU8a', () => {
it('returns an empty buffer when null provided', () => {
expect(
bufferToU8a(null)
).toEqual(new Uint8Array([]));
});
it('returns a Uint8Buffer with the correct values', () => {
expect(
bufferToU8a(Buffer.from([128, 0, 10]))
).toEqual(new Uint8Array([128, 0, 10]));
});
});