From b31b5feb849078c5156ce1c82e86289368c0d2d1 Mon Sep 17 00:00:00 2001 From: Roland Eigelsreiter Date: Thu, 23 Nov 2023 17:09:54 +0100 Subject: [PATCH] added more demos --- README.md | 29 +++++++++++++++++++++++++++++ demo/convenient-usage.js | 13 +++++++++++++ demo/raw-usage.js | 25 +++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 demo/convenient-usage.js create mode 100644 demo/raw-usage.js diff --git a/README.md b/README.md index 9a302b0..a14322c 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,35 @@ cool if you leave a follow or spend some virtual coffee. For more demos see in folder `demo`. ```js +// test convenient methods +let key = 'mypassword' +let message = ['this can be any data type 😎 文', 123] +let associatedData = 'Some data 😋 文 This data is not contained in the encrypt output. You must pass the same data to encrypt and decrypt in order to be able to decrypt the message.' +let encrypted = JsAscon.encryptToHex(key, message, associatedData) +let decrypted = JsAscon.decryptFromHex(key, encrypted, associatedData) +JsAscon.assertSame(JSON.stringify(message), JSON.stringify(decrypted), 'Encryption/Decryption to hex failed') + +// raw usage of basic methods +// key must be 16 bytes or 20 bytes, depending on variant +const key = [0x90, 0x80, 0x70, 0x60, 0x50, 0x40, 0x30, 0x20, 0x10, 0xAA, 0x90, 0x90, 0x90, 0x90, 0xCC, 0xEF] +// nonce must be 16 bytes and should always be random bytes, you must use same nonce for encrypt and decrypt the same message +const nonce = JsAscon.getRandomUintArray(16) +// this is the text you want to encrypt +const plaintext = 'Hi, i am a secret message!' +// associated data is not being encrypted, but is taken into account in the ciphertext +// this means, you can only decrypt when you pass the exact same associated data to the decrypt function as well +// so you can make sure that associated data and plaintext is not manipulated for given encrypted message +// this is optional and can be an empty string +const associatedData = 'Some data to pass to encryption and decryption - This data is not contained in the ciphertext output.' +const ciphertextByteArray = JsAscon.encrypt(key, nonce, associatedData, plaintext) +const plaintextDecrypted = JsAscon.decrypt(key, nonce, associatedData, ciphertextByteArray) + +console.log("Hash") +console.log(JsAscon.hash("Testmessage")) + +console.log("Mac") +console.log(JsAscon.mac(key, "Testmessage")) + ``` See `tests/performance.html` for some tests with various message data size. diff --git a/demo/convenient-usage.js b/demo/convenient-usage.js new file mode 100644 index 0000000..35cd998 --- /dev/null +++ b/demo/convenient-usage.js @@ -0,0 +1,13 @@ +// depending on your environment, include JsAscon as module, script tag or require from nodejs +//const JsAscon = require('js-ascon') // when using as installed npm module +const JsAscon = require('../dist/ascon') + +// test convenient methods +let key = 'mypassword' +let message = ['this can be any data type 😎 文', 123] +let associatedData = 'Some data 😋 文 This data is not contained in the encrypt output. You must pass the same data to encrypt and decrypt in order to be able to decrypt the message.' +let encrypted = JsAscon.encryptToHex(key, message, associatedData) +let decrypted = JsAscon.decryptFromHex(key, encrypted, associatedData) +JsAscon.assertSame(JSON.stringify(message), JSON.stringify(decrypted), 'Encryption/Decryption to hex failed') + +console.log('convienient-usage.js successfully done') \ No newline at end of file diff --git a/demo/raw-usage.js b/demo/raw-usage.js new file mode 100644 index 0000000..91288da --- /dev/null +++ b/demo/raw-usage.js @@ -0,0 +1,25 @@ +// depending on your environment, include JsAscon as module, script tag or require from nodejs +//const JsAscon = require('js-ascon') // when using as installed npm module +const JsAscon = require('../dist/ascon') + +// key must be 16 bytes or 20 bytes, depending on variant +const key = [0x90, 0x80, 0x70, 0x60, 0x50, 0x40, 0x30, 0x20, 0x10, 0xAA, 0x90, 0x90, 0x90, 0x90, 0xCC, 0xEF] +// nonce must be 16 bytes and should always be random bytes, you must use same nonce for encrypt and decrypt the same message +const nonce = JsAscon.getRandomUintArray(16) +// this is the text you want to encrypt +const plaintext = 'Hi, i am a secret message!' +// associated data is not being encrypted, but is taken into account in the ciphertext +// this means, you can only decrypt when you pass the exact same associated data to the decrypt function as well +// so you can make sure that associated data and plaintext is not manipulated for given encrypted message +// this is optional and can be an empty string +const associatedData = 'Some data to pass to encryption and decryption - This data is not contained in the ciphertext output.' +const ciphertextByteArray = JsAscon.encrypt(key, nonce, associatedData, plaintext) +const plaintextDecrypted = JsAscon.decrypt(key, nonce, associatedData, ciphertextByteArray) + +console.log("Hash") +console.log(JsAscon.hash("Testmessage")) + +console.log("Mac") +console.log(JsAscon.mac(key, "Testmessage")) + +console.log('raw-usage.js successfully done') \ No newline at end of file