* refactor: Destructure imports together from same source in example util-crypto 01 * refactor: Destructure imports together example util-crypto 02. Avoid using default import. Show data to user * refactor: Destructure imports together example util-crypto 03. Show data to user * refactor: Split import over two lines looks cleaner * fix: Address review comments. Update dependencies in examples
24 lines
940 B
JavaScript
Executable File
24 lines
940 B
JavaScript
Executable File
const { naclEncrypt, naclDecrypt } = 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());
|