Skip to content

Commit a77e3b7

Browse files
committed
1.0.0
0 parents  commit a77e3b7

26 files changed

+1769
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.nyc_output
2+
coverage
3+
build
4+
node_modules
5+
6+
npm-debug.log
7+
yarn.lock

.travis.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
sudo: false
2+
os:
3+
- linux
4+
language: node_js
5+
node_js:
6+
- "4"
7+
- "5"
8+
- "6"
9+
- "7"
10+
addons:
11+
apt:
12+
sources:
13+
- ubuntu-toolchain-r-test
14+
packages:
15+
- g++-4.8
16+
env:
17+
global:
18+
- JOBS=2
19+
matrix:
20+
- TEST_SUITE=unit
21+
matrix:
22+
fast_finish: true
23+
include:
24+
- os: linux
25+
node_js: "6"
26+
env: TEST_SUITE=lint
27+
before_install:
28+
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then export CXX=g++-4.8; fi
29+
script: npm run $TEST_SUITE

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# blake-hash
2+
3+
Version | Linux
4+
------- | ---------
5+
[![NPM Package](https://img.shields.io/npm/v/blake-hash.svg?style=flat-square)](https://www.npmjs.org/package/blake-hash) | [![Build Status](https://img.shields.io/travis/cryptocoinjs/blake-hash.svg?branch=master&style=flat-square)](https://travis-ci.org/cryptocoinjs/blake-hash)
6+
7+
[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
8+
9+
This module provides native bindings to [Blake][1] [[pdf][2]]. In browser pure JavaScript implementation will be used.
10+
11+
## Usage
12+
13+
You can use this package as [node Hash][3].
14+
15+
```js
16+
const createBlakeHash = require('blake-hash')
17+
18+
console.log(createBlakeHash('blake256').digest().toString('hex'))
19+
// => 716f6e863f744b9ac22c97ec7b76ea5f5908bc5b2f67c61510bfc4751384ea7a
20+
21+
console.log(createBlakeHash('blake256').update('Hello world!').digest('hex'))
22+
// => e0d8a3b73d07feca605c2376f5e54820cf8280af4a195d125ff5eadbf214adf3
23+
```
24+
25+
## LICENSE
26+
27+
This library is free and open-source software released under the MIT license.
28+
29+
[1]: http://131002.net/blake/
30+
[2]: http://131002.net/blake/blake.pdf
31+
[3]: https://nodejs.org/api/crypto.html#crypto_class_hash

binding.gyp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"targets": [{
3+
"target_name": "blake",
4+
"sources": [
5+
"./src/addon.cc",
6+
"./src/blake224.c",
7+
"./src/blake256.c",
8+
"./src/blake384.c",
9+
"./src/blake512.c"
10+
],
11+
"include_dirs": [
12+
"<!(node -e \"require('nan')\")"
13+
],
14+
"cflags": [
15+
"-Wall",
16+
"-Wno-maybe-uninitialized",
17+
"-Wno-uninitialized",
18+
"-Wno-unused-function",
19+
"-Wextra"
20+
]
21+
}]
22+
}

bindings.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'use strict'
2+
module.exports = require('./lib/api')(require('bindings')('blake'))

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
try {
3+
module.exports = require('./bindings')
4+
} catch (err) {
5+
if (process.env.DEBUG) {
6+
console.error('Blake bindings are not compiled. Pure JS implementation will be used.')
7+
}
8+
9+
module.exports = require('./js')
10+
}

js.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'use strict'
2+
module.exports = require('./lib/api')(require('./lib'))

lib/api/blake.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict'
2+
var Transform = require('stream').Transform
3+
var inherits = require('inherits')
4+
5+
function Blake (engine, options) {
6+
Transform.call(this, options)
7+
8+
this._engine = engine
9+
this._finalized = false
10+
}
11+
12+
inherits(Blake, Transform)
13+
14+
Blake.prototype._transform = function (chunk, encoding, callback) {
15+
var error = null
16+
try {
17+
this.update(chunk, encoding)
18+
} catch (err) {
19+
error = err
20+
}
21+
22+
callback(error)
23+
}
24+
25+
Blake.prototype._flush = function (callback) {
26+
var error = null
27+
try {
28+
this.push(this.digest())
29+
} catch (err) {
30+
error = err
31+
}
32+
33+
callback(error)
34+
}
35+
36+
Blake.prototype.update = function (data, encoding) {
37+
if (!Buffer.isBuffer(data) && typeof data !== 'string') throw new TypeError('Data must be a string or a buffer')
38+
if (this._finalized) throw new Error('Digest already called')
39+
if (!Buffer.isBuffer(data)) data = new Buffer(data, encoding)
40+
41+
this._engine.update(data)
42+
43+
return this
44+
}
45+
46+
Blake.prototype.digest = function (encoding) {
47+
if (this._finalized) throw new Error('Digest already called')
48+
this._finalized = true
49+
50+
var digest = this._engine.digest()
51+
if (encoding !== undefined) digest = digest.toString(encoding)
52+
53+
return digest
54+
}
55+
56+
module.exports = Blake

lib/api/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
var Blake = require('./blake')
3+
4+
module.exports = function (engines) {
5+
function getEngine (algorithm) {
6+
var hash = typeof algorithm === 'string' ? algorithm.toLowerCase() : algorithm
7+
switch (hash) {
8+
case 'blake224': return engines.Blake224
9+
case 'blake256': return engines.Blake256
10+
case 'blake384': return engines.Blake384
11+
case 'blake512': return engines.Blake512
12+
13+
default: throw new Error('Invald algorithm: ' + algorithm)
14+
}
15+
}
16+
17+
return function (algorithm, options) {
18+
var Engine = getEngine(algorithm)
19+
return new Blake(new Engine(), options)
20+
}
21+
}

lib/blake.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use strict'
2+
3+
function Blake () {}
4+
5+
Blake.sigma = [
6+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
7+
[14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3],
8+
[11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4],
9+
[7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8],
10+
[9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13],
11+
[2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9],
12+
[12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11],
13+
[13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10],
14+
[6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5],
15+
[10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0],
16+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
17+
[14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3],
18+
[11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4],
19+
[7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8],
20+
[9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13],
21+
[2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9]
22+
]
23+
24+
Blake.u256 = [
25+
0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344,
26+
0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
27+
0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
28+
0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917
29+
]
30+
31+
Blake.u512 = [
32+
0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344,
33+
0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
34+
0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
35+
0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917,
36+
0x9216d5d9, 0x8979fb1b, 0xd1310ba6, 0x98dfb5ac,
37+
0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96,
38+
0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7,
39+
0x0801f2e2, 0x858efc16, 0x636920d8, 0x71574e69
40+
]
41+
42+
Blake.padding = new Buffer([
43+
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
51+
])
52+
53+
Blake.prototype._length_carry = function (arr) {
54+
for (var j = 0; j < arr.length; ++j) {
55+
if (arr[j] < 0x0100000000) break
56+
arr[j] -= 0x0100000000
57+
arr[j + 1] += 1
58+
}
59+
}
60+
61+
Blake.prototype.update = function (data) {
62+
var block = this._block
63+
var offset = 0
64+
65+
while (this._blockOffset + data.length - offset >= block.length) {
66+
for (var i = this._blockOffset; i < block.length;) block[i++] = data[offset++]
67+
68+
this._length[0] += block.length * 8
69+
this._length_carry(this._length)
70+
71+
this._compress()
72+
this._blockOffset = 0
73+
}
74+
75+
while (offset < data.length) block[this._blockOffset++] = data[offset++]
76+
}
77+
78+
module.exports = Blake

0 commit comments

Comments
 (0)