fixes
This commit is contained in:
parent
850515cc42
commit
85e3303341
59
index.js
59
index.js
@ -3,6 +3,7 @@
|
|||||||
* Created by Timo Hocker <timo@scode.ovh>, March 2020
|
* Created by Timo Hocker <timo@scode.ovh>, March 2020
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// @ts-nocheck
|
||||||
/* eslint-disable no-magic-numbers */
|
/* eslint-disable no-magic-numbers */
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
@ -13,7 +14,7 @@ const encryption = {
|
|||||||
algorithm: 'aes-256-gcm',
|
algorithm: 'aes-256-gcm',
|
||||||
nonce_size: 12,
|
nonce_size: 12,
|
||||||
tag_size: 16,
|
tag_size: 16,
|
||||||
key_size: 16,
|
key_size: 32,
|
||||||
hash: 'sha256',
|
hash: 'sha256',
|
||||||
salt_size: 16,
|
salt_size: 16,
|
||||||
iterations: 32767
|
iterations: 32767
|
||||||
@ -181,33 +182,35 @@ function encrypt_aes (text, pass) {
|
|||||||
* @returns {string} plaintext
|
* @returns {string} plaintext
|
||||||
*/
|
*/
|
||||||
function decrypt_aes (ciphertext, pass) {
|
function decrypt_aes (ciphertext, pass) {
|
||||||
const buf = Buffer.from (ciphertext, 'base64');
|
try {
|
||||||
const salt = buf.slice (0, encryption.salt_size);
|
let buf = Buffer.from (ciphertext, 'base64');
|
||||||
// eslint-disable-next-line no-sync
|
const salt = buf.slice (0, encryption.salt_size);
|
||||||
const key = crypto.pbkdf2Sync (
|
buf = buf.slice (encryption.salt_size);
|
||||||
Buffer.from (pass, 'utf-8'),
|
// eslint-disable-next-line no-sync
|
||||||
salt,
|
const key = crypto.pbkdf2Sync (
|
||||||
encryption.iterations,
|
Buffer.from (pass, 'utf-8'),
|
||||||
encryption.key_size,
|
salt,
|
||||||
encryption.hash
|
encryption.iterations,
|
||||||
);
|
encryption.key_size,
|
||||||
const nonce = buf.slice (encryption.salt_size, encryption.nonce_size);
|
encryption.hash
|
||||||
const enc = buf.slice (
|
);
|
||||||
encryption.salt_size + encryption.nonce_size,
|
const nonce = buf.slice (0, encryption.nonce_size);
|
||||||
buf.length - encryption.salt_size - encryption.tag_size
|
buf = buf.slice (encryption.nonce_size);
|
||||||
);
|
const tag = buf.slice (
|
||||||
const tag = buf.slice (
|
buf.length - encryption.tag_size
|
||||||
encryption.salt_size
|
);
|
||||||
+ encryption.nonce_size
|
buf = buf.slice (0, buf.length - encryption.tag_size);
|
||||||
+ enc.length
|
const cipher = crypto.createDecipheriv (encryption.algorithm, key, nonce);
|
||||||
);
|
cipher.setAuthTag (tag);
|
||||||
const cipher = crypto.createDecipheriv (encryption.algorithm, key, nonce);
|
return Buffer.concat ([
|
||||||
cipher.setAuthTag (tag);
|
cipher.update (buf),
|
||||||
return Buffer.concat ([
|
cipher.final ()
|
||||||
cipher.update (enc),
|
])
|
||||||
cipher.final ()
|
.toString ('utf-8');
|
||||||
])
|
}
|
||||||
.toString ('utf-8');
|
catch (e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
4512
package-lock.json
generated
Normal file
4512
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,3 +1,5 @@
|
|||||||
|
/* eslint-disable no-magic-numbers */
|
||||||
|
// @ts-nocheck
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const test = require ('ava');
|
const test = require ('ava');
|
||||||
@ -19,3 +21,18 @@ test ('fail decryption', (t) => {
|
|||||||
const dec = crypto.decrypt_aes (enc, 'baz');
|
const dec = crypto.decrypt_aes (enc, 'baz');
|
||||||
t.is (dec, null);
|
t.is (dec, null);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test ('unique crypto strings', (t) => {
|
||||||
|
const enc = [
|
||||||
|
crypto.encrypt_aes ('foo', 'bar'),
|
||||||
|
crypto.encrypt_aes ('foo', 'bar'),
|
||||||
|
crypto.encrypt_aes ('foo', 'bar'),
|
||||||
|
crypto.encrypt_aes ('foo', 'bar'),
|
||||||
|
crypto.encrypt_aes ('foo', 'bar'),
|
||||||
|
crypto.encrypt_aes ('foo', 'bar'),
|
||||||
|
crypto.encrypt_aes ('foo', 'bar'),
|
||||||
|
crypto.encrypt_aes ('foo', 'bar')
|
||||||
|
];
|
||||||
|
const unique = enc.filter ((v, i) => enc.indexOf (v) === i).length;
|
||||||
|
t.is (unique, 8);
|
||||||
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user