Skip to content

Commit

Permalink
20210721A
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoalh committed Jul 21, 2021
1 parent b9dcb76 commit 0ee9bcb
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 19 deletions.
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ body:
description: "What version(s) is affected? Must be a supported version(s) listed in the \"Security Policy\" (/.github/SECURITY.md)."
multiple: true
options:
- "v1.2.1"
- "v1.2.0"
- "v1.1.2"
- "v1.1.1"
Expand Down
5 changes: 3 additions & 2 deletions lib/decrypt-multiple-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ const check = require("./internal/check.js"),
*/
function decryptMultipleLine(data, passphrase) {
check(data, passphrase);
let resultObjectRN = {};
let hash = internalCrypto.hash(passphrase),
resultObjectRN = {};
Promise.allSettled(
data.split("\r\n").map((itemRN, indexRN) => {
new Promise(() => {
let resultObjectN = {};
Promise.allSettled(
itemRN.split("\n").map((itemN, indexN) => {
new Promise(() => {
resultObjectN[indexN] = (itemN.length === 0) ? "" : internalCrypto.decrypt(itemN, passphrase);
resultObjectN[indexN] = (itemN.length === 0) ? "" : internalCrypto.decrypt(itemN, hash);
}).catch();
})
);
Expand Down
2 changes: 1 addition & 1 deletion lib/decrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ const check = require("./internal/check.js"),
*/
function decrypt(data, passphrase) {
check(data, passphrase);
return internalCrypto.decrypt(data, passphrase);
return internalCrypto.decrypt(data, internalCrypto.hash(passphrase));
};
module.exports = decrypt;
5 changes: 3 additions & 2 deletions lib/encrypt-multiple-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ const check = require("./internal/check.js"),
*/
function encryptMultipleLine(data, passphrase) {
check(data, passphrase);
let resultObjectRN = {};
let hash = internalCrypto.hash(passphrase),
resultObjectRN = {};
Promise.allSettled(
data.split("\r\n").map((itemRN, indexRN) => {
new Promise(() => {
let resultObjectN = {};
Promise.allSettled(
itemRN.split("\n").map((itemN, indexN) => {
new Promise(() => {
resultObjectN[indexN] = (itemN.length === 0) ? "" : internalCrypto.encrypt(itemN, passphrase);
resultObjectN[indexN] = (itemN.length === 0) ? "" : internalCrypto.encrypt(itemN, hash);
}).catch();
})
);
Expand Down
2 changes: 1 addition & 1 deletion lib/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ const check = require("./internal/check.js"),
*/
function encrypt(data, passphrase) {
check(data, passphrase);
return internalCrypto.encrypt(data, passphrase);
return internalCrypto.encrypt(data, internalCrypto.hash(passphrase));
};
module.exports = encrypt;
23 changes: 12 additions & 11 deletions lib/internal/crypto.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
const crypto = require("crypto");
/**
* @private
* @function hash
* @function hashDigest
* @param {string} passphrase
* @returns {Buffer}
*/
function hash(passphrase) {
return crypto.createHash("sha256").update(passphrase).digest();
function hashDigest(passphrase) {
return crypto.createHash("sha256").update(passphrase).digest().slice(0, 32);
};
/**
* @private
* @function decrypt
* @param {string} data
* @param {string} passphrase
* @param {Buffer} hash
* @returns {string}
*/
function decrypt(data, passphrase) {
function decrypt(data, hash) {
let encrypted = Buffer.from(data, "base64");
let decipher = crypto.createDecipheriv("AES-256-CBC", hash(passphrase).slice(0, 32), encrypted.slice(0, 16));
let decipher = crypto.createDecipheriv("AES-256-CBC", hash, encrypted.slice(0, 16));
let decrypted = Buffer.concat([decipher.update(encrypted.slice(16)), decipher.final()]).toString();
return decrypted.substr(0, decrypted.length - decrypted.charCodeAt(decrypted.length - 1));
};
/**
* @private
* @function encrypt
* @param {string} data
* @param {string} passphrase
* @param {Buffer} hash
* @returns {string}
*/
function encrypt(data, passphrase) {
function encrypt(data, hash) {
let iv = crypto.randomBytes(16),
tone = 16 - (data.length % 16);
let cipher = crypto.createCipheriv("AES-256-CBC", hash(passphrase).slice(0, 32), iv);
let cipher = crypto.createCipheriv("AES-256-CBC", hash, iv);
return Buffer.concat([iv, Buffer.concat([cipher.update(data.padEnd(data.length + tone, String.fromCharCode(tone))), cipher.final()])]).toString("base64");
};
module.exports = {
decrypt,
encrypt
decrypt: decrypt,
encrypt: encrypt,
hash: hashDigest
};
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hugoalh/symmetric-crypto",
"version": "1.2.0",
"version": "1.2.1",
"description": "A module to provide an easier symmetric crypto.",
"keywords": [
"crypto",
Expand Down

0 comments on commit 0ee9bcb

Please sign in to comment.