summaryrefslogtreecommitdiff
path: root/test/parallel/test-crypto-hmac.js
diff options
context:
space:
mode:
Diffstat (limited to 'test/parallel/test-crypto-hmac.js')
-rw-r--r--test/parallel/test-crypto-hmac.js88
1 files changed, 37 insertions, 51 deletions
diff --git a/test/parallel/test-crypto-hmac.js b/test/parallel/test-crypto-hmac.js
index f21db29d36..9e0e364a4f 100644
--- a/test/parallel/test-crypto-hmac.js
+++ b/test/parallel/test-crypto-hmac.js
@@ -36,20 +36,37 @@ common.expectsError(
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
- message: 'The "key" argument must be one of type string, TypedArray, or ' +
- 'DataView. Received type object'
+ message: 'The "key" argument must be one of type Buffer, TypedArray, ' +
+ 'DataView, string, or KeyObject. Received type object'
});
+function testHmac(algo, key, data, expected) {
+ // FIPS does not support MD5.
+ if (common.hasFipsCrypto && algo === 'md5')
+ return;
+
+ if (!Array.isArray(data))
+ data = [data];
+
+ // If the key is a Buffer, test Hmac with a key object as well.
+ const keyWrappers = [
+ (key) => key,
+ ...(typeof key === 'string' ? [] : [crypto.createSecretKey])
+ ];
+
+ for (const keyWrapper of keyWrappers) {
+ const hmac = crypto.createHmac(algo, keyWrapper(key));
+ for (const chunk of data)
+ hmac.update(chunk);
+ const actual = hmac.digest('hex');
+ assert.strictEqual(actual, expected);
+ }
+}
+
{
- // Test HMAC
- const actual = crypto.createHmac('sha1', 'Node')
- .update('some data')
- .update('to hmac')
- .digest('hex');
- const expected = '19fd6e1ba73d9ed2224dd5094a71babe85d9a892';
- assert.strictEqual(actual,
- expected,
- `Test HMAC: ${actual} must be ${expected}`);
+ // Test HMAC with multiple updates.
+ testHmac('sha1', 'Node', ['some data', 'to hmac'],
+ '19fd6e1ba73d9ed2224dd5094a71babe85d9a892');
}
// Test HMAC (Wikipedia Test Cases)
@@ -96,24 +113,11 @@ const wikipedia = [
},
];
-for (let i = 0, l = wikipedia.length; i < l; i++) {
- for (const hash in wikipedia[i].hmac) {
- // FIPS does not support MD5.
- if (common.hasFipsCrypto && hash === 'md5')
- continue;
- const expected = wikipedia[i].hmac[hash];
- const actual = crypto.createHmac(hash, wikipedia[i].key)
- .update(wikipedia[i].data)
- .digest('hex');
- assert.strictEqual(
- actual,
- expected,
- `Test HMAC-${hash} wikipedia case ${i + 1}: ${actual} must be ${expected}`
- );
- }
+for (const { key, data, hmac } of wikipedia) {
+ for (const hash in hmac)
+ testHmac(hash, key, data, hmac[hash]);
}
-
// Test HMAC-SHA-* (rfc 4231 Test Cases)
const rfc4231 = [
{
@@ -342,6 +346,10 @@ const rfc2202_md5 = [
hmac: '6f630fad67cda0ee1fb1f562db3aa53e'
}
];
+
+for (const { key, data, hmac } of rfc2202_md5)
+ testHmac('md5', key, data, hmac);
+
const rfc2202_sha1 = [
{
key: Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'),
@@ -397,30 +405,8 @@ const rfc2202_sha1 = [
}
];
-if (!common.hasFipsCrypto) {
- for (let i = 0, l = rfc2202_md5.length; i < l; i++) {
- const actual = crypto.createHmac('md5', rfc2202_md5[i].key)
- .update(rfc2202_md5[i].data)
- .digest('hex');
- const expected = rfc2202_md5[i].hmac;
- assert.strictEqual(
- actual,
- expected,
- `Test HMAC-MD5 rfc 2202 case ${i + 1}: ${actual} must be ${expected}`
- );
- }
-}
-for (let i = 0, l = rfc2202_sha1.length; i < l; i++) {
- const actual = crypto.createHmac('sha1', rfc2202_sha1[i].key)
- .update(rfc2202_sha1[i].data)
- .digest('hex');
- const expected = rfc2202_sha1[i].hmac;
- assert.strictEqual(
- actual,
- expected,
- `Test HMAC-SHA1 rfc 2202 case ${i + 1}: ${actual} must be ${expected}`
- );
-}
+for (const { key, data, hmac } of rfc2202_sha1)
+ testHmac('sha1', key, data, hmac);
common.expectsError(
() => crypto.createHmac('sha256', 'w00t').digest('ucs2'),