Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
wrapTest(async () => {
// import keys
const keys = await getKeys(crypto, action.key);
const encKey = keys.publicKey;
const decKey = keys.privateKey;
const algorithm = Object.assign({}, action.algorithm);
algorithm.name = algorithm.name.toLowerCase();
// encrypt
const enc = await crypto.subtle.encrypt(algorithm, encKey, action.data);
// decrypt
let dec = await crypto.subtle.decrypt(algorithm, decKey, enc);
assert.equal(Convert.ToHex(dec), Convert.ToHex(action.data));
dec = await crypto.subtle.decrypt(algorithm, decKey, action.encData);
assert.equal(Convert.ToHex(dec), Convert.ToHex(action.data));
}, action, index);
});
const keys = await getKeys(crypto, action.key);
const verifyKey = keys.publicKey;
const signKey = keys.privateKey;
const algorithm = Object.assign({}, action.algorithm);
algorithm.name = algorithm.name.toLowerCase();
// sign
const signature = await crypto.subtle.sign(algorithm, signKey, action.data);
// verify
let ok = await crypto.subtle.verify(algorithm, verifyKey, signature, action.data);
assert.equal(true, ok, "Cannot verify signature from Action data");
ok = await crypto.subtle.verify(algorithm, verifyKey, action.signature, action.data);
if (!ok) {
assert.equal(Convert.ToHex(signature), Convert.ToHex(action.signature));
}
assert.equal(true, ok);
}, action, index);
});
// Can't continue if key is not extractable.
if (!action.extractable) {
return;
}
const exportedData = await crypto.subtle.exportKey(
action.format,
importedKey);
if (action.format === "jwk") {
exportedData.key_ops.sort();
(action.data as JsonWebKey).key_ops.sort();
assert.deepEqual(exportedData, action.data);
} else {
assert.equal(Convert.ToHex(exportedData as ArrayBuffer), Convert.ToHex(action.data as ArrayBuffer));
}
}, action, index);
});
wrapTest(async () => {
// import keys
const keys = await getKeys(crypto, action.key);
const algorithm = Object.assign({}, action.algorithm, { public: keys.publicKey });
algorithm.name = algorithm.name.toLowerCase();
// derive key
const derivedKey = await crypto.subtle.deriveKey(algorithm, keys.privateKey, action.derivedKeyType, true, action.keyUsages);
const keyData = await crypto.subtle.exportKey(action.format, derivedKey);
if (action.format === "jwk") {
assert.deepEqual(keyData, action.keyData);
} else {
assert.equal(Convert.ToHex(keyData as ArrayBuffer), Convert.ToHex(action.keyData as ArrayBuffer));
}
}, action, index);
});
// import keys
const keys = await getKeys(crypto, action.key);
const encKey = keys.publicKey;
const decKey = keys.privateKey;
const algorithm = Object.assign({}, action.algorithm);
algorithm.name = algorithm.name.toLowerCase();
// encrypt
const enc = await crypto.subtle.encrypt(algorithm, encKey, action.data);
// decrypt
let dec = await crypto.subtle.decrypt(algorithm, decKey, enc);
assert.equal(Convert.ToHex(dec), Convert.ToHex(action.data));
dec = await crypto.subtle.decrypt(algorithm, decKey, action.encData);
assert.equal(Convert.ToHex(dec), Convert.ToHex(action.data));
}, action, index);
});
wrapTest(async () => {
const wKey = (await getKeys(crypto, action.wKey)).privateKey;
const key = await getKeys(crypto, action.key);
const wrappedKey = await crypto.subtle.wrapKey(action.wKey.format, wKey, key.publicKey, action.algorithm);
if (action.wrappedKey) {
assert.equal(Convert.ToHex(wrappedKey), Convert.ToHex(action.wrappedKey));
}
const unwrappedKey = await crypto.subtle.unwrapKey(
action.wKey.format,
wrappedKey,
key.privateKey,
action.algorithm,
action.wKey.algorithm,
action.wKey.extractable,
action.wKey.keyUsages);
assert.deepEqual(unwrappedKey.algorithm, wKey.algorithm);
}, action, index);
});
wrapTest(async () => {
const hash = await crypto.subtle.digest(action.algorithm, action.data);
assert.equal(Convert.ToHex(hash), Convert.ToHex(action.hash));
}, action, index);
});
public async thumbprint() {
const bytes = await this.serialize();
const thumbprint = await Secret.digest("SHA-256", bytes);
return Convert.ToHex(thumbprint);
}