11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciif (!common.hasCrypto) 41cb0ef41Sopenharmony_ci common.skip('missing crypto'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst assert = require('assert'); 71cb0ef41Sopenharmony_ciconst crypto = require('crypto'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci{ 101cb0ef41Sopenharmony_ci const Hmac = crypto.Hmac; 111cb0ef41Sopenharmony_ci const instance = crypto.Hmac('sha256', 'Node'); 121cb0ef41Sopenharmony_ci assert(instance instanceof Hmac, 'Hmac is expected to return a new instance' + 131cb0ef41Sopenharmony_ci ' when called without `new`'); 141cb0ef41Sopenharmony_ci} 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciassert.throws( 171cb0ef41Sopenharmony_ci () => crypto.createHmac(null), 181cb0ef41Sopenharmony_ci { 191cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 201cb0ef41Sopenharmony_ci name: 'TypeError', 211cb0ef41Sopenharmony_ci message: 'The "hmac" argument must be of type string. Received null' 221cb0ef41Sopenharmony_ci }); 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci// This used to segfault. See: https://github.com/nodejs/node/issues/9819 251cb0ef41Sopenharmony_ciassert.throws( 261cb0ef41Sopenharmony_ci () => crypto.createHmac('sha256', 'key').digest({ 271cb0ef41Sopenharmony_ci toString: () => { throw new Error('boom'); }, 281cb0ef41Sopenharmony_ci }), 291cb0ef41Sopenharmony_ci { 301cb0ef41Sopenharmony_ci name: 'Error', 311cb0ef41Sopenharmony_ci message: 'boom' 321cb0ef41Sopenharmony_ci }); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ciassert.throws( 351cb0ef41Sopenharmony_ci () => crypto.createHmac('sha1', null), 361cb0ef41Sopenharmony_ci { 371cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 381cb0ef41Sopenharmony_ci name: 'TypeError', 391cb0ef41Sopenharmony_ci }); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_cifunction testHmac(algo, key, data, expected) { 421cb0ef41Sopenharmony_ci // FIPS does not support MD5. 431cb0ef41Sopenharmony_ci if (common.hasFipsCrypto && algo === 'md5') 441cb0ef41Sopenharmony_ci return; 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci if (!Array.isArray(data)) 471cb0ef41Sopenharmony_ci data = [data]; 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci // If the key is a Buffer, test Hmac with a key object as well. 501cb0ef41Sopenharmony_ci const keyWrappers = [ 511cb0ef41Sopenharmony_ci (key) => key, 521cb0ef41Sopenharmony_ci ...(typeof key === 'string' ? [] : [crypto.createSecretKey]), 531cb0ef41Sopenharmony_ci ]; 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci for (const keyWrapper of keyWrappers) { 561cb0ef41Sopenharmony_ci const hmac = crypto.createHmac(algo, keyWrapper(key)); 571cb0ef41Sopenharmony_ci for (const chunk of data) 581cb0ef41Sopenharmony_ci hmac.update(chunk); 591cb0ef41Sopenharmony_ci const actual = hmac.digest('hex'); 601cb0ef41Sopenharmony_ci assert.strictEqual(actual, expected); 611cb0ef41Sopenharmony_ci } 621cb0ef41Sopenharmony_ci} 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci{ 651cb0ef41Sopenharmony_ci // Test HMAC with multiple updates. 661cb0ef41Sopenharmony_ci testHmac('sha1', 'Node', ['some data', 'to hmac'], 671cb0ef41Sopenharmony_ci '19fd6e1ba73d9ed2224dd5094a71babe85d9a892'); 681cb0ef41Sopenharmony_ci} 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci// Test HMAC (Wikipedia Test Cases) 711cb0ef41Sopenharmony_ciconst wikipedia = [ 721cb0ef41Sopenharmony_ci { 731cb0ef41Sopenharmony_ci key: 'key', data: 'The quick brown fox jumps over the lazy dog', 741cb0ef41Sopenharmony_ci hmac: { // HMACs lifted from Wikipedia. 751cb0ef41Sopenharmony_ci md5: '80070713463e7749b90c2dc24911e275', 761cb0ef41Sopenharmony_ci sha1: 'de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9', 771cb0ef41Sopenharmony_ci sha256: 781cb0ef41Sopenharmony_ci 'f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc' + 791cb0ef41Sopenharmony_ci '2d1a3cd8' 801cb0ef41Sopenharmony_ci } 811cb0ef41Sopenharmony_ci }, 821cb0ef41Sopenharmony_ci { 831cb0ef41Sopenharmony_ci key: 'key', data: '', 841cb0ef41Sopenharmony_ci hmac: { // Intermediate test to help debugging. 851cb0ef41Sopenharmony_ci md5: '63530468a04e386459855da0063b6596', 861cb0ef41Sopenharmony_ci sha1: 'f42bb0eeb018ebbd4597ae7213711ec60760843f', 871cb0ef41Sopenharmony_ci sha256: 881cb0ef41Sopenharmony_ci '5d5d139563c95b5967b9bd9a8c9b233a9dedb45072794cd232dc1b74' + 891cb0ef41Sopenharmony_ci '832607d0' 901cb0ef41Sopenharmony_ci } 911cb0ef41Sopenharmony_ci }, 921cb0ef41Sopenharmony_ci { 931cb0ef41Sopenharmony_ci key: '', data: 'The quick brown fox jumps over the lazy dog', 941cb0ef41Sopenharmony_ci hmac: { // Intermediate test to help debugging. 951cb0ef41Sopenharmony_ci md5: 'ad262969c53bc16032f160081c4a07a0', 961cb0ef41Sopenharmony_ci sha1: '2ba7f707ad5f187c412de3106583c3111d668de8', 971cb0ef41Sopenharmony_ci sha256: 981cb0ef41Sopenharmony_ci 'fb011e6154a19b9a4c767373c305275a5a69e8b68b0b4c9200c383dc' + 991cb0ef41Sopenharmony_ci 'ed19a416' 1001cb0ef41Sopenharmony_ci } 1011cb0ef41Sopenharmony_ci }, 1021cb0ef41Sopenharmony_ci { 1031cb0ef41Sopenharmony_ci key: '', data: '', 1041cb0ef41Sopenharmony_ci hmac: { // HMACs lifted from Wikipedia. 1051cb0ef41Sopenharmony_ci md5: '74e6f7298a9c2d168935f58c001bad88', 1061cb0ef41Sopenharmony_ci sha1: 'fbdb1d1b18aa6c08324b7d64b71fb76370690e1d', 1071cb0ef41Sopenharmony_ci sha256: 1081cb0ef41Sopenharmony_ci 'b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c71214' + 1091cb0ef41Sopenharmony_ci '4292c5ad' 1101cb0ef41Sopenharmony_ci } 1111cb0ef41Sopenharmony_ci }, 1121cb0ef41Sopenharmony_ci]; 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_cifor (const { key, data, hmac } of wikipedia) { 1151cb0ef41Sopenharmony_ci for (const hash in hmac) 1161cb0ef41Sopenharmony_ci testHmac(hash, key, data, hmac[hash]); 1171cb0ef41Sopenharmony_ci} 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci// Test HMAC-SHA-* (rfc 4231 Test Cases) 1201cb0ef41Sopenharmony_ciconst rfc4231 = [ 1211cb0ef41Sopenharmony_ci { 1221cb0ef41Sopenharmony_ci key: Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'), 1231cb0ef41Sopenharmony_ci data: Buffer.from('4869205468657265', 'hex'), // 'Hi There' 1241cb0ef41Sopenharmony_ci hmac: { 1251cb0ef41Sopenharmony_ci sha224: '896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22', 1261cb0ef41Sopenharmony_ci sha256: 1271cb0ef41Sopenharmony_ci 'b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c' + 1281cb0ef41Sopenharmony_ci '2e32cff7', 1291cb0ef41Sopenharmony_ci sha384: 1301cb0ef41Sopenharmony_ci 'afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c' + 1311cb0ef41Sopenharmony_ci '7cebc59cfaea9ea9076ede7f4af152e8b2fa9cb6', 1321cb0ef41Sopenharmony_ci sha512: 1331cb0ef41Sopenharmony_ci '87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b305' + 1341cb0ef41Sopenharmony_ci '45e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f170' + 1351cb0ef41Sopenharmony_ci '2e696c203a126854' 1361cb0ef41Sopenharmony_ci } 1371cb0ef41Sopenharmony_ci }, 1381cb0ef41Sopenharmony_ci { 1391cb0ef41Sopenharmony_ci key: Buffer.from('4a656665', 'hex'), // 'Jefe' 1401cb0ef41Sopenharmony_ci data: Buffer.from('7768617420646f2079612077616e7420666f72206e6f74686' + 1411cb0ef41Sopenharmony_ci '96e673f', 'hex'), // 'what do ya want for nothing?' 1421cb0ef41Sopenharmony_ci hmac: { 1431cb0ef41Sopenharmony_ci sha224: 'a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44', 1441cb0ef41Sopenharmony_ci sha256: 1451cb0ef41Sopenharmony_ci '5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b9' + 1461cb0ef41Sopenharmony_ci '64ec3843', 1471cb0ef41Sopenharmony_ci sha384: 1481cb0ef41Sopenharmony_ci 'af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec373' + 1491cb0ef41Sopenharmony_ci '6322445e8e2240ca5e69e2c78b3239ecfab21649', 1501cb0ef41Sopenharmony_ci sha512: 1511cb0ef41Sopenharmony_ci '164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7' + 1521cb0ef41Sopenharmony_ci 'ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b' + 1531cb0ef41Sopenharmony_ci '636e070a38bce737' 1541cb0ef41Sopenharmony_ci } 1551cb0ef41Sopenharmony_ci }, 1561cb0ef41Sopenharmony_ci { 1571cb0ef41Sopenharmony_ci key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'hex'), 1581cb0ef41Sopenharmony_ci data: Buffer.from('ddddddddddddddddddddddddddddddddddddddddddddddddd' + 1591cb0ef41Sopenharmony_ci 'ddddddddddddddddddddddddddddddddddddddddddddddddddd', 1601cb0ef41Sopenharmony_ci 'hex'), 1611cb0ef41Sopenharmony_ci hmac: { 1621cb0ef41Sopenharmony_ci sha224: '7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea', 1631cb0ef41Sopenharmony_ci sha256: 1641cb0ef41Sopenharmony_ci '773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514' + 1651cb0ef41Sopenharmony_ci 'ced565fe', 1661cb0ef41Sopenharmony_ci sha384: 1671cb0ef41Sopenharmony_ci '88062608d3e6ad8a0aa2ace014c8a86f0aa635d947ac9febe83ef4e5' + 1681cb0ef41Sopenharmony_ci '5966144b2a5ab39dc13814b94e3ab6e101a34f27', 1691cb0ef41Sopenharmony_ci sha512: 1701cb0ef41Sopenharmony_ci 'fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33' + 1711cb0ef41Sopenharmony_ci 'b2279d39bf3e848279a722c806b485a47e67c807b946a337bee89426' + 1721cb0ef41Sopenharmony_ci '74278859e13292fb' 1731cb0ef41Sopenharmony_ci } 1741cb0ef41Sopenharmony_ci }, 1751cb0ef41Sopenharmony_ci { 1761cb0ef41Sopenharmony_ci key: Buffer.from('0102030405060708090a0b0c0d0e0f10111213141516171819', 1771cb0ef41Sopenharmony_ci 'hex'), 1781cb0ef41Sopenharmony_ci data: Buffer.from('cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdc' + 1791cb0ef41Sopenharmony_ci 'dcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd', 1801cb0ef41Sopenharmony_ci 'hex'), 1811cb0ef41Sopenharmony_ci hmac: { 1821cb0ef41Sopenharmony_ci sha224: '6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a', 1831cb0ef41Sopenharmony_ci sha256: 1841cb0ef41Sopenharmony_ci '82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff4' + 1851cb0ef41Sopenharmony_ci '6729665b', 1861cb0ef41Sopenharmony_ci sha384: 1871cb0ef41Sopenharmony_ci '3e8a69b7783c25851933ab6290af6ca77a9981480850009cc5577c6e' + 1881cb0ef41Sopenharmony_ci '1f573b4e6801dd23c4a7d679ccf8a386c674cffb', 1891cb0ef41Sopenharmony_ci sha512: 1901cb0ef41Sopenharmony_ci 'b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050' + 1911cb0ef41Sopenharmony_ci '361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2d' + 1921cb0ef41Sopenharmony_ci 'e2adebeb10a298dd' 1931cb0ef41Sopenharmony_ci } 1941cb0ef41Sopenharmony_ci }, 1951cb0ef41Sopenharmony_ci 1961cb0ef41Sopenharmony_ci { 1971cb0ef41Sopenharmony_ci key: Buffer.from('0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c', 'hex'), 1981cb0ef41Sopenharmony_ci // 'Test With Truncation' 1991cb0ef41Sopenharmony_ci data: Buffer.from('546573742057697468205472756e636174696f6e', 'hex'), 2001cb0ef41Sopenharmony_ci hmac: { 2011cb0ef41Sopenharmony_ci sha224: '0e2aea68a90c8d37c988bcdb9fca6fa8', 2021cb0ef41Sopenharmony_ci sha256: 'a3b6167473100ee06e0c796c2955552b', 2031cb0ef41Sopenharmony_ci sha384: '3abf34c3503b2a23a46efc619baef897', 2041cb0ef41Sopenharmony_ci sha512: '415fad6271580a531d4179bc891d87a6' 2051cb0ef41Sopenharmony_ci }, 2061cb0ef41Sopenharmony_ci truncate: true 2071cb0ef41Sopenharmony_ci }, 2081cb0ef41Sopenharmony_ci { 2091cb0ef41Sopenharmony_ci key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 2101cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 2111cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 2121cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 2131cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 2141cb0ef41Sopenharmony_ci 'aaaaaaaaaaaa', 'hex'), 2151cb0ef41Sopenharmony_ci // 'Test Using Larger Than Block-Size Key - Hash Key First' 2161cb0ef41Sopenharmony_ci data: Buffer.from('54657374205573696e67204c6172676572205468616e20426' + 2171cb0ef41Sopenharmony_ci 'c6f636b2d53697a65204b6579202d2048617368204b657920' + 2181cb0ef41Sopenharmony_ci '4669727374', 'hex'), 2191cb0ef41Sopenharmony_ci hmac: { 2201cb0ef41Sopenharmony_ci sha224: '95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e', 2211cb0ef41Sopenharmony_ci sha256: 2221cb0ef41Sopenharmony_ci '60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f' + 2231cb0ef41Sopenharmony_ci '0ee37f54', 2241cb0ef41Sopenharmony_ci sha384: 2251cb0ef41Sopenharmony_ci '4ece084485813e9088d2c63a041bc5b44f9ef1012a2b588f3cd11f05' + 2261cb0ef41Sopenharmony_ci '033ac4c60c2ef6ab4030fe8296248df163f44952', 2271cb0ef41Sopenharmony_ci sha512: 2281cb0ef41Sopenharmony_ci '80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b0137' + 2291cb0ef41Sopenharmony_ci '83f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec' + 2301cb0ef41Sopenharmony_ci '8b915a985d786598' 2311cb0ef41Sopenharmony_ci } 2321cb0ef41Sopenharmony_ci }, 2331cb0ef41Sopenharmony_ci { 2341cb0ef41Sopenharmony_ci key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 2351cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 2361cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 2371cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 2381cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 2391cb0ef41Sopenharmony_ci 'aaaaaaaaaaaa', 'hex'), 2401cb0ef41Sopenharmony_ci // 'This is a test using a larger than block-size key and a larger ' + 2411cb0ef41Sopenharmony_ci // 'than block-size data. The key needs to be hashed before being ' + 2421cb0ef41Sopenharmony_ci // 'used by the HMAC algorithm.' 2431cb0ef41Sopenharmony_ci data: Buffer.from('5468697320697320612074657374207573696e672061206c6' + 2441cb0ef41Sopenharmony_ci '172676572207468616e20626c6f636b2d73697a65206b6579' + 2451cb0ef41Sopenharmony_ci '20616e642061206c6172676572207468616e20626c6f636b2' + 2461cb0ef41Sopenharmony_ci 'd73697a6520646174612e20546865206b6579206e65656473' + 2471cb0ef41Sopenharmony_ci '20746f20626520686173686564206265666f7265206265696' + 2481cb0ef41Sopenharmony_ci 'e6720757365642062792074686520484d414320616c676f72' + 2491cb0ef41Sopenharmony_ci '6974686d2e', 'hex'), 2501cb0ef41Sopenharmony_ci hmac: { 2511cb0ef41Sopenharmony_ci sha224: '3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1', 2521cb0ef41Sopenharmony_ci sha256: 2531cb0ef41Sopenharmony_ci '9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f5153' + 2541cb0ef41Sopenharmony_ci '5c3a35e2', 2551cb0ef41Sopenharmony_ci sha384: 2561cb0ef41Sopenharmony_ci '6617178e941f020d351e2f254e8fd32c602420feb0b8fb9adccebb82' + 2571cb0ef41Sopenharmony_ci '461e99c5a678cc31e799176d3860e6110c46523e', 2581cb0ef41Sopenharmony_ci sha512: 2591cb0ef41Sopenharmony_ci 'e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d' + 2601cb0ef41Sopenharmony_ci '20cdc944b6022cac3c4982b10d5eeb55c3e4de15134676fb6de04460' + 2611cb0ef41Sopenharmony_ci '65c97440fa8c6a58' 2621cb0ef41Sopenharmony_ci } 2631cb0ef41Sopenharmony_ci }, 2641cb0ef41Sopenharmony_ci]; 2651cb0ef41Sopenharmony_ci 2661cb0ef41Sopenharmony_cifor (let i = 0, l = rfc4231.length; i < l; i++) { 2671cb0ef41Sopenharmony_ci for (const hash in rfc4231[i].hmac) { 2681cb0ef41Sopenharmony_ci const str = crypto.createHmac(hash, rfc4231[i].key); 2691cb0ef41Sopenharmony_ci str.end(rfc4231[i].data); 2701cb0ef41Sopenharmony_ci let strRes = str.read().toString('hex'); 2711cb0ef41Sopenharmony_ci let actual = crypto.createHmac(hash, rfc4231[i].key) 2721cb0ef41Sopenharmony_ci .update(rfc4231[i].data) 2731cb0ef41Sopenharmony_ci .digest('hex'); 2741cb0ef41Sopenharmony_ci if (rfc4231[i].truncate) { 2751cb0ef41Sopenharmony_ci actual = actual.substr(0, 32); // first 128 bits == 32 hex chars 2761cb0ef41Sopenharmony_ci strRes = strRes.substr(0, 32); 2771cb0ef41Sopenharmony_ci } 2781cb0ef41Sopenharmony_ci const expected = rfc4231[i].hmac[hash]; 2791cb0ef41Sopenharmony_ci assert.strictEqual( 2801cb0ef41Sopenharmony_ci actual, 2811cb0ef41Sopenharmony_ci expected, 2821cb0ef41Sopenharmony_ci `Test HMAC-${hash} rfc 4231 case ${i + 1}: ${actual} must be ${expected}` 2831cb0ef41Sopenharmony_ci ); 2841cb0ef41Sopenharmony_ci assert.strictEqual( 2851cb0ef41Sopenharmony_ci actual, 2861cb0ef41Sopenharmony_ci strRes, 2871cb0ef41Sopenharmony_ci `Should get same result from stream (hash: ${hash} and case: ${i + 1})` + 2881cb0ef41Sopenharmony_ci ` => ${actual} must be ${strRes}` 2891cb0ef41Sopenharmony_ci ); 2901cb0ef41Sopenharmony_ci } 2911cb0ef41Sopenharmony_ci} 2921cb0ef41Sopenharmony_ci 2931cb0ef41Sopenharmony_ci// Test HMAC-MD5/SHA1 (rfc 2202 Test Cases) 2941cb0ef41Sopenharmony_ciconst rfc2202_md5 = [ 2951cb0ef41Sopenharmony_ci { 2961cb0ef41Sopenharmony_ci key: Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'), 2971cb0ef41Sopenharmony_ci data: 'Hi There', 2981cb0ef41Sopenharmony_ci hmac: '9294727a3638bb1c13f48ef8158bfc9d' 2991cb0ef41Sopenharmony_ci }, 3001cb0ef41Sopenharmony_ci { 3011cb0ef41Sopenharmony_ci key: 'Jefe', 3021cb0ef41Sopenharmony_ci data: 'what do ya want for nothing?', 3031cb0ef41Sopenharmony_ci hmac: '750c783e6ab0b503eaa86e310a5db738' 3041cb0ef41Sopenharmony_ci }, 3051cb0ef41Sopenharmony_ci { 3061cb0ef41Sopenharmony_ci key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'hex'), 3071cb0ef41Sopenharmony_ci data: Buffer.from('ddddddddddddddddddddddddddddddddddddddddddddddddd' + 3081cb0ef41Sopenharmony_ci 'ddddddddddddddddddddddddddddddddddddddddddddddddddd', 3091cb0ef41Sopenharmony_ci 'hex'), 3101cb0ef41Sopenharmony_ci hmac: '56be34521d144c88dbb8c733f0e8b3f6' 3111cb0ef41Sopenharmony_ci }, 3121cb0ef41Sopenharmony_ci { 3131cb0ef41Sopenharmony_ci key: Buffer.from('0102030405060708090a0b0c0d0e0f10111213141516171819', 3141cb0ef41Sopenharmony_ci 'hex'), 3151cb0ef41Sopenharmony_ci data: Buffer.from('cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdc' + 3161cb0ef41Sopenharmony_ci 'dcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd' + 3171cb0ef41Sopenharmony_ci 'cdcdcdcdcd', 3181cb0ef41Sopenharmony_ci 'hex'), 3191cb0ef41Sopenharmony_ci hmac: '697eaf0aca3a3aea3a75164746ffaa79' 3201cb0ef41Sopenharmony_ci }, 3211cb0ef41Sopenharmony_ci { 3221cb0ef41Sopenharmony_ci key: Buffer.from('0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c', 'hex'), 3231cb0ef41Sopenharmony_ci data: 'Test With Truncation', 3241cb0ef41Sopenharmony_ci hmac: '56461ef2342edc00f9bab995690efd4c' 3251cb0ef41Sopenharmony_ci }, 3261cb0ef41Sopenharmony_ci { 3271cb0ef41Sopenharmony_ci key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 3281cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 3291cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 3301cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaa', 3311cb0ef41Sopenharmony_ci 'hex'), 3321cb0ef41Sopenharmony_ci data: 'Test Using Larger Than Block-Size Key - Hash Key First', 3331cb0ef41Sopenharmony_ci hmac: '6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd' 3341cb0ef41Sopenharmony_ci }, 3351cb0ef41Sopenharmony_ci { 3361cb0ef41Sopenharmony_ci key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 3371cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 3381cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 3391cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaa', 3401cb0ef41Sopenharmony_ci 'hex'), 3411cb0ef41Sopenharmony_ci data: 3421cb0ef41Sopenharmony_ci 'Test Using Larger Than Block-Size Key and Larger Than One ' + 3431cb0ef41Sopenharmony_ci 'Block-Size Data', 3441cb0ef41Sopenharmony_ci hmac: '6f630fad67cda0ee1fb1f562db3aa53e' 3451cb0ef41Sopenharmony_ci }, 3461cb0ef41Sopenharmony_ci]; 3471cb0ef41Sopenharmony_ci 3481cb0ef41Sopenharmony_cifor (const { key, data, hmac } of rfc2202_md5) 3491cb0ef41Sopenharmony_ci testHmac('md5', key, data, hmac); 3501cb0ef41Sopenharmony_ci 3511cb0ef41Sopenharmony_ciconst rfc2202_sha1 = [ 3521cb0ef41Sopenharmony_ci { 3531cb0ef41Sopenharmony_ci key: Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'), 3541cb0ef41Sopenharmony_ci data: 'Hi There', 3551cb0ef41Sopenharmony_ci hmac: 'b617318655057264e28bc0b6fb378c8ef146be00' 3561cb0ef41Sopenharmony_ci }, 3571cb0ef41Sopenharmony_ci { 3581cb0ef41Sopenharmony_ci key: 'Jefe', 3591cb0ef41Sopenharmony_ci data: 'what do ya want for nothing?', 3601cb0ef41Sopenharmony_ci hmac: 'effcdf6ae5eb2fa2d27416d5f184df9c259a7c79' 3611cb0ef41Sopenharmony_ci }, 3621cb0ef41Sopenharmony_ci { 3631cb0ef41Sopenharmony_ci key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'hex'), 3641cb0ef41Sopenharmony_ci data: Buffer.from('ddddddddddddddddddddddddddddddddddddddddddddd' + 3651cb0ef41Sopenharmony_ci 'ddddddddddddddddddddddddddddddddddddddddddddd' + 3661cb0ef41Sopenharmony_ci 'dddddddddd', 3671cb0ef41Sopenharmony_ci 'hex'), 3681cb0ef41Sopenharmony_ci hmac: '125d7342b9ac11cd91a39af48aa17b4f63f175d3' 3691cb0ef41Sopenharmony_ci }, 3701cb0ef41Sopenharmony_ci { 3711cb0ef41Sopenharmony_ci key: Buffer.from('0102030405060708090a0b0c0d0e0f10111213141516171819', 3721cb0ef41Sopenharmony_ci 'hex'), 3731cb0ef41Sopenharmony_ci data: Buffer.from('cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdc' + 3741cb0ef41Sopenharmony_ci 'dcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd' + 3751cb0ef41Sopenharmony_ci 'cdcdcdcdcd', 3761cb0ef41Sopenharmony_ci 'hex'), 3771cb0ef41Sopenharmony_ci hmac: '4c9007f4026250c6bc8414f9bf50c86c2d7235da' 3781cb0ef41Sopenharmony_ci }, 3791cb0ef41Sopenharmony_ci { 3801cb0ef41Sopenharmony_ci key: Buffer.from('0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c', 'hex'), 3811cb0ef41Sopenharmony_ci data: 'Test With Truncation', 3821cb0ef41Sopenharmony_ci hmac: '4c1a03424b55e07fe7f27be1d58bb9324a9a5a04' 3831cb0ef41Sopenharmony_ci }, 3841cb0ef41Sopenharmony_ci { 3851cb0ef41Sopenharmony_ci key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 3861cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 3871cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 3881cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaa', 3891cb0ef41Sopenharmony_ci 'hex'), 3901cb0ef41Sopenharmony_ci data: 'Test Using Larger Than Block-Size Key - Hash Key First', 3911cb0ef41Sopenharmony_ci hmac: 'aa4ae5e15272d00e95705637ce8a3b55ed402112' 3921cb0ef41Sopenharmony_ci }, 3931cb0ef41Sopenharmony_ci { 3941cb0ef41Sopenharmony_ci key: Buffer.from('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 3951cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 3961cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + 3971cb0ef41Sopenharmony_ci 'aaaaaaaaaaaaaaaaaaaaaa', 3981cb0ef41Sopenharmony_ci 'hex'), 3991cb0ef41Sopenharmony_ci data: 4001cb0ef41Sopenharmony_ci 'Test Using Larger Than Block-Size Key and Larger Than One ' + 4011cb0ef41Sopenharmony_ci 'Block-Size Data', 4021cb0ef41Sopenharmony_ci hmac: 'e8e99d0f45237d786d6bbaa7965c7808bbff1a91' 4031cb0ef41Sopenharmony_ci }, 4041cb0ef41Sopenharmony_ci]; 4051cb0ef41Sopenharmony_ci 4061cb0ef41Sopenharmony_cifor (const { key, data, hmac } of rfc2202_sha1) 4071cb0ef41Sopenharmony_ci testHmac('sha1', key, data, hmac); 4081cb0ef41Sopenharmony_ci 4091cb0ef41Sopenharmony_ciassert.strictEqual( 4101cb0ef41Sopenharmony_ci crypto.createHmac('sha256', 'w00t').digest('ucs2'), 4111cb0ef41Sopenharmony_ci crypto.createHmac('sha256', 'w00t').digest().toString('ucs2')); 4121cb0ef41Sopenharmony_ci 4131cb0ef41Sopenharmony_ci// Check initialized -> uninitialized state transition after calling digest(). 4141cb0ef41Sopenharmony_ci{ 4151cb0ef41Sopenharmony_ci const expected = 4161cb0ef41Sopenharmony_ci '\u0010\u0041\u0052\u00c5\u00bf\u00dc\u00a0\u007b\u00c6\u0033' + 4171cb0ef41Sopenharmony_ci '\u00ee\u00bd\u0046\u0019\u009f\u0002\u0055\u00c9\u00f4\u009d'; 4181cb0ef41Sopenharmony_ci { 4191cb0ef41Sopenharmony_ci const h = crypto.createHmac('sha1', 'key').update('data'); 4201cb0ef41Sopenharmony_ci assert.deepStrictEqual(h.digest('buffer'), Buffer.from(expected, 'latin1')); 4211cb0ef41Sopenharmony_ci assert.deepStrictEqual(h.digest('buffer'), Buffer.from('')); 4221cb0ef41Sopenharmony_ci } 4231cb0ef41Sopenharmony_ci { 4241cb0ef41Sopenharmony_ci const h = crypto.createHmac('sha1', 'key').update('data'); 4251cb0ef41Sopenharmony_ci assert.strictEqual(h.digest('latin1'), expected); 4261cb0ef41Sopenharmony_ci assert.strictEqual(h.digest('latin1'), ''); 4271cb0ef41Sopenharmony_ci } 4281cb0ef41Sopenharmony_ci} 4291cb0ef41Sopenharmony_ci 4301cb0ef41Sopenharmony_ci// Check initialized -> uninitialized state transition after calling digest(). 4311cb0ef41Sopenharmony_ci// Calls to update() omitted intentionally. 4321cb0ef41Sopenharmony_ci{ 4331cb0ef41Sopenharmony_ci const expected = 4341cb0ef41Sopenharmony_ci '\u00f4\u002b\u00b0\u00ee\u00b0\u0018\u00eb\u00bd\u0045\u0097' + 4351cb0ef41Sopenharmony_ci '\u00ae\u0072\u0013\u0071\u001e\u00c6\u0007\u0060\u0084\u003f'; 4361cb0ef41Sopenharmony_ci { 4371cb0ef41Sopenharmony_ci const h = crypto.createHmac('sha1', 'key'); 4381cb0ef41Sopenharmony_ci assert.deepStrictEqual(h.digest('buffer'), Buffer.from(expected, 'latin1')); 4391cb0ef41Sopenharmony_ci assert.deepStrictEqual(h.digest('buffer'), Buffer.from('')); 4401cb0ef41Sopenharmony_ci } 4411cb0ef41Sopenharmony_ci { 4421cb0ef41Sopenharmony_ci const h = crypto.createHmac('sha1', 'key'); 4431cb0ef41Sopenharmony_ci assert.strictEqual(h.digest('latin1'), expected); 4441cb0ef41Sopenharmony_ci assert.strictEqual(h.digest('latin1'), ''); 4451cb0ef41Sopenharmony_ci } 4461cb0ef41Sopenharmony_ci} 4471cb0ef41Sopenharmony_ci 4481cb0ef41Sopenharmony_ci{ 4491cb0ef41Sopenharmony_ci assert.throws( 4501cb0ef41Sopenharmony_ci () => crypto.createHmac('sha7', 'key'), 4511cb0ef41Sopenharmony_ci /Invalid digest/); 4521cb0ef41Sopenharmony_ci} 4531cb0ef41Sopenharmony_ci 4541cb0ef41Sopenharmony_ci{ 4551cb0ef41Sopenharmony_ci const buf = Buffer.alloc(0); 4561cb0ef41Sopenharmony_ci const keyObject = crypto.createSecretKey(Buffer.alloc(0)); 4571cb0ef41Sopenharmony_ci assert.deepStrictEqual( 4581cb0ef41Sopenharmony_ci crypto.createHmac('sha256', buf).update('foo').digest(), 4591cb0ef41Sopenharmony_ci crypto.createHmac('sha256', keyObject).update('foo').digest(), 4601cb0ef41Sopenharmony_ci ); 4611cb0ef41Sopenharmony_ci} 462