Bugfix secretbox: Fix buffer conversions
This commit is contained in:
parent
a2d2b58b38
commit
c0441a1dcd
1 changed files with 6 additions and 5 deletions
|
@ -1,13 +1,14 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
import { randomBytes, secretbox } from 'tweetnacl';
|
import tweetnacl from 'tweetnacl';
|
||||||
|
const { randomBytes, secretbox } = tweetnacl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new key ready for encryption.
|
* Creates a new key ready for encryption.
|
||||||
* @return {string} A new base64-encoded key.
|
* @return {string} A new base64-encoded key.
|
||||||
*/
|
*/
|
||||||
function make_key() {
|
function make_key() {
|
||||||
return randomBytes(secretbox.keyLength).toString("base64");
|
return Buffer.from(randomBytes(secretbox.keyLength)).toString("base64");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -28,7 +29,7 @@ function encrypt(key, data) {
|
||||||
key_bytes.fill(0);
|
key_bytes.fill(0);
|
||||||
nonce.fill(0);
|
nonce.fill(0);
|
||||||
|
|
||||||
return concat_bytes.toString("base64");
|
return Buffer.from(concat_bytes).toString("base64");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -39,7 +40,7 @@ function encrypt(key, data) {
|
||||||
*/
|
*/
|
||||||
function decrypt(key, cipher_text) {
|
function decrypt(key, cipher_text) {
|
||||||
const concat_bytes = Buffer.from(cipher_text, "base64");
|
const concat_bytes = Buffer.from(cipher_text, "base64");
|
||||||
const key_bytes = Buffer.from(key, "basse64");
|
const key_bytes = Buffer.from(key, "base64");
|
||||||
|
|
||||||
const nonce = concat_bytes.slice(0, secretbox.nonceLength);
|
const nonce = concat_bytes.slice(0, secretbox.nonceLength);
|
||||||
const cipher_bytes = concat_bytes.slice(secretbox.nonceLength);
|
const cipher_bytes = concat_bytes.slice(secretbox.nonceLength);
|
||||||
|
@ -50,7 +51,7 @@ function decrypt(key, cipher_text) {
|
||||||
// Ref https://github.com/dchest/tweetnacl-js/wiki/Examples#secretbox
|
// Ref https://github.com/dchest/tweetnacl-js/wiki/Examples#secretbox
|
||||||
if(!data_bytes) return null;
|
if(!data_bytes) return null;
|
||||||
|
|
||||||
return data_bytes.toString("utf-8");
|
return Buffer.from(data_bytes).toString("utf-8");
|
||||||
}
|
}
|
||||||
|
|
||||||
export { make_key, encrypt, decrypt };
|
export { make_key, encrypt, decrypt };
|
||||||
|
|
Loading…
Reference in a new issue