Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions packages/utilities/src/crypto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import crypto from 'crypto';
import { cryptoRandomObjectId } from './utilities';

const ENCRYPTION_ALGORITHM = 'aes-256-gcm';
const ENCRYPTION_KEY_LENGTH = 32;
const ENCRYPTION_IV_LENGTH = 16;
const ENCRYPTION_AUTH_TAG_LENGTH = 16;

type DecryptOptions = {
privateKey: Buffer;
passphrase: string;
encryptedPassword: string;
encryptedValue: string;
}

type EncryptOptions = {
publicKey: Buffer;
value: string;
}

/**
* It encrypts the given value using AES cipher and the password for encryption using the public key.
* NOTE: The encryption password is a string of encryption key and initial vector used for cipher.
* It returns the encrypted password and encrypted value in BASE64 format.
*
* @param publicKey {Buffer} Public key used for encryption
* @param value {string} Value to be encrypted
* @returns {Object<encryptedPassword, encryptedValue>}
*/
export function publicEncrypt({ publicKey, value }: EncryptOptions) {
const key = cryptoRandomObjectId(ENCRYPTION_KEY_LENGTH);
const initVector = cryptoRandomObjectId(ENCRYPTION_IV_LENGTH);
const cipher = crypto.createCipheriv(ENCRYPTION_ALGORITHM, key, initVector);

const bufferFromValue = Buffer.from(value, 'utf-8');
const bufferFromKey = Buffer.from(key, 'utf-8');
const bufferFromInitVector = Buffer.from(initVector, 'utf-8');
const passwordBuffer = Buffer.concat([bufferFromKey, bufferFromInitVector]);

// NOTE: Auth Tag is appended to the end of the encrypted data, it has length of 16 bytes and ensures integrity of the data.
const encryptedValue = Buffer.concat([cipher.update(bufferFromValue), cipher.final(), cipher.getAuthTag()]);
const encryptedPassword = crypto.publicEncrypt(publicKey, passwordBuffer);

return {
encryptedPassword: encryptedPassword.toString('base64'),
encryptedValue: encryptedValue.toString('base64'),
};
}

/**
* It decrypts encrypted password using private key
* and uses the password(consists of encrypted key and initial vector)
* to decrypt the encrypted value.
*
* @param privateKey {Buffer} Private key used for decryption
* @param passphrase {string} Passphrase from private key
* @param encryptedPassword {string} Password in Base64 encrypted using private key
* @param encryptedValue {string} Content in Base64 encrypted using AES cipher
* @returns {string}
*/
export function privateDecrypt({
privateKey,
passphrase,
encryptedPassword,
encryptedValue,
}: DecryptOptions): string {
const encryptedValueBuffer = Buffer.from(encryptedValue, 'base64');
const encryptedPasswordBuffer = Buffer.from(encryptedPassword, 'base64');

const passwordBuffer = crypto.privateDecrypt({ key: privateKey, passphrase }, encryptedPasswordBuffer);
if (passwordBuffer.length !== ENCRYPTION_KEY_LENGTH + ENCRYPTION_IV_LENGTH) {
throw new Error('privateDecrypt: Decryption failed, invalid password length!');
}

// Slice Auth tag from the final value cipher
const authTagBuffer = encryptedValueBuffer.slice(encryptedValueBuffer.length - ENCRYPTION_AUTH_TAG_LENGTH);
const encryptedDataBuffer = encryptedValueBuffer.slice(0, encryptedValueBuffer.length - ENCRYPTION_AUTH_TAG_LENGTH);

const encryptionKeyBuffer = passwordBuffer.slice(0, ENCRYPTION_KEY_LENGTH);
const initVectorBuffer = passwordBuffer.slice(ENCRYPTION_KEY_LENGTH);
const decipher = crypto.createDecipheriv(ENCRYPTION_ALGORITHM, encryptionKeyBuffer, initVectorBuffer);
decipher.setAuthTag(authTagBuffer);

return Buffer.concat([decipher.update(encryptedDataBuffer), decipher.final()]).toString('utf-8');
}
1 change: 1 addition & 0 deletions packages/utilities/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './health_checker';
export * from './parse_jsonl_stream';
export * from './streams_utilities';
export * from './webhook_payload_template';
export * from './crypto';
96 changes: 96 additions & 0 deletions test/crypto.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import * as utils from '@apify/utilities';

// eslint-disable-next-line max-len
const publicKey = Buffer.from('LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtp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xdmRQSTdLb2Q0MEdDM2dlQnhweFFNYnp1OFNUcGpOcElJRkJvRVc5dFRhemUzeHZXWnV6dDc0CnFjZS8xWURuUHBLeW5lM0xGMk94VWoyYWVYUW5YQkpYcGhTZTBVTGJMcWJtUll4bjJKWkl1d09RNHV5dm94NjUKdG9TWGNac054dUs4QTErZXNXR3JSN3pVc0djdU9QQTFERE9Ja2JjcGtmRUxMNjk4RTJRckdqTU9JWnhrcWdxZQoySE5VNktWRmV2NzdZeEJDbm1VcVdXZEhYMjcyU2NPMUYzdWpUdFVnRVBNWGN0aEdBckYzTWxEaUw1Q0k0RkhqCnhHc3pVemxzalRQTmpiY2MzdUE2MjVZS3VVZEI2c1h1Rk5NUHk5UDgwTzBpRWJGTXl3MWxmN2VpdFhvaUUxWVoKc3NhMDVxTUx4M3pPUXZTLzFDdFpqaFp4cVJMRW5pQ3NWa2JVRlVYclpodEU4dG94bGpWSUtpQ25qbitORmtqdwo2bTZ1anpBSytZZHd2Nk5WMFB4S0gwUk5NYVhwb1lmQk1oUmZ3dGlaS3V3Y2hyRFB5UEhBQ2J3WXNZOXdtUE9rCnpwdDNxWi9JdDVYTmVqNDI0RzAzcGpMbk1sd1B1T1VzYmFQUWQ2VHU4TFhsckZReUVjTXJDNHdjUTA1SzFVN3kKM1NNN3RFaTlnbjV3RjY1YVI5eEFBR0grTUtMMk5WNnQrUmlTazJVaWs1clNmeDE4Mk9wYmpSQ2grdmQ4UXhJdwotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=', 'base64');
const passphrase = 'pwd1234';

describe('publicEncrypt() and privateDecrypt()', () => {
it('should decrypt encrypted random strings', () => {
for (let i = 0; i < 100; i++) {
const randomString = utils.cryptoRandomObjectId(10);
const { encryptedPassword, encryptedValue } = utils.publicEncrypt({
publicKey,
value: randomString,
});
const decryptedValue = utils.privateDecrypt({
privateKey,
passphrase,
encryptedPassword,
encryptedValue,
});
expect(randomString).toEqual(decryptedValue);
}
});

it('should decrypt encrypted random strings with special characters', () => {
const randomString = utils.cryptoRandomObjectId(10);
// eslint-disable-next-line max-len
for (const char of ['👍', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '|', ';', ':', '"', "'", ',', '.', '<', '>', '?', '/', '~', '`']) {
const stringWithSpecialChar = `${char}${randomString}${char}${randomString}${char}`;
const { encryptedPassword, encryptedValue } = utils.publicEncrypt({
publicKey,
value: stringWithSpecialChar,
});
const decryptedValue = utils.privateDecrypt({
privateKey,
passphrase,
encryptedPassword,
encryptedValue,
});
expect(stringWithSpecialChar).toEqual(decryptedValue);
}
});

it('throws if encrypted password is not valid', () => {
const randomString = utils.cryptoRandomObjectId(10);
const { encryptedPassword, encryptedValue } = utils.publicEncrypt({ publicKey, value: randomString });
expect(() => utils.privateDecrypt({
privateKey,
passphrase,
encryptedPassword: encryptedPassword.slice(2),
encryptedValue,
})).toThrow();
expect(() => utils.privateDecrypt({
privateKey,
passphrase,
encryptedPassword: `bla${encryptedPassword}`,
encryptedValue,
})).toThrow();
});

it('throws if encrypted value is not valid', () => {
const randomString = utils.cryptoRandomObjectId(10);
const { encryptedPassword, encryptedValue } = utils.publicEncrypt({ publicKey, value: randomString });
expect(() => utils.privateDecrypt({
privateKey,
passphrase,
encryptedPassword,
encryptedValue: encryptedValue.slice(2),
})).toThrow();
expect(() => utils.privateDecrypt({
privateKey,
passphrase,
encryptedPassword,
encryptedValue: `bla${encryptedValue}`,
})).toThrow();
});

it('should return different cipher for the same string', () => {
const randomString = utils.cryptoRandomObjectId(100);
const uniqueCiphers = new Set();
const uniqueCiphers16Bytes = new Set();
for (let i = 0; i < 100; i++) {
const { encryptedValue } = utils.publicEncrypt({
publicKey,
value: randomString,
});
const theFirst16Bytes = Buffer.from(encryptedValue, 'base64').toString('utf-8').slice(0, 16);
expect(uniqueCiphers.has(encryptedValue)).toBe(false);
expect(uniqueCiphers16Bytes.has(theFirst16Bytes)).toBe(false);
uniqueCiphers.add(encryptedValue);
uniqueCiphers16Bytes.add(theFirst16Bytes);
}
});
});