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 u8aFromBuffer = require('../buffer/toU8a');
/**
@name u8aFromBuffer
@signature u8aFromBuffer (value?: Buffer): number
@summary Creates a Uint8Array value from a Buffer object.
@alias buffer/toU8a
*/
module.exports = u8aFromBuffer;
+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 { u8aFromBuffer } = require('./index');
describe('u8aFromBuffer', () => {
it('exists as a function', () => {
expect(
isFunction(u8aFromBuffer)
).toEqual(true);
});
});
+28
View File
@@ -0,0 +1,28 @@
// 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
require('./polyfill/textEncoder');
const encoder = new TextEncoder();
/**
@name u8aFromUtf8
@signature u8aFromUtf8 (value?: string): UInt8Array
@summary Creates a Uint8Array object from a utf-8 string.
@description
String input values return the actual encoded `UInt8Array`. `null` or `undefined` values returns an empty encoded array.
@example
import { u8aFromUtf8 } from '@polkadot/util';
u8aFromUtf8('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
*/
module.exports = function u8aFromUtf8 (value?: string): Uint8Array {
// flowlint-next-line sketchy-null-string:off
if (!value) {
return new Uint8Array([]);
}
return encoder.encode(value);
};
+21
View File
@@ -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.
const { u8aFromUtf8 } = require('./index');
describe('u8aFromUtf8', () => {
it('decodes to an empty string for undefined', () => {
expect(
u8aFromUtf8()
).toEqual(new Uint8Array([]));
});
it('encodes the string correctly', () => {
expect(
u8aFromUtf8('Привет, мир!')
).toEqual(
new Uint8Array([208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130, 44, 32, 208, 188, 208, 184, 209, 128, 33])
);
});
});
+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 u8aFromBuffer = require('./fromBuffer');
const u8aFromUtf8 = require('./fromUtf8');
const u8aToBuffer = require('./toBuffer');
const u8aToUtf8 = require('./toUtf8');
/**
@summary Utility methods to convert to and from `Uint8Array` objects
*/
module.exports = {
u8aFromBuffer,
u8aFromUtf8,
u8aToBuffer,
u8aToUtf8
};
@@ -0,0 +1,9 @@
// 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
if (typeof TextDecoder === 'undefined') {
// $FlowFixMe trust me flow, it is there
global.TextDecoder = require('util').TextDecoder;
}
@@ -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.
describe('TextDecoder', () => {
let origTD;
beforeEach(() => {
origTD = global.TextDecoder;
});
afterEach(() => {
global.TextDecoder = origTD;
});
it('polyfills with no exceptions (without TextDecoder)', () => {
global.TextDecoder = null;
expect(require('./textDecoder')).toBeDefined();
expect(global.TextDecoder).toBeDefined();
});
it('polyfills with no exceptions (with TextDecoder)', () => {
global.TextDecoder = require('util').TextDecoder;
expect(require('./textDecoder')).toBeDefined();
expect(global.TextDecoder).toBeDefined();
});
});
@@ -0,0 +1,9 @@
// 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
if (typeof TextEncoder === 'undefined') {
// $FlowFixMe trust me flow, it is there
global.TextEncoder = require('util').TextEncoder;
}
@@ -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.
describe('TextEncoder', () => {
let origTE;
beforeEach(() => {
origTE = global.TextEncoder;
});
afterEach(() => {
global.TextEncoder = origTE;
});
it('polyfills with no exceptions (without TextEncoder)', () => {
global.TextEncoder = null;
expect(require('./textEncoder')).toBeDefined();
expect(global.TextEncoder).toBeDefined();
});
it('polyfills with no exceptions (with TextEncoder)', () => {
global.TextEncoder = require('util').TextEncoder;
expect(require('./textEncoder')).toBeDefined();
expect(global.TextEncoder).toBeDefined();
});
});
+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
/**
@name u8aToBuffer
@signature u8aToBuffer (value?: UInt8Array): Buffer
@summary Creates a Buffer object from a hex string.
@description
`null` inputs returns an empty `Buffer` result. `UInt8Array` input values return the actual bytes value converted to a `Buffer`. Anything that is not a `UInt8Array` throws an error.
@example
import { u8aToBuffer } from '@polkadot/util';
console.log('Buffer', u8aToBuffer('0x123480001f'));
*/
module.exports = function u8aToBuffer (value?: Uint8Array): Buffer {
if (!value) {
return Buffer.from([]);
}
return Buffer.from(value);
};
+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 { u8aToBuffer } = require('./index');
describe('u8aToBuffer', () => {
it('returns [] when the buffer is null', () => {
expect(
u8aToBuffer(null)
).toEqual(Buffer.from([]));
});
it('returns the Buffer value for the Uint8Array', () => {
expect(
u8aToBuffer(new Uint8Array([128, 0, 10]))
).toEqual(Buffer.from([128, 0, 10]));
});
});
+27
View File
@@ -0,0 +1,27 @@
// 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
require('./polyfill/textDecoder');
const decoder = new TextDecoder('utf-8');
/**
@name u8aToUtf8
@signature u8aToUtf8 (value?: UInt8Array): string
@summary Creates a utf-8 string from a Uint8Array object.
@description
`UInt8Array` input values return the actual decoded utf-8 string. `null` or `undefined` values returns an empty string.
@example
import { u8aToString } from '@polkadot/util';
u8aToUtf8(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello
*/
module.exports = function u8aToUtf8 (value?: Uint8Array): string {
if (!value || !value.length) {
return '';
}
return decoder.decode(value);
};
+27
View File
@@ -0,0 +1,27 @@
// 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 { u8aToUtf8 } = require('./index');
describe('u8aToUtf8', () => {
it('decodes to an empty string for undefined', () => {
expect(
u8aToUtf8()
).toEqual('');
});
it('decodes to an empty string for empty buffer', () => {
expect(
u8aToUtf8(new Uint8Array([]))
).toEqual('');
});
it('decodes the buffer correctly', () => {
expect(
u8aToUtf8(
new Uint8Array([208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130, 44, 32, 208, 188, 208, 184, 209, 128, 33])
)
).toEqual('Привет, мир!');
});
});