Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,17 @@
"max-lines-per-function": "off",
},
},
{
"files": "hash.js",
"globals": {
"Uint8Array": false,
},
},
{
"files": "test/test.js",
"globals": {
"Uint16Array": false,
},
},
],
}
6 changes: 2 additions & 4 deletions hash.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var Buffer = require('safe-buffer').Buffer;
var toBuffer = require('to-buffer');

// prototype class for hash functions
function Hash(blockSize, finalSize) {
Expand All @@ -12,10 +13,7 @@ function Hash(blockSize, finalSize) {

Hash.prototype.update = function (data, enc) {
/* eslint no-param-reassign: 0 */
if (typeof data === 'string') {
enc = enc || 'utf8';
data = Buffer.from(data, enc);
}
data = toBuffer(data, enc || 'utf8');

var block = this._block;
var blockSize = this._blockSize;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
},
"dependencies": {
"inherits": "^2.0.4",
"safe-buffer": "^5.2.1"
"safe-buffer": "^5.2.1",
"to-buffer": "^1.2.0"
},
"devDependencies": {
"@ljharb/eslint-config": "^21.1.1",
Expand Down
39 changes: 35 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ var Buffer = require('safe-buffer').Buffer;

var Sha1 = require('../').sha1;

var nodeSupportsUint16 = false;
try {
crypto.createHash('sha1').update(new Uint16Array());
nodeSupportsUint16 = true;
} catch (err) {}

var inputs = [
['', 'ascii'],
['abc', 'ascii'],
Expand All @@ -15,8 +21,10 @@ var inputs = [
['123456789abcdef123456789abcdef123456789abcdef123456789ab', 'ascii'],
['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde', 'ascii'],
['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', 'ascii'],
['foobarbaz', 'ascii']
];
['foobarbaz', 'ascii'],
[Buffer.from('buffer')],
nodeSupportsUint16 ? [new Uint16Array([1, 2, 3])] : null
].filter(Boolean);

tape("hash is the same as node's crypto", function (t) {
inputs.forEach(function (v) {
Expand All @@ -35,7 +43,7 @@ tape('call update multiple times', function (t) {
var sha1hash = crypto.createHash('sha1');

for (var i = 0; i < v[0].length; i = (i + 1) * 2) {
var s = v[0].substring(i, (i + 1) * 2);
var s = v[0].slice(i, (i + 1) * 2);
hash.update(s, v[1]);
sha1hash.update(s, v[1]);
}
Expand Down Expand Up @@ -74,7 +82,7 @@ tape('hex encoding', function (t) {
var sha1hash = crypto.createHash('sha1');

for (var i = 0; i < v[0].length; i = (i + 1) * 2) {
var s = v[0].substring(i, (i + 1) * 2);
var s = v[0].slice(i, (i + 1) * 2);
hash.update(Buffer.from(s, 'ascii').toString('hex'), 'hex');
sha1hash.update(Buffer.from(s, 'ascii').toString('hex'), 'hex');
}
Expand All @@ -88,6 +96,29 @@ tape('hex encoding', function (t) {
t.end();
});

tape('throws on invalid input', function (t) {
var invalid = [
{}, // non-arrayish
{ length: 20 }, // undefined values
[NaN], // non-numbers
[[]], // non-numbers
[1, 1.5], // non-integers
[1, 256], // out of bounds
[-1, 0] // out of bounds
];

invalid.forEach(function (input) {
var hash = new Sha1();

t['throws'](function () {
hash.update(input);
hash.digest('hex');
});
});

t.end();
});

tape('call digest for more than MAX_UINT32 bits of data', function (t) {
var sha1hash = crypto.createHash('sha1');
var hash = new Sha1();
Expand Down