Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const libsodium = require('libsodium-wrappers');
- const concat_typed_array = require("concat-typed-array");
- const nonce_length = libsodium.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES;
- (async () => {
- await libsodium.ready;
- }).then(function(){
- module.exports = class Sodium {
- constructor(key){
- if(key === null) {
- throw "Sodium: Private key not set."
- }
- this.key = libsodium.from_hex(key);
- }
- encrypt(plaintext){
- var nonce = libsodium.randombytes_buf(nonce_length),
- ciphertext = libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt(plaintext, null, nonce, nonce, this.key);
- console.log("nonce", nonce);
- console.log("ciphertext", ciphertext);
- return concat_typed_array(Uint8Array, nonce, ciphertext);
- }
- decrypt(encryption){
- var nonce = encryption.slice(0, nonce_length),
- ciphertext = encryption.slice(nonce_length);
- console.log("nonce", nonce);
- console.log("ciphertext", ciphertext);
- return libsodium.crypto_aead_xchacha20poly1305_ietf_decrypt(nonce, ciphertext, null, nonce, this.key, "text");
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement