зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1166078 - Clean up and add expected result strings to test_hmac.js. r=keeler
--HG-- extra : rebase_source : 7d8d3e38e2f706324bb4563f65a4d85b1af8866b
This commit is contained in:
Родитель
852d7d3d59
Коммит
740177a058
|
@ -1,53 +1,54 @@
|
|||
var ScriptableUnicodeConverter =
|
||||
"use strict";
|
||||
|
||||
let ScriptableUnicodeConverter =
|
||||
Components.Constructor("@mozilla.org/intl/scriptableunicodeconverter",
|
||||
"nsIScriptableUnicodeConverter");
|
||||
|
||||
function getHMAC(data, key, alg)
|
||||
{
|
||||
var converter = new ScriptableUnicodeConverter();
|
||||
converter.charset = 'utf8';
|
||||
var dataarray = converter.convertToByteArray(data);
|
||||
|
||||
var keyObject = Components.classes["@mozilla.org/security/keyobjectfactory;1"]
|
||||
.getService(Components.interfaces.nsIKeyObjectFactory)
|
||||
.keyFromString(Components.interfaces.nsIKeyObject.HMAC, key);
|
||||
|
||||
var cryptoHMAC = Components.classes["@mozilla.org/security/hmac;1"]
|
||||
.createInstance(Components.interfaces.nsICryptoHMAC);
|
||||
|
||||
"nsIScriptableUnicodeConverter");
|
||||
|
||||
function getHMAC(data, key, alg) {
|
||||
let converter = new ScriptableUnicodeConverter();
|
||||
converter.charset = "utf8";
|
||||
let dataArray = converter.convertToByteArray(data);
|
||||
|
||||
let keyObject = Cc["@mozilla.org/security/keyobjectfactory;1"]
|
||||
.getService(Ci.nsIKeyObjectFactory)
|
||||
.keyFromString(Ci.nsIKeyObject.HMAC, key);
|
||||
|
||||
let cryptoHMAC = Cc["@mozilla.org/security/hmac;1"]
|
||||
.createInstance(Ci.nsICryptoHMAC);
|
||||
|
||||
cryptoHMAC.init(alg, keyObject);
|
||||
cryptoHMAC.update(dataarray, dataarray.length);
|
||||
var digest1 = cryptoHMAC.finish(false);
|
||||
|
||||
cryptoHMAC.update(dataArray, dataArray.length);
|
||||
let digest1 = cryptoHMAC.finish(false);
|
||||
|
||||
cryptoHMAC.reset();
|
||||
cryptoHMAC.update(dataarray, dataarray.length);
|
||||
var digest2 = cryptoHMAC.finish(false);
|
||||
|
||||
do_check_eq(digest1, digest2);
|
||||
|
||||
cryptoHMAC.update(dataArray, dataArray.length);
|
||||
let digest2 = cryptoHMAC.finish(false);
|
||||
|
||||
equal(digest1, digest2,
|
||||
"Initial digest and digest after calling reset() should match");
|
||||
|
||||
return digest1;
|
||||
}
|
||||
|
||||
function testHMAC(alg) {
|
||||
const key1 = 'MyKey_ABCDEFGHIJKLMN';
|
||||
const key2 = 'MyKey_01234567890123';
|
||||
|
||||
|
||||
const dataA = "Secret message";
|
||||
const dataB = "Secres message";
|
||||
|
||||
var diegest1a = getHMAC(key1, dataA, alg);
|
||||
var diegest2 = getHMAC(key2, dataA, alg);
|
||||
var diegest1b = getHMAC(key1, dataA, alg);
|
||||
|
||||
do_check_eq(diegest1a, diegest1b);
|
||||
do_check_neq(diegest1a, diegest2);
|
||||
|
||||
var diegest1 = getHMAC(key1, dataA, alg);
|
||||
diegest2 = getHMAC(key1, dataB, alg);
|
||||
|
||||
do_check_neq(diegest1, diegest2);
|
||||
|
||||
return diegest1;
|
||||
|
||||
let digest1a = getHMAC(key1, dataA, alg);
|
||||
let digest2 = getHMAC(key2, dataA, alg);
|
||||
let digest1b = getHMAC(key1, dataA, alg);
|
||||
|
||||
equal(digest1a, digest1b,
|
||||
"The digests for the same key, data and algorithm should match");
|
||||
notEqual(digest1a, digest2, "The digests for different keys should not match");
|
||||
|
||||
let digest1 = getHMAC(key1, dataA, alg);
|
||||
digest2 = getHMAC(key1, dataB, alg);
|
||||
|
||||
notEqual(digest1, digest2, "The digests for different data should not match");
|
||||
}
|
||||
|
||||
function hexdigest(data) {
|
||||
|
@ -56,35 +57,32 @@ function hexdigest(data) {
|
|||
|
||||
function testVectors() {
|
||||
// These are test vectors taken from RFC 4231, section 4.3. (Test Case 2)
|
||||
|
||||
const keyTestVector = "Jefe";
|
||||
const dataTestVector = "what do ya want for nothing?";
|
||||
|
||||
var diegest;
|
||||
/*
|
||||
Bug 356713
|
||||
diegest = hexdigest(getHMAC(dataTestVector, keyTestVector,
|
||||
Components.interfaces.nsICryptoHMAC.SHA224));
|
||||
do_check_eq(diegest, "a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44");
|
||||
*/
|
||||
let digest = hexdigest(getHMAC(dataTestVector, keyTestVector,
|
||||
Ci.nsICryptoHMAC.SHA256));
|
||||
equal(digest,
|
||||
"5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843",
|
||||
"Actual and expected SHA-256 digests should match");
|
||||
|
||||
diegest = hexdigest(getHMAC(dataTestVector, keyTestVector,
|
||||
Components.interfaces.nsICryptoHMAC.SHA256));
|
||||
do_check_eq(diegest, "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843");
|
||||
digest = hexdigest(getHMAC(dataTestVector, keyTestVector,
|
||||
Ci.nsICryptoHMAC.SHA384));
|
||||
equal(digest,
|
||||
"af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649",
|
||||
"Actual and expected SHA-384 digests should match");
|
||||
|
||||
diegest = hexdigest(getHMAC(dataTestVector, keyTestVector,
|
||||
Components.interfaces.nsICryptoHMAC.SHA384));
|
||||
do_check_eq(diegest, "af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649");
|
||||
|
||||
diegest = hexdigest(getHMAC(dataTestVector, keyTestVector,
|
||||
Components.interfaces.nsICryptoHMAC.SHA512));
|
||||
do_check_eq(diegest, "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737");
|
||||
digest = hexdigest(getHMAC(dataTestVector, keyTestVector,
|
||||
Ci.nsICryptoHMAC.SHA512));
|
||||
equal(digest,
|
||||
"164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737",
|
||||
"Actual and expected SHA-512 digests should match");
|
||||
}
|
||||
|
||||
function run_test() {
|
||||
testVectors();
|
||||
|
||||
testHMAC(Components.interfaces.nsICryptoHMAC.SHA1);
|
||||
testHMAC(Components.interfaces.nsICryptoHMAC.SHA512);
|
||||
testHMAC(Components.interfaces.nsICryptoHMAC.MD5);
|
||||
testHMAC(Ci.nsICryptoHMAC.SHA1);
|
||||
testHMAC(Ci.nsICryptoHMAC.SHA512);
|
||||
testHMAC(Ci.nsICryptoHMAC.MD5);
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче