Skip to content

Commit

Permalink
[Tests] node 17 doesn’t support rmd160
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Nov 27, 2023
1 parent 33596f6 commit 797455f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 47 deletions.
49 changes: 29 additions & 20 deletions test/create-hash.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
'use strict';

var test = require('tape');
var satisfies = require('semver/functions/satisfies');

var algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160'];
var encodings = ['hex', 'base64']; // FIXME: test binary
var vectors = require('hash-test-vectors');

function runTest(name, createHash, algorithm) {
test(name + ' test ' + algorithm + ' against test vectors', function (t) {
vectors.forEach(function (obj, i) {
var input = new Buffer(obj.input, 'base64');
var node = obj[algorithm];
var js = createHash(algorithm).update(input).digest('hex');
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node);
var isUnsupported = satisfies(process.version, '^17') && (
algorithm === 'rmd160'
|| algorithm === 'hmac(rmd160)'
);
test(
name + ' test ' + algorithm + ' against test vectors',
{ skip: isUnsupported && 'this node version does not support ' + algorithm },
function (t) {
vectors.forEach(function (obj, i) {
var input = new Buffer(obj.input, 'base64');
var node = obj[algorithm];
var js = createHash(algorithm).update(input).digest('hex');
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node);

encodings.forEach(function (encoding) {
var eInput = new Buffer(obj.input, 'base64').toString(encoding);
var eNode = obj[algorithm];
var eJS = createHash(algorithm).update(eInput, encoding).digest('hex');
t.equal(eJS, eNode, algorithm + '(testVector[' + i + '], ' + encoding + ') == ' + eNode);
encodings.forEach(function (encoding) {
var eInput = new Buffer(obj.input, 'base64').toString(encoding);
var eNode = obj[algorithm];
var eJS = createHash(algorithm).update(eInput, encoding).digest('hex');
t.equal(eJS, eNode, algorithm + '(testVector[' + i + '], ' + encoding + ') == ' + eNode);
});
input = new Buffer(obj.input, 'base64');
node = obj[algorithm];
var hash = createHash(algorithm);
hash.end(input);
js = hash.read().toString('hex');
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node);
});
input = new Buffer(obj.input, 'base64');
node = obj[algorithm];
var hash = createHash(algorithm);
hash.end(input);
js = hash.read().toString('hex');
t.equal(js, node, algorithm + '(testVector[' + i + ']) == ' + node);
});

t.end();
});
t.end();
}
);
}

function testLib(name, createHash) {
Expand Down
67 changes: 40 additions & 27 deletions test/create-hmac.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,52 @@
'use strict';

var test = require('tape');
var satisfies = require('semver/functions/satisfies');

var algorithms = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160'];
var vectors = require('hash-test-vectors/hmac');

function testLib(name, createHmac) {
algorithms.forEach(function (alg) {
test(name + ' hmac(' + alg + ')', function (t) {
vectors.forEach(function (input) {
var output = createHmac(alg, new Buffer(input.key, 'hex'))
.update(input.data, 'hex').digest();

output = input.truncate ? output.slice(0, input.truncate) : output;
output = output.toString('hex');
t.equal(output, input[alg]);
});

t.end();
});

test('hmac(' + alg + ')', function (t) {
vectors.forEach(function (input) {
var hmac = createHmac(alg, new Buffer(input.key, 'hex'));

hmac.end(input.data, 'hex');
var output = hmac.read();

output = input.truncate ? output.slice(0, input.truncate) : output;
output = output.toString('hex');
t.equal(output, input[alg]);
});

t.end();
});
var isUnsupported = satisfies(process.version, '^17') && (
alg === 'rmd160'
|| alg === 'hmac(rmd160)'
);
test(
name + ' hmac(' + alg + ')',
{ skip: isUnsupported && 'this node version does not support ' + alg },
function (t) {
vectors.forEach(function (input) {
var output = createHmac(alg, new Buffer(input.key, 'hex'))
.update(input.data, 'hex').digest();

output = input.truncate ? output.slice(0, input.truncate) : output;
output = output.toString('hex');
t.equal(output, input[alg]);
});

t.end();
}
);

test(
'hmac(' + alg + ')',
{ skip: isUnsupported && 'this node version does not support ' + alg },
function (t) {
vectors.forEach(function (input) {
var hmac = createHmac(alg, new Buffer(input.key, 'hex'));

hmac.end(input.data, 'hex');
var output = hmac.read();

output = input.truncate ? output.slice(0, input.truncate) : output;
output = output.toString('hex');
t.equal(output, input[alg]);
});

t.end();
}
);
});
}

Expand Down

0 comments on commit 797455f

Please sign in to comment.