зеркало из https://github.com/github/blakejs.git
Supports Buffers and UTF8 strings, version 0.4.5
This commit is contained in:
Родитель
5c3d24029e
Коммит
b975e1df1d
|
@ -28,7 +28,8 @@ Pass it a `Uint8Array` containing bytes to hash, and it will return a `Uint8Arra
|
|||
// Returns a n-byte Uint8Array
|
||||
//
|
||||
// Parameters:
|
||||
// - input - the input bytes, as a Uint8Array or ASCII string
|
||||
// - input - the input bytes, as a string, Buffer, or Uint8Array
|
||||
// Strings are converted to UTF8 bytes
|
||||
// - key - optional key Uint8Array, up to 64 bytes
|
||||
// - outlen - optional output length in bytes, default 64
|
||||
function blake2b(input, key, outlen) {
|
||||
|
|
|
@ -237,7 +237,7 @@ function blake2b_final (ctx) {
|
|||
// Returns a n-byte Uint8Array
|
||||
//
|
||||
// Parameters:
|
||||
// - input - the input bytes, as a Uint8Array or ASCII string
|
||||
// - 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) {
|
||||
|
@ -256,7 +256,7 @@ function blake2b (input, key, outlen) {
|
|||
// Returns an n-byte hash in hex, all lowercase
|
||||
//
|
||||
// Parameters:
|
||||
// - input - the input bytes, as a Uint8Array or ASCII string
|
||||
// - 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) {
|
||||
|
|
|
@ -154,7 +154,7 @@ function blake2s_final (ctx) {
|
|||
// Returns a n-byte Uint8Array
|
||||
//
|
||||
// Parameters:
|
||||
// - input - the input bytes, as a Uint8Array or ASCII string
|
||||
// - 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) {
|
||||
|
@ -173,7 +173,7 @@ function blake2s (input, key, outlen) {
|
|||
// Returns an n-byte hash in hex, all lowercase
|
||||
//
|
||||
// Parameters:
|
||||
// - input - the input bytes, as a Uint8Array or ASCII string
|
||||
// - 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) {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "blakejs",
|
||||
"version": "0.4.4",
|
||||
"version": "0.4.5",
|
||||
"description": "Pure Javascript implementation of the BLAKE2b and BLAKE2s hash functions",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
|
|
|
@ -10,9 +10,6 @@ test('BLAKE2b basic', function (t) {
|
|||
t.equal('ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1' +
|
||||
'7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923',
|
||||
blake2bHex('abc'))
|
||||
t.equal('ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1' +
|
||||
'7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923',
|
||||
blake2bHex(new Uint8Array([97, 98, 99])))
|
||||
|
||||
t.equal('786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419' +
|
||||
'd25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce',
|
||||
|
@ -25,6 +22,14 @@ test('BLAKE2b basic', function (t) {
|
|||
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('abc'), blake2bHex(new Uint8Array([97, 98, 99])))
|
||||
t.equal(blake2bHex('abc'), blake2bHex(new Buffer([97, 98, 99])))
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('BLAKE2b generated test vectors', function (t) {
|
||||
var contents = fs.readFileSync('generated_test_vectors.txt', 'utf8')
|
||||
contents.split('\n').forEach(function (line) {
|
||||
|
|
12
util.js
12
util.js
|
@ -1,18 +1,14 @@
|
|||
var ERROR_MSG_INPUT = 'Input must be an ASCII string or Uint8Array'
|
||||
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(input.length)
|
||||
for (var i = 0; i < input.length; i++) {
|
||||
if (input.charCodeAt(i) > 255) {
|
||||
throw new Error(ERROR_MSG_INPUT)
|
||||
}
|
||||
ret[i] = input.charCodeAt(i)
|
||||
}
|
||||
ret = new Uint8Array(new Buffer(input, 'utf8'))
|
||||
} else {
|
||||
throw new Error(ERROR_MSG_INPUT)
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче