Beta 7 with some repo names changed.

This commit is contained in:
Luis Fernando Molina committed 2020-11-15 13:05:19 +01:00
1 parent 0f7b40cca7
commit 963c19493a
3408 files changed
+462217 -15516

No files matched your search

+273
View File
@@ -0,0 +1,273 @@
// Blake2B in pure Javascript
// Adapted from the reference implementation in RFC7693
// Ported to Javascript by DC - https://github.com/dcposch
var util = require('./util')
// 64-bit unsigned addition
// Sets v[a,a+1] += v[b,b+1]
// v should be a Uint32Array
function ADD64AA (v, a, b) {
var o0 = v[a] + v[b]
var o1 = v[a + 1] + v[b + 1]
if (o0 >= 0x100000000) {
o1++
}
v[a] = o0
v[a + 1] = o1
}
// 64-bit unsigned addition
// Sets v[a,a+1] += b
// b0 is the low 32 bits of b, b1 represents the high 32 bits
function ADD64AC (v, a, b0, b1) {
var o0 = v[a] + b0
if (b0 < 0) {
o0 += 0x100000000
}
var o1 = v[a + 1] + b1
if (o0 >= 0x100000000) {
o1++
}
v[a] = o0
v[a + 1] = o1
}
// Little-endian byte access
function B2B_GET32 (arr, i) {
return (arr[i] ^
(arr[i + 1] << 8) ^
(arr[i + 2] << 16) ^
(arr[i + 3] << 24))
}
// G Mixing function
// The ROTRs are inlined for speed
function B2B_G (a, b, c, d, ix, iy) {
var x0 = m[ix]
var x1 = m[ix + 1]
var y0 = m[iy]
var y1 = m[iy + 1]
ADD64AA(v, a, b) // v[a,a+1] += v[b,b+1] ... in JS we must store a uint64 as two uint32s
ADD64AC(v, a, x0, x1) // v[a, a+1] += x ... x0 is the low 32 bits of x, x1 is the high 32 bits
// v[d,d+1] = (v[d,d+1] xor v[a,a+1]) rotated to the right by 32 bits
var xor0 = v[d] ^ v[a]
var xor1 = v[d + 1] ^ v[a + 1]
v[d] = xor1
v[d + 1] = xor0
ADD64AA(v, c, d)
// v[b,b+1] = (v[b,b+1] xor v[c,c+1]) rotated right by 24 bits
xor0 = v[b] ^ v[c]
xor1 = v[b + 1] ^ v[c + 1]
v[b] = (xor0 >>> 24) ^ (xor1 << 8)
v[b + 1] = (xor1 >>> 24) ^ (xor0 << 8)
ADD64AA(v, a, b)
ADD64AC(v, a, y0, y1)
// v[d,d+1] = (v[d,d+1] xor v[a,a+1]) rotated right by 16 bits
xor0 = v[d] ^ v[a]
xor1 = v[d + 1] ^ v[a + 1]
v[d] = (xor0 >>> 16) ^ (xor1 << 16)
v[d + 1] = (xor1 >>> 16) ^ (xor0 << 16)
ADD64AA(v, c, d)
// v[b,b+1] = (v[b,b+1] xor v[c,c+1]) rotated right by 63 bits
xor0 = v[b] ^ v[c]
xor1 = v[b + 1] ^ v[c + 1]
v[b] = (xor1 >>> 31) ^ (xor0 << 1)
v[b + 1] = (xor0 >>> 31) ^ (xor1 << 1)
}
// Initialization Vector
var BLAKE2B_IV32 = new Uint32Array([
0xF3BCC908, 0x6A09E667, 0x84CAA73B, 0xBB67AE85,
0xFE94F82B, 0x3C6EF372, 0x5F1D36F1, 0xA54FF53A,
0xADE682D1, 0x510E527F, 0x2B3E6C1F, 0x9B05688C,
0xFB41BD6B, 0x1F83D9AB, 0x137E2179, 0x5BE0CD19
])
var SIGMA8 = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3,
11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4,
7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8,
9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13,
2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9,
12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11,
13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10,
6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5,
10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3
]
// These are offsets into a uint64 buffer.
// Multiply them all by 2 to make them offsets into a uint32 buffer,
// because this is Javascript and we don't have uint64s
var SIGMA82 = new Uint8Array(SIGMA8.map(function (x) { return x * 2 }))
// Compression function. 'last' flag indicates last block.
// Note we're representing 16 uint64s as 32 uint32s
var v = new Uint32Array(32)
var m = new Uint32Array(32)
function blake2bCompress (ctx, last) {
var i = 0
// init work variables
for (i = 0; i < 16; i++) {
v[i] = ctx.h[i]
v[i + 16] = BLAKE2B_IV32[i]
}
// low 64 bits of offset
v[24] = v[24] ^ ctx.t
v[25] = v[25] ^ (ctx.t / 0x100000000)
// high 64 bits not supported, offset may not be higher than 2**53-1
// last block flag set ?
if (last) {
v[28] = ~v[28]
v[29] = ~v[29]
}
// get little-endian words
for (i = 0; i < 32; i++) {
m[i] = B2B_GET32(ctx.b, 4 * i)
}
// twelve rounds of mixing
// uncomment the DebugPrint calls to log the computation
// and match the RFC sample documentation
// util.debugPrint(' m[16]', m, 64)
for (i = 0; i < 12; i++) {
// util.debugPrint(' (i=' + (i < 10 ? ' ' : '') + i + ') v[16]', v, 64)
B2B_G(0, 8, 16, 24, SIGMA82[i * 16 + 0], SIGMA82[i * 16 + 1])
B2B_G(2, 10, 18, 26, SIGMA82[i * 16 + 2], SIGMA82[i * 16 + 3])
B2B_G(4, 12, 20, 28, SIGMA82[i * 16 + 4], SIGMA82[i * 16 + 5])
B2B_G(6, 14, 22, 30, SIGMA82[i * 16 + 6], SIGMA82[i * 16 + 7])
B2B_G(0, 10, 20, 30, SIGMA82[i * 16 + 8], SIGMA82[i * 16 + 9])
B2B_G(2, 12, 22, 24, SIGMA82[i * 16 + 10], SIGMA82[i * 16 + 11])
B2B_G(4, 14, 16, 26, SIGMA82[i * 16 + 12], SIGMA82[i * 16 + 13])
B2B_G(6, 8, 18, 28, SIGMA82[i * 16 + 14], SIGMA82[i * 16 + 15])
}
// util.debugPrint(' (i=12) v[16]', v, 64)
for (i = 0; i < 16; i++) {
ctx.h[i] = ctx.h[i] ^ v[i] ^ v[i + 16]
}
// util.debugPrint('h[8]', ctx.h, 64)
}
// Creates a BLAKE2b hashing context
// Requires an output length between 1 and 64 bytes
// Takes an optional Uint8Array key
function blake2bInit (outlen, key) {
if (outlen === 0 || outlen > 64) {
throw new Error('Illegal output length, expected 0 < length <= 64')
}
if (key && key.length > 64) {
throw new Error('Illegal key, expected Uint8Array with 0 < length <= 64')
}
// state, 'param block'
var ctx = {
b: new Uint8Array(128),
h: new Uint32Array(16),
t: 0, // input count
c: 0, // pointer within buffer
outlen: outlen // output length in bytes
}
// initialize hash state
for (var i = 0; i < 16; i++) {
ctx.h[i] = BLAKE2B_IV32[i]
}
var keylen = key ? key.length : 0
ctx.h[0] ^= 0x01010000 ^ (keylen << 8) ^ outlen
// key the hash, if applicable
if (key) {
blake2bUpdate(ctx, key)
// at the end
ctx.c = 128
}
return ctx
}
// Updates a BLAKE2b streaming hash
// Requires hash context and Uint8Array (byte array)
function blake2bUpdate (ctx, input) {
for (var i = 0; i < input.length; i++) {
if (ctx.c === 128) { // buffer full ?
ctx.t += ctx.c // add counters
blake2bCompress(ctx, false) // compress (not last)
ctx.c = 0 // counter to zero
}
ctx.b[ctx.c++] = input[i]
}
}
// Completes a BLAKE2b streaming hash
// Returns a Uint8Array containing the message digest
function blake2bFinal (ctx) {
ctx.t += ctx.c // mark last block offset
while (ctx.c < 128) { // fill up with zeros
ctx.b[ctx.c++] = 0
}
blake2bCompress(ctx, true) // final block flag = 1
// little endian convert and store
var out = new Uint8Array(ctx.outlen)
for (var i = 0; i < ctx.outlen; i++) {
out[i] = ctx.h[i >> 2] >> (8 * (i & 3))
}
return out
}
// Computes the BLAKE2B hash of a string or byte array, and returns a Uint8Array
//
// Returns a n-byte Uint8Array
//
// Parameters:
// - input - the input bytes, as a string, Buffer or Uint8Array
// - key - optional key Uint8Array, up to 64 bytes
// - outlen - optional output length in bytes, default 64
function blake2b (input, key, outlen) {
// preprocess inputs
outlen = outlen || 64
input = util.normalizeInput(input)
// do the math
var ctx = blake2bInit(outlen, key)
blake2bUpdate(ctx, input)
return blake2bFinal(ctx)
}
// Computes the BLAKE2B hash of a string or byte array
//
// Returns an n-byte hash in hex, all lowercase
//
// Parameters:
// - input - the input bytes, as a string, Buffer, or Uint8Array
// - key - optional key Uint8Array, up to 64 bytes
// - outlen - optional output length in bytes, default 64
function blake2bHex (input, key, outlen) {
var output = blake2b(input, key, outlen)
return util.toHex(output)
}
module.exports = {
blake2b: blake2b,
blake2bHex: blake2bHex,
blake2bInit: blake2bInit,
blake2bUpdate: blake2bUpdate,
blake2bFinal: blake2bFinal
}
+190
View File
@@ -0,0 +1,190 @@
// BLAKE2s hash function in pure Javascript
// Adapted from the reference implementation in RFC7693
// Ported to Javascript by DC - https://github.com/dcposch
var util = require('./util')
// Little-endian byte access.
// Expects a Uint8Array and an index
// Returns the little-endian uint32 at v[i..i+3]
function B2S_GET32 (v, i) {
return v[i] ^ (v[i + 1] << 8) ^ (v[i + 2] << 16) ^ (v[i + 3] << 24)
}
// Mixing function G.
function B2S_G (a, b, c, d, x, y) {
v[a] = v[a] + v[b] + x
v[d] = ROTR32(v[d] ^ v[a], 16)
v[c] = v[c] + v[d]
v[b] = ROTR32(v[b] ^ v[c], 12)
v[a] = v[a] + v[b] + y
v[d] = ROTR32(v[d] ^ v[a], 8)
v[c] = v[c] + v[d]
v[b] = ROTR32(v[b] ^ v[c], 7)
}
// 32-bit right rotation
// x should be a uint32
// y must be between 1 and 31, inclusive
function ROTR32 (x, y) {
return (x >>> y) ^ (x << (32 - y))
}
// Initialization Vector.
var BLAKE2S_IV = new Uint32Array([
0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19])
var SIGMA = new Uint8Array([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3,
11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4,
7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8,
9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13,
2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9,
12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11,
13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10,
6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5,
10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0])
// Compression function. "last" flag indicates last block
var v = new Uint32Array(16)
var m = new Uint32Array(16)
function blake2sCompress (ctx, last) {
var i = 0
for (i = 0; i < 8; i++) { // init work variables
v[i] = ctx.h[i]
v[i + 8] = BLAKE2S_IV[i]
}
v[12] ^= ctx.t // low 32 bits of offset
v[13] ^= (ctx.t / 0x100000000) // high 32 bits
if (last) { // last block flag set ?
v[14] = ~v[14]
}
for (i = 0; i < 16; i++) { // get little-endian words
m[i] = B2S_GET32(ctx.b, 4 * i)
}
// ten rounds of mixing
// uncomment the DebugPrint calls to log the computation
// and match the RFC sample documentation
// util.debugPrint(' m[16]', m, 32)
for (i = 0; i < 10; i++) {
// util.debugPrint(' (i=' + i + ') v[16]', v, 32)
B2S_G(0, 4, 8, 12, m[SIGMA[i * 16 + 0]], m[SIGMA[i * 16 + 1]])
B2S_G(1, 5, 9, 13, m[SIGMA[i * 16 + 2]], m[SIGMA[i * 16 + 3]])
B2S_G(2, 6, 10, 14, m[SIGMA[i * 16 + 4]], m[SIGMA[i * 16 + 5]])
B2S_G(3, 7, 11, 15, m[SIGMA[i * 16 + 6]], m[SIGMA[i * 16 + 7]])
B2S_G(0, 5, 10, 15, m[SIGMA[i * 16 + 8]], m[SIGMA[i * 16 + 9]])
B2S_G(1, 6, 11, 12, m[SIGMA[i * 16 + 10]], m[SIGMA[i * 16 + 11]])
B2S_G(2, 7, 8, 13, m[SIGMA[i * 16 + 12]], m[SIGMA[i * 16 + 13]])
B2S_G(3, 4, 9, 14, m[SIGMA[i * 16 + 14]], m[SIGMA[i * 16 + 15]])
}
// util.debugPrint(' (i=10) v[16]', v, 32)
for (i = 0; i < 8; i++) {
ctx.h[i] ^= v[i] ^ v[i + 8]
}
// util.debugPrint('h[8]', ctx.h, 32)
}
// Creates a BLAKE2s hashing context
// Requires an output length between 1 and 32 bytes
// Takes an optional Uint8Array key
function blake2sInit (outlen, key) {
if (!(outlen > 0 && outlen <= 32)) {
throw new Error('Incorrect output length, should be in [1, 32]')
}
var keylen = key ? key.length : 0
if (key && !(keylen > 0 && keylen <= 32)) {
throw new Error('Incorrect key length, should be in [1, 32]')
}
var ctx = {
h: new Uint32Array(BLAKE2S_IV), // hash state
b: new Uint32Array(64), // input block
c: 0, // pointer within block
t: 0, // input count
outlen: outlen // output length in bytes
}
ctx.h[0] ^= 0x01010000 ^ (keylen << 8) ^ outlen
if (keylen > 0) {
blake2sUpdate(ctx, key)
ctx.c = 64 // at the end
}
return ctx
}
// Updates a BLAKE2s streaming hash
// Requires hash context and Uint8Array (byte array)
function blake2sUpdate (ctx, input) {
for (var i = 0; i < input.length; i++) {
if (ctx.c === 64) { // buffer full ?
ctx.t += ctx.c // add counters
blake2sCompress(ctx, false) // compress (not last)
ctx.c = 0 // counter to zero
}
ctx.b[ctx.c++] = input[i]
}
}
// Completes a BLAKE2s streaming hash
// Returns a Uint8Array containing the message digest
function blake2sFinal (ctx) {
ctx.t += ctx.c // mark last block offset
while (ctx.c < 64) { // fill up with zeros
ctx.b[ctx.c++] = 0
}
blake2sCompress(ctx, true) // final block flag = 1
// little endian convert and store
var out = new Uint8Array(ctx.outlen)
for (var i = 0; i < ctx.outlen; i++) {
out[i] = (ctx.h[i >> 2] >> (8 * (i & 3))) & 0xFF
}
return out
}
// Computes the BLAKE2S hash of a string or byte array, and returns a Uint8Array
//
// Returns a n-byte Uint8Array
//
// Parameters:
// - input - the input bytes, as a string, Buffer, or Uint8Array
// - key - optional key Uint8Array, up to 32 bytes
// - outlen - optional output length in bytes, default 64
function blake2s (input, key, outlen) {
// preprocess inputs
outlen = outlen || 32
input = util.normalizeInput(input)
// do the math
var ctx = blake2sInit(outlen, key)
blake2sUpdate(ctx, input)
return blake2sFinal(ctx)
}
// Computes the BLAKE2S hash of a string or byte array
//
// Returns an n-byte hash in hex, all lowercase
//
// Parameters:
// - input - the input bytes, as a string, Buffer, or Uint8Array
// - key - optional key Uint8Array, up to 32 bytes
// - outlen - optional output length in bytes, default 64
function blake2sHex (input, key, outlen) {
var output = blake2s(input, key, outlen)
return util.toHex(output)
}
module.exports = {
blake2s: blake2s,
blake2sHex: blake2sHex,
blake2sInit: blake2sInit,
blake2sUpdate: blake2sUpdate,
blake2sFinal: blake2sFinal
}
+15
View File
@@ -0,0 +1,15 @@
var b2b = require('./blake2b')
var b2s = require('./blake2s')
module.exports = {
blake2b: b2b.blake2b,
blake2bHex: b2b.blake2bHex,
blake2bInit: b2b.blake2bInit,
blake2bUpdate: b2b.blake2bUpdate,
blake2bFinal: b2b.blake2bFinal,
blake2s: b2s.blake2s,
blake2sHex: b2s.blake2sHex,
blake2sInit: b2s.blake2sInit,
blake2sUpdate: b2s.blake2sUpdate,
blake2sFinal: b2s.blake2sFinal
}
+48
View File
@@ -0,0 +1,48 @@
{
"_from": "blakejs@^1.1.0",
"_id": "blakejs@1.1.0",
"_inBundle": false,
"_integrity": "sha1-ad+S75U6qIylGjLfarHFShVfx6U=",
"_location": "/blakejs",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "blakejs@^1.1.0",
"name": "blakejs",
"escapedName": "blakejs",
"rawSpec": "^1.1.0",
"saveSpec": null,
"fetchSpec": "^1.1.0"
},
"_requiredBy": [
"/ethereum-cryptography"
],
"_resolved": "https://registry.npmjs.org/blakejs/-/blakejs-1.1.0.tgz",
"_shasum": "69df92ef953aa88ca51a32df6ab1c54a155fc7a5",
"_spec": "blakejs@^1.1.0",
"_where": "C:\\Users\\Luis\\source\\repos\\AAMasters\\BackendServers\\node_modules\\ethereum-cryptography",
"author": "",
"bugs": {
"url": "https://github.com/dcposch/blakejs/issues"
},
"bundleDependencies": false,
"deprecated": false,
"description": "Pure Javascript implementation of the BLAKE2b and BLAKE2s hash functions",
"devDependencies": {
"standard": "*",
"tape": "*"
},
"homepage": "https://github.com/dcposch/blakejs#readme",
"license": "CC0-1.0",
"main": "index.js",
"name": "blakejs",
"repository": {
"type": "git",
"url": "git+https://github.com/dcposch/blakejs.git"
},
"scripts": {
"test": "standard && node test_blake2b.js && node test_blake2s.js"
},
"version": "1.1.0"
}
+88
View File
@@ -0,0 +1,88 @@
var test = require('tape')
var blake2b = require('./blake2b')
var util = require('./util')
var fs = require('fs')
var blake2bHex = blake2b.blake2bHex
test('BLAKE2b basic', function (t) {
// From the example computation in the RFC
t.equal(blake2bHex('abc'),
'ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1' +
'7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923')
t.equal(blake2bHex(''),
'786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419' +
'd25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce')
t.equal(blake2bHex('The quick brown fox jumps over the lazy dog'),
'a8add4bdddfd93e4877d2746e62817b116364a1fa7bc148d95090bc7333b3673' +
'f82401cf7aa2e4cb1ecd90296e3f14cb5413f8ed77be73045b13914cdcd6a918')
t.end()
})
test('Input types', function (t) {
// Supports string, Uint8Array, and Buffer inputs
// We already verify that blake2bHex('abc') produces the correct hash above
t.equal(blake2bHex(new Uint8Array([97, 98, 99])), blake2bHex('abc'))
t.equal(blake2bHex(Buffer.from([97, 98, 99])), blake2bHex('abc'))
t.end()
})
test('BLAKE2b generated test vectors', function (t) {
var contents = fs.readFileSync('generated_test_vectors.txt', 'utf8')
contents.split('\n').forEach(function (line) {
if (line.length === 0) {
return
}
var parts = line.split('\t')
var inputHex = parts[0]
var keyHex = parts[1]
var outLen = parts[2]
var outHex = parts[3]
t.equal(blake2bHex(hexToBytes(inputHex), hexToBytes(keyHex), outLen), outHex)
})
t.end()
})
test('BLAKE2b performance', function (t) {
var N = 1 << 22 // number of bytes to hash
var RUNS = 3 // how often to repeat, to allow JIT to finish
console.log('Benchmarking BLAKE2b(' + (N >> 20) + ' MB input)')
util.testSpeed(blake2bHex, N, RUNS)
t.end()
})
test('Byte counter should support values up to 2**53', function (t) {
var testCases = [
{t: 1, a0: 1, a1: 0},
{t: 0xffffffff, a0: 0xffffffff, a1: 0},
{t: 0x100000000, a0: 0, a1: 1},
{t: 0x123456789abcd, a0: 0x6789abcd, a1: 0x12345},
// test 2**53 - 1
{t: 0x1fffffffffffff, a0: 0xffffffff, a1: 0x1fffff}]
testCases.forEach(function (testCase) {
var arr = new Uint32Array([0, 0])
// test the code that's inlined in both blake2s.js and blake2b.js
// to make sure it splits byte counters up to 2**53 into uint32s correctly
arr[0] ^= testCase.t
arr[1] ^= (testCase.t / 0x100000000)
t.equal(arr[0], testCase.a0)
t.equal(arr[1], testCase.a1)
})
t.end()
})
function hexToBytes (hex) {
var ret = new Uint8Array(hex.length / 2)
for (var i = 0; i < ret.length; i++) {
ret[i] = parseInt(hex.substring(i * 2, i * 2 + 2), 16)
}
return ret
}
+80
View File
@@ -0,0 +1,80 @@
var test = require('tape')
var toHex = require('./util').toHex
var util = require('./util')
var b2s = require('./blake2s')
var blake2s = b2s.blake2s
var blake2sHex = b2s.blake2sHex
var blake2sInit = b2s.blake2sInit
var blake2sUpdate = b2s.blake2sUpdate
var blake2sFinal = b2s.blake2sFinal
test('BLAKE2s basic', function (t) {
// From the example computation in the RFC
t.equal(blake2sHex('abc'),
'508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982')
t.equal(blake2sHex(new Uint8Array([97, 98, 99])),
'508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982')
t.equal(blake2sHex(Buffer.from([97, 98, 99])),
'508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982')
t.end()
})
test('BLAKE2s self test', function (t) {
// Grand hash of hash results
var expectedHash = [
0x6A, 0x41, 0x1F, 0x08, 0xCE, 0x25, 0xAD, 0xCD,
0xFB, 0x02, 0xAB, 0xA6, 0x41, 0x45, 0x1C, 0xEC,
0x53, 0xC5, 0x98, 0xB2, 0x4F, 0x4F, 0xC7, 0x87,
0xFB, 0xDC, 0x88, 0x79, 0x7F, 0x4C, 0x1D, 0xFE]
// Parameter sets
var outputLengths = [16, 20, 28, 32]
var inputLengths = [0, 3, 64, 65, 255, 1024]
// 256-bit hash for testing
var ctx = blake2sInit(32)
for (var i = 0; i < 4; i++) {
var outlen = outputLengths[i]
for (var j = 0; j < 6; j++) {
var inlen = inputLengths[j]
var arr = generateInput(inlen, inlen)
var hash = blake2s(arr, null, outlen) // unkeyed hash
blake2sUpdate(ctx, hash) // hash the hash
var key = generateInput(outlen, outlen)
hash = blake2s(arr, key, outlen) // keyed hash
blake2sUpdate(ctx, hash) // hash the hash
}
}
// Compute and compare the hash of hashes
var finalHash = blake2sFinal(ctx)
t.equal(toHex(finalHash), toHex(expectedHash))
t.end()
})
// Returns a Uint8Array of len bytes
function generateInput (len, seed) {
var out = new Uint8Array(len)
var a = new Uint32Array(3)
a[0] = 0xDEAD4BAD * seed // prime
a[1] = 1
for (var i = 0; i < len; i++) { // fill the buf
a[2] = a[0] + a[1]
a[0] = a[1]
a[1] = a[2]
out[i] = (a[2] >>> 24) & 0xFF
}
return out
}
test('BLAKE2s performance', function (t) {
var N = 1 << 22 // number of bytes to hash
var RUNS = 3 // how often to repeat, to allow JIT to finish
console.log('Benchmarking BLAKE2s(' + (N >> 20) + ' MB input)')
util.testSpeed(blake2sHex, N, RUNS)
t.end()
})
+81
View File
@@ -0,0 +1,81 @@
var ERROR_MSG_INPUT = 'Input must be an string, Buffer or Uint8Array'
// For convenience, let people hash a string, not just a Uint8Array
function normalizeInput (input) {
var ret
if (input instanceof Uint8Array) {
ret = input
} else if (input instanceof Buffer) {
ret = new Uint8Array(input)
} else if (typeof (input) === 'string') {
ret = new Uint8Array(Buffer.from(input, 'utf8'))
} else {
throw new Error(ERROR_MSG_INPUT)
}
return ret
}
// Converts a Uint8Array to a hexadecimal string
// For example, toHex([255, 0, 255]) returns "ff00ff"
function toHex (bytes) {
return Array.prototype.map.call(bytes, function (n) {
return (n < 16 ? '0' : '') + n.toString(16)
}).join('')
}
// Converts any value in [0...2^32-1] to an 8-character hex string
function uint32ToHex (val) {
return (0x100000000 + val).toString(16).substring(1)
}
// For debugging: prints out hash state in the same format as the RFC
// sample computation exactly, so that you can diff
function debugPrint (label, arr, size) {
var msg = '\n' + label + ' = '
for (var i = 0; i < arr.length; i += 2) {
if (size === 32) {
msg += uint32ToHex(arr[i]).toUpperCase()
msg += ' '
msg += uint32ToHex(arr[i + 1]).toUpperCase()
} else if (size === 64) {
msg += uint32ToHex(arr[i + 1]).toUpperCase()
msg += uint32ToHex(arr[i]).toUpperCase()
} else throw new Error('Invalid size ' + size)
if (i % 6 === 4) {
msg += '\n' + new Array(label.length + 4).join(' ')
} else if (i < arr.length - 2) {
msg += ' '
}
}
console.log(msg)
}
// For performance testing: generates N bytes of input, hashes M times
// Measures and prints MB/second hash performance each time
function testSpeed (hashFn, N, M) {
var startMs = new Date().getTime()
var input = new Uint8Array(N)
for (var i = 0; i < N; i++) {
input[i] = i % 256
}
var genMs = new Date().getTime()
console.log('Generated random input in ' + (genMs - startMs) + 'ms')
startMs = genMs
for (i = 0; i < M; i++) {
var hashHex = hashFn(input)
var hashMs = new Date().getTime()
var ms = hashMs - startMs
startMs = hashMs
console.log('Hashed in ' + ms + 'ms: ' + hashHex.substring(0, 20) + '...')
console.log(Math.round(N / (1 << 20) / (ms / 1000) * 100) / 100 + ' MB PER SECOND')
}
}
module.exports = {
normalizeInput: normalizeInput,
toHex: toHex,
debugPrint: debugPrint,
testSpeed: testSpeed
}