Skip to content

Commit

Permalink
Add safe-buffer to dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Jun 7, 2017
1 parent 6d60872 commit 9fdabd3
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 16 deletions.
3 changes: 2 additions & 1 deletion lib/api/keccak.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
var Buffer = require('safe-buffer').Buffer
var Transform = require('stream').Transform
var inherits = require('inherits')

Expand Down Expand Up @@ -44,7 +45,7 @@ module.exports = function (KeccakState) {
Keccak.prototype.update = function (data, encoding) {
if (!Buffer.isBuffer(data) && typeof data !== 'string') throw new TypeError('Data must be a string or a buffer')
if (this._finalized) throw new Error('Digest already called')
if (!Buffer.isBuffer(data)) data = new Buffer(data, encoding)
if (!Buffer.isBuffer(data)) data = Buffer.from(data, encoding)

this._state.absorb(data)

Expand Down
3 changes: 2 additions & 1 deletion lib/api/shake.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
var Buffer = require('safe-buffer').Buffer
var Transform = require('stream').Transform
var inherits = require('inherits')

Expand Down Expand Up @@ -38,7 +39,7 @@ module.exports = function (KeccakState) {
Shake.prototype.update = function (data, encoding) {
if (!Buffer.isBuffer(data) && typeof data !== 'string') throw new TypeError('Data must be a string or a buffer')
if (this._finalized) throw new Error('Squeeze already called')
if (!Buffer.isBuffer(data)) data = new Buffer(data, encoding)
if (!Buffer.isBuffer(data)) data = Buffer.from(data, encoding)

this._state.absorb(data)

Expand Down
3 changes: 2 additions & 1 deletion lib/keccak.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
var Buffer = require('safe-buffer').Buffer
var keccakState = require('./keccak-state-unroll')

function Keccak () {
Expand Down Expand Up @@ -46,7 +47,7 @@ Keccak.prototype.absorbLastFewBits = function (bits) {
Keccak.prototype.squeeze = function (length) {
if (!this.squeezing) this.absorbLastFewBits(0x01)

var output = new Buffer(length)
var output = Buffer.alloc(length)
for (var i = 0; i < length; ++i) {
output[i] = (this.state[~~(this.count / 4)] >>> (8 * (this.count % 4))) & 0xff
this.count += 1
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@
"bindings": "^1.2.1",
"inherits": "^2.0.3",
"nan": "^2.2.1",
"prebuild-install": "^2.0.0"
"prebuild-install": "^2.0.0",
"safe-buffer": "^5.1.0"
},
"devDependencies": {
"browserify": "^14.1.0",
"nyc": "^10.0.0",
"nyc": "^11.0.2",
"prebuild": "^6.0.0",
"prebuild-ci": "^2.2.0",
"proxyquire": "^1.7.10",
"standard": "^9.0.2",
"standard": "^10.0.2",
"tape": "^4.5.1"
},
"engines": {
Expand Down
7 changes: 4 additions & 3 deletions test/api-sha3.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
const Buffer = require('safe-buffer').Buffer
const test = require('tape')

const utf8text = 'УТФ-8 text'
Expand All @@ -23,7 +24,7 @@ module.exports = (name, createHash) => {

t.plan(1)
hash.update = () => { throw err }
hash._transform(new Buffer(0), 'buffer', (_err) => t.true(_err === err))
hash._transform(Buffer.alloc(0), 'buffer', (_err) => t.true(_err === err))
t.end()
})

Expand All @@ -33,7 +34,7 @@ module.exports = (name, createHash) => {
test(`${name} Keccak#_flush`, (t) => {
t.test('should use Keccak#digest', (t) => {
const hash = createHash('sha3-256')
const buffer = new Buffer(0)
const buffer = Buffer.alloc(0)

t.plan(2)
hash.push = (data) => t.true(data === buffer)
Expand Down Expand Up @@ -78,7 +79,7 @@ module.exports = (name, createHash) => {
t.test('should return `this`', (t) => {
const hash = createHash('sha3-256')

t.same(hash.update(new Buffer(0)), hash)
t.same(hash.update(Buffer.alloc(0)), hash)
t.end()
})

Expand Down
9 changes: 5 additions & 4 deletions test/api-shake.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
const Buffer = require('safe-buffer').Buffer
const test = require('tape')
const stream = require('stream')

Expand All @@ -24,7 +25,7 @@ module.exports = (name, createHash) => {

t.plan(1)
hash.update = () => { throw err }
hash._transform(new Buffer(0), 'buffer', (_err) => t.true(_err === err))
hash._transform(Buffer.alloc(0), 'buffer', (_err) => t.true(_err === err))
t.end()
})

Expand All @@ -42,7 +43,7 @@ module.exports = (name, createHash) => {
}
})

let data = new Buffer(0)
let data = Buffer.alloc(0)
const squeezed = '46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762f'
const dst = stream.Writable({
write (chunk, encoding, callback) {
Expand Down Expand Up @@ -84,7 +85,7 @@ module.exports = (name, createHash) => {
t.test('should return `this`', (t) => {
const hash = createHash('shake256')

t.same(hash.update(new Buffer(0)), hash)
t.same(hash.update(Buffer.alloc(0)), hash)
t.end()
})

Expand Down Expand Up @@ -128,7 +129,7 @@ module.exports = (name, createHash) => {

const squeezed1 = hash1.squeeze(32, 'hex')
t.throws(() => {
hash1.update(new Buffer(0))
hash1.update(Buffer.alloc(0))
}, /^Error: Squeeze already called$/)
t.equal(hash2.squeeze(32, 'hex'), squeezed1)

Expand Down
5 changes: 3 additions & 2 deletions test/js-keccak.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
const Buffer = require('safe-buffer').Buffer
const test = require('tape')
const proxyquire = require('proxyquire')

Expand All @@ -18,7 +19,7 @@ function to32bin (x) {
test('absorb', (t) => {
const keccak = new Keccak()
keccak.initialize(56, 1544)
keccak.absorb(new Buffer(10).fill(0x01))
keccak.absorb(Buffer.alloc(10, 0x01))

t.equal(keccak.state[0], (0x01 << 24))
t.equal(keccak.state[1], (0x01 << 16) | (0x01 << 8) | (0x01 << 0))
Expand Down Expand Up @@ -48,7 +49,7 @@ test('absorbLastFewBits', (t) => {
test('squeeze', (t) => {
const keccak = new Keccak()
keccak.initialize(56, 1544)
keccak.absorb(new Buffer(5).fill(0x01))
keccak.absorb(Buffer.alloc(5, 0x01))

t.equal(keccak.squeeze(7).toString('hex'), '01010101010180')
t.equal(keccak.squeeze(3).toString('hex'), '010101')
Expand Down
3 changes: 2 additions & 1 deletion test/vectors-keccak.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
const Buffer = require('safe-buffer').Buffer
const test = require('tape')

const vectors = [{
Expand All @@ -12,7 +13,7 @@ const vectors = [{
module.exports = (name, createHash) => {
for (let i = 0; i < vectors.length; ++i) {
const vector = vectors[i]
const data = new Buffer(vector.input, 'hex')
const data = Buffer.from(vector.input, 'hex')

for (let hash of ['keccak224', 'keccak256', 'keccak384', 'keccak512']) {
test(`${name} ${hash} vector#${i}`, (t) => {
Expand Down

0 comments on commit 9fdabd3

Please sign in to comment.