Skip to content

Instantly share code, notes, and snippets.

@bluebycode
Created November 30, 2019 00:30
Show Gist options
  • Save bluebycode/c53492f975ee926ad0cc58055ed0faf3 to your computer and use it in GitHub Desktop.
Save bluebycode/c53492f975ee926ad0cc58055ed0faf3 to your computer and use it in GitHub Desktop.
import { secretbox, box, randomBytes } from "tweetnacl";
import {
decodeUTF8,
encodeUTF8,
encodeBase64,
decodeBase64
} from "tweetnacl-util";
const fixedNonce = () => new Uint8Array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])//randomBytes(secretbox.nonceLength);
const newNonce = () => randomBytes(secretbox.nonceLength);
export const generateKey = () => encodeBase64(randomBytes(secretbox.keyLength));
export const encrypt = (keyUint8Array, msg) => {
const nonce = newNonce();
console.log("NONCE", nonce)
const messageUint8 = decodeUTF8(msg);
const box = secretbox(messageUint8, nonce, keyUint8Array);
const fullMessage = new Uint8Array(nonce.length + box.length);
fullMessage.set(nonce);
fullMessage.set(box, nonce.length);
console.log("fullMessage", fullMessage)
const base64FullMessage = encodeBase64(fullMessage);
return base64FullMessage;
};
export const decrypt = (keyUint8Array, messageWithNonce) => {
const messageWithNonceAsUint8Array = decodeBase64(messageWithNonce);
const nonce = messageWithNonceAsUint8Array.slice(0, secretbox.nonceLength);
const message = messageWithNonceAsUint8Array.slice(
secretbox.nonceLength,
messageWithNonce.length
);
const decrypted = secretbox.open(message, nonce, keyUint8Array);
if (!decrypted) {
throw new Error("Could not decrypt message");
}
return encodeUTF8(decrypted);
};
export const buildSharedKey = (pub, priv) => box.before(pub, priv)
/*
const obj = { hello: 'world' };
const pairA = generateKeyPair();
const pairB = generateKeyPair();
const sharedA = box.before(pairB.publicKey, pairA.secretKey);
const sharedB = box.before(pairA.publicKey, pairB.secretKey);
const encrypted = encrypt(sharedA, obj);
const decrypted = decrypt(sharedB, encrypted);
console.log(obj, encrypted, decrypted);*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment