Files
common/docs/examples/util-crypto/01_encrypt_decrypt_message_nacl/index.js
T
2020-04-07 21:48:07 +02:00

28 lines
1.1 KiB
JavaScript
Executable File

/* eslint-disable header/header */
/* eslint-disable @typescript-eslint/require-await */
/* eslint-disable @typescript-eslint/unbound-method */
const { naclDecrypt, naclEncrypt } = require('@polkadot/util-crypto');
async function main () {
const secret = new Uint8Array(32);
const messagePreEncryption = new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]);
const noncePreEncryption = new Uint8Array(24);
// Encrypt the message
const { encrypted, nonce } = naclEncrypt(messagePreEncryption, secret, noncePreEncryption);
// Show contents of the encrypted message
console.log(`Encrypted message: ${JSON.stringify(encrypted, null, 2)}`);
// Decrypt the message
const messageDecrypted = naclDecrypt(encrypted, nonce, secret);
// Convert each Uint8Array to a string for comparison
const isMatch = messagePreEncryption.toString === messageDecrypted.toString;
// Verify that the decrypted message matches the original message
console.log(`Does the decrypted message match the original message? ${isMatch}`);
}
main().catch(console.error).finally(() => process.exit());