Forked from 842Mono/libsodium xchacha20poly1305_ietf.js
Created
January 10, 2022 04:13
-
-
Save lgh06/d8038ab080dae78790dd3af6d8330515 to your computer and use it in GitHub Desktop.
Encrypting and decrypting a message using secret key aead xchacha20poly1305_ietf (libsodium js)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const sodium = require('libsodium-wrappers'); | |
sodium.ready.then(function() | |
{ | |
console.log('sodium ready'); | |
//xchacha20ploy1305 ietf | |
//A key and a nonce are generated by the encrypting party. The decrypting | |
//party should use them for decryption | |
//encryption | |
let key3 = sodium.crypto_aead_xchacha20poly1305_ietf_keygen(); | |
let nonce3 = sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES); | |
let ciphertext3 = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt( | |
'secret message.', | |
null, | |
null, | |
nonce3, | |
key3 | |
) | |
console.log('ciphertext') | |
console.log(ciphertext3) | |
//decryption. | |
let decryptedText3 = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt( | |
null, | |
ciphertext3, | |
null, | |
nonce3, | |
key3 | |
) | |
console.log('decrypted ciphertext') | |
console.log(sodium.to_string(decryptedText3)) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment