|
| 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