-
Notifications
You must be signed in to change notification settings - Fork 11
feat(utilities): add publicEncrypt() and privateDecrypt() method #334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e8768fe
feat(utilities): add publicDecrypt() and privateDecrypt() method
drobnikj e624825
feat(utilities): add publicDecrypt() and privateDecrypt() method
drobnikj 3c09394
fix(utilities): fix PR comments from @fnesveda
drobnikj 3319102
fix(utilities): fix tests
drobnikj afc536d
Merge branch 'master' into feat/public_key_encryption
drobnikj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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('LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUF0dis3NlNXbklhOFFKWC94RUQxRQpYdnBBQmE3ajBnQnVYenJNUU5adjhtTW1RU0t2VUF0TmpOL2xacUZpQ0haZUQxU2VDcGV1MnFHTm5XbGRxNkhUCnh5cXJpTVZEbFNKaFBNT09QSENISVNVdFI4Tk5lR1Y1MU0wYkxJcENabHcyTU9GUjdq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mRUxMNjk4RTJRckdqTU9JWnhrcWdxZQoySE5VNktWRmV2NzdZeEJDbm1VcVdXZEhYMjcyU2NPMUYzdWpUdFVnRVBNWGN0aEdBckYzTWxEaUw1Q0k0RkhqCnhHc3pVemxzalRQTmpiY2MzdUE2MjVZS3VVZEI2c1h1Rk5NUHk5UDgwTzBpRWJGTXl3MWxmN2VpdFhvaUUxWVoKc3NhMDVxTUx4M3pPUXZTLzFDdFpqaFp4cVJMRW5pQ3NWa2JVRlVYclpodEU4dG94bGpWSUtpQ25qbitORmtqdwo2bTZ1anpBSytZZHd2Nk5WMFB4S0gwUk5NYVhwb1lmQk1oUmZ3dGlaS3V3Y2hyRFB5UEhBQ2J3WXNZOXdtUE9rCnpwdDNxWi9JdDVYTmVqNDI0RzAzcGpMbk1sd1B1T1VzYmFQUWQ2VHU4TFhsckZReUVjTXJDNHdjUTA1SzFVN3kKM1NNN3RFaTlnbjV3RjY1YVI5eEFBR0grTUtMMk5WNnQrUmlTazJVaWs1clNmeDE4Mk9wYmpSQ2grdmQ4UXhJdwotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=', '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); | ||
| } | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.