Skip to content
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

Add buffer + array encrypt/decrypt methods #41

Merged
merged 2 commits into from
Aug 28, 2020
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
20 changes: 10 additions & 10 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",
"request": "attach",
"skipFiles": [
"<node_internals>/**"
],
"type": "pwa-node"
},
{
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",
"request": "attach",
"skipFiles": [
"<node_internals>/**"
],
"type": "pwa-node"
},
{
"type": "node",
"name": "vscode-jest-tests",
"request": "launch",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"args": [
"packages/sdk",
"packages/crypto",
"--runInBand"
],
"cwd": "${workspaceFolder}",
Expand Down
Binary file added packages/crypto/Dramatic-Prairie-Dog.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 51 additions & 6 deletions packages/crypto/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,76 @@ module.exports = class LorenaCrypto {
*
* @param {string} password Password to encrypt the message
* @param {string} message Message to be encrypted
* @returns {Promise} Return a promise with the execution of the encryption.
* @returns {*} encrypted message
*/
encrypt (password, message) {
return this.encryptArray(password, stringToU8a(message))
}

/**
* Encrypts (symmetric) an array with a keypair.
*
* @param {string} password Password to encrypt the message
* @param {Buffer} buffer content to be encrypted
* @returns {*} encrypted message
*/
encryptBuffer (password, buffer) {
const array = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / Uint8Array.BYTES_PER_ELEMENT)
const result = this.encryptArray(password, array)
return (result)
}

/**
* Encrypts (symmetric) an array with a keypair.
*
* @param {string} password Password to encrypt the message
* @param {Uint8Array} array content to be encrypted
* @returns {*} encrypted message
*/
encryptArray (password, array) {
let secret = stringToU8a(password)
secret = u8aConcat(secret, new Uint8Array(32 - secret.length))
const messagePreEncryption = stringToU8a(message)
const noncePreEncryption = randomAsU8a(24)

// Encrypt the message
const result = naclEncrypt(messagePreEncryption, secret, noncePreEncryption)
const result = naclEncrypt(array, secret, noncePreEncryption)
return (result)
}

/**
* Encrypts (symmetric) a message with a keypair.
* Decrypts (symmetric) a message with a keypair.
*
* @param {string} password Password to decrypt the message
* @param {string} msgEncrypted Message to be decrypted
* @returns {Promise} Return a promise with the execution of the encryption.
* @returns {string} decrypted message
*/
decrypt (password, msgEncrypted) {
return hexToString(u8aToHex(this.decryptArray(password, msgEncrypted)))
}

/**
* Decrypts (symmetric) an array with a keypair.
*
* @param {string} password Password to decrypt the message
* @param {string} msgEncrypted Message to be decrypted
* @returns {Buffer} decrypted buffer
*/
decryptBuffer (password, msgEncrypted) {
return Buffer.from(this.decryptArray(password, msgEncrypted))
}

/**
* Decrypts (symmetric) an array with a keypair.
*
* @param {string} password Password to decrypt the message
* @param {string} msgEncrypted Message to be decrypted
* @returns {Uint8Array} decrypted array
*/
decryptArray (password, msgEncrypted) {
let secret = stringToU8a(password)
secret = u8aConcat(secret, new Uint8Array(32 - secret.length))
const messageDecrypted = naclDecrypt(msgEncrypted.encrypted, msgEncrypted.nonce, secret)
return (hexToString(u8aToHex(messageDecrypted)))
return messageDecrypted
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/crypto/src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const fs = require('fs')
const path = require('path')
const LorenaCrypto = require('./index')
const crypto = new LorenaCrypto()

Expand Down Expand Up @@ -76,6 +78,16 @@ test('Should encrypt & decrypt a message', () => {
expect(msg).toEqual(message)
})

// Encryption.
test('Should encrypt & decrypt a buffer', () => {
const buffer = fs.readFileSync(path.resolve(__dirname, '../Dramatic-Prairie-Dog.gif'))
const msgEncrypted = crypto.encryptBuffer(password, buffer)
expect(msgEncrypted.encrypted).toBeDefined()
expect(msgEncrypted.nonce).toBeDefined()
const msg = crypto.decryptBuffer(password, msgEncrypted)
expect(msg).toEqual(buffer)
})

// Encryption.
test('Should encrypt & decrypt an object', () => {
const msgEncrypted = crypto.encryptObj(password, { msg: message, test: 'áà # test' })
Expand Down