add_task(async function test_key_memoization() {
let cryptoGlobal = cryptoSvc._getCrypto();
let oldImport = cryptoGlobal.subtle.importKey; if (!oldImport) {
_("Couldn't swizzle crypto.subtle.importKey; returning."); return;
}
let iv = cryptoSvc.generateRandomIV();
let key = await cryptoSvc.generateRandomKey();
let c = 0;
cryptoGlobal.subtle.importKey = function (
format,
keyData,
algo,
extractable,
usages
) {
c++; return oldImport.call(
cryptoGlobal.subtle,
format,
keyData,
algo,
extractable,
usages
);
};
// Encryption should cause a single counter increment. Assert.equal(c, 0);
let cipherText = await cryptoSvc.encrypt("Hello, world.", key, iv); Assert.equal(c, 1);
cipherText = await cryptoSvc.encrypt("Hello, world.", key, iv); Assert.equal(c, 1);
// ... as should decryption.
await cryptoSvc.decrypt(cipherText, key, iv);
await cryptoSvc.decrypt(cipherText, key, iv);
await cryptoSvc.decrypt(cipherText, key, iv); Assert.equal(c, 2);
// Just verify that it gets populated with the correct bytes.
add_task(async function test_makeUint8Array() {
ChromeUtils.importESModule("resource://gre/modules/ctypes.sys.mjs");
let item1 = cryptoSvc.makeUint8Array("abcdefghi", false); Assert.ok(item1); for (let i = 0; i < 8; ++i) { Assert.equal(item1[i], "abcdefghi".charCodeAt(i));
}
});
add_task(async function test_encrypt_decrypt() { // First, do a normal run with expected usage... Generate a random key and // iv, encrypt and decrypt a string. var iv = cryptoSvc.generateRandomIV(); Assert.equal(iv.length, 24);
var key = await cryptoSvc.generateRandomKey(); Assert.equal(key.length, 44);
var mySecret = "bacon is a vegetable"; var cipherText = await cryptoSvc.encrypt(mySecret, key, iv); Assert.equal(cipherText.length, 44);
var clearText = await cryptoSvc.decrypt(cipherText, key, iv); Assert.equal(clearText.length, 20);
// Did the text survive the encryption round-trip? Assert.equal(clearText, mySecret); Assert.notEqual(cipherText, mySecret); // just to be explicit
// Do some more tests with a fixed key/iv, to check for reproducable results.
key = "St1tFCor7vQEJNug/465dQ==";
iv = "oLjkfrLIOnK2bDRvW4kXYA==";
_("Testing small IV.");
mySecret = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXo=";
let shortiv = "YWJj";
let err; try {
await cryptoSvc.encrypt(mySecret, key, shortiv);
} catch (ex) {
err = ex;
} Assert.ok(!!err);
var badkey = "badkeybadkeybadkeybadk=="; var badiv = "badivbadivbadivbadivbad="; var badcipher = "crapinputcrapinputcrapinputcrapinputcrapinp="; var failure;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.