Bugfix secretbox: Fix buffer conversions

This commit is contained in:
Starbeamrainbowlabs 2021-10-02 02:11:26 +01:00
parent a2d2b58b38
commit c0441a1dcd
Signed by: sbrl
GPG key ID: 1BE5172E637709C2

View file

@ -1,13 +1,14 @@
"use strict";
import { randomBytes, secretbox } from 'tweetnacl';
import tweetnacl from 'tweetnacl';
const { randomBytes, secretbox } = tweetnacl;
/**
* Creates a new key ready for encryption.
* @return {string} A new base64-encoded 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);
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) {
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 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
if(!data_bytes) return null;
return data_bytes.toString("utf-8");
return Buffer.from(data_bytes).toString("utf-8");
}
export { make_key, encrypt, decrypt };