Skip to content

Commit

Permalink
fix: passthru non-random IVs on all functions (for testing, encrypted…
Browse files Browse the repository at this point in the history
… indexes, etc)
  • Loading branch information
coolaj86 committed Apr 26, 2024
1 parent 4bbc867 commit 5a5ebd9
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions cipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,39 @@ Cipher.create = function (sharedSecret) {
/**
* Encrypts a string and encodes it as base64urlsafe
* @param {String} str - utf8 string
* @param {Uint8Array} [_staticIv] - for non-random IVs, such as in tests
* @returns {Promise<String>}
*/
cipher.encryptString = async function (str) {
cipher.encryptString = async function (str, _staticIv) {
let bytes = encoder.encode(str);
let encrypted = await cipher.encrypt(bytes);
let encrypted = await cipher.encrypt(bytes, _staticIv);

return encrypted;
};

/**
* Encrypts a string and encodes it as base64urlsafe
* @param {String} str - utf8 string
* @param {Uint8Array} [_staticIv] - for non-random IVs, such as in tests
* @returns {Promise<Uint8Array>}
*/
cipher.encryptStringAsBytes = async function (str) {
cipher.encryptStringAsBytes = async function (str, _staticIv) {
let bytes = encoder.encode(str);
let encBytes = await cipher.encryptAsBytes(bytes);
let encBytes = await cipher.encryptAsBytes(bytes, _staticIv);

return encBytes;
};

/**
* Encrypts a byte array and encodes it as base64urlsafe
* @param {Uint8Array} bytes
* @param {Uint8Array} [_testIv] - for tests only, do not use
* @param {Uint8Array} [_staticIv] - for non-random IVs, such as in tests
* @returns {Promise<String>}
*/
cipher.encrypt = async function (bytes, _testIv) {
cipher.encrypt = async function (bytes, _staticIv) {
// let iv64 = Cipher.utils.bytesToUrlSafe(initializationVector);

let enc = await cipher.encryptAsBytes(bytes, _testIv);
let enc = await cipher.encryptAsBytes(bytes, _staticIv);
let encUrlSafe = Cipher.utils.bytesToUrlSafe(enc);

return encUrlSafe;
Expand All @@ -87,16 +89,18 @@ Cipher.create = function (sharedSecret) {
/**
* Encrypts a byte array and encodes it as base64urlsafe
* @param {Uint8Array} bytes
* @param {Uint8Array} [_testIv] - for tests only, do not use
* @param {Uint8Array} [_staticIv] - for non-random IVs, such as in tests
* @returns {Promise<Uint8Array>}
*/
cipher.encryptAsBytes = async function (bytes, _testIv) {
cipher.encryptAsBytes = async function (bytes, _staticIv) {
await cipher._init();

let initializationVector = new Uint8Array(IV_SIZE);
void Crypto.getRandomValues(initializationVector);
if (_testIv) {
initializationVector = _testIv;
let initializationVector;
if (_staticIv) {
initializationVector = _staticIv;
} else {
initializationVector = new Uint8Array(IV_SIZE);
void Crypto.getRandomValues(initializationVector);
}

let encryptOpts = Object.assign({ iv: initializationVector }, algoOpts);
Expand Down

0 comments on commit 5a5ebd9

Please sign in to comment.