Node.JS Generate random UUID in Node.JS Quick and easy thanks to crypto library. In a new .js file paste the following code: const crypto = require('crypto'); console.log(crypto.randomUUID()); console.log(crypto.randomUUID()); console.log(crypto.randomUUID()); console.log(crypto.randomUUID()); console.log(crypto.randomUUID()); console.log(crypto.randomUUID()); console.log(crypto.randomUUID()); console.log(
Node.JS Colors in console with Node.JS I recently wanted to make the output of my Node.JS applications a little more easy to read and I discovered that it's possible to print with some colors an other format. Here is a code that you can put inside a colors.js file and import it anywhere. // Others
Node.JS Find a String inside a JSON object with recursivity in Node.JS Simple thing: const THE_UNKNOWN = { a: 2, b: "hello", c: { d: 34.21, e: "TARGET", f: { g: "not easter egg", h: { i: "TARGET" } }, j: [ { k: 308 }, { l: "TARGET" } ], m: [ "some", "string", "array", "TARGET" ] } }; function find(what, her
Node.JS Scramble string N times in Node.JS let myStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz\\\""; function scramble(str, times) { str = Buffer.from(str, 'utf8'); console.log(str + " >>>----> " + str.length + " chars"); for (let i = 1; i <= times; i++) { let orig = Math.floor(Math.random() * str.length); let dest = Math.floor(Math.random() * str.length); let temp = str[orig]; str[orig] = str[dest]
Node.JS Generate UUID-like strings with crypto in Node.JS const crypto = require('crypto'); let uuidStyleStr = crypto.randomBytes(4).toString('hex') + "-" + crypto.randomBytes(2).toString('hex') + "-" + crypto.randomBytes(2).toString('hex') + "-" + crypto.randomBytes(2).toString('hex') + "-" + crypto.randomBytes(6).toString('hex'); console.log(uuidStyleStr); Sample output: b99c8dbc-1258-a348-3046-1674613bd86a
Node.JS Copy-Paste-Run-Ready string encryption / decryption with AES-256 in Node.JS /* --- Code ready to copy -> paste -> run --- */ // Libraries const crypto = require('crypto'); const ENCODING = "hex"; // hex or base64 // Implementations function encryptAES256(clearMsg, key) { let iv = crypto.randomBytes(16); // Generating 16 byte random IV let cipher = crypto.createCipheriv( "aes256", crypto.createHash("sha256").update(key, "utf8").digest(), // 32 bytes
Node.JS Shortest and simplest string encryption / decryption with AES-256 in Node.JS Copy and paste the following code in a .js file, then run it. // Libraries const crypto = require('crypto'); // Constants const OUTPUT_FORMAT = "hex"; const ALGORITHM = "aes256"; const KEY = "012345ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const IV = "ABCDEF0123456789"; // Implementations function encrypt(message) { let cipher = crypto.createCipheriv( ALGORITHM, Buffer.from(KEY), Buffer.from(IV) ); return (Buffer.concat(
Node.JS SCRYPT demo in Node.JS v15 SCRYPT is yet another password-based key derivation function that allows you to tweak its arguments in order to discourage brute-force attacks. Basically you can adjust the options to make it slower, and therefore brute-force attacks will need more time to be performed. Here is an example of how to use
Node.JS Password Based Key Derivation Function 2 (PBKDF2) demo in Node.JS v15 A quick demo on how to use Password Based Key Derivation Function 2 (PBKDF2) to generate keys and IVs to use with all different ciphers available in Node.JS v15.x. const crypto = require('crypto'); const PASSWORD = "My_Secret_Password"; // Could be the password/key used to encrypt a message
Node.JS Encrypt / Decrypt with AES (CCM & GCM) in Node.JS (Part 2 – Concatenate the Authentication Tag) In the past I wrote a post about encrypting and decrypting text strings with AES in GCM and CCM mode. At the time, the demo was written to show how the algorithm works in those modes. Today I needed to handle the inclusion of such Authentication Tag inside the encrypted
Cryptography Node.JS Ciphers Info in version 15.x After the deprecation of methods createCipher(…) and createDecipher(…) due to the introduction of createCipheriv(…) and createDecipheriv(…) a lot of us had issues when specifying the keys and IVs because sometimes the key would be the incorrect size and same for the IVs. I developed a kind of brute force tester
Node.JS Encrypt / Decrypt strings with RSA in Node.JS Interesting things that go through my mind during lock-down haha! const crypto = require('crypto'); const PASSPHRASE = 'I had learned that some things are best kept secret.'; const KEY_PAIR_OPTIONS = { modulusLength: 2048, publicKeyEncoding: { type: 'spki', format: 'pem' }, privateKeyEncoding: { type: 'pkcs8', format: 'pem', cipher: 'aes-256-cbc', passphrase: PASSPHRASE } }; const KEY_PAIR
Node.JS Random Password Generator in Node.JS The other day I thought about how to generate passwords from random arrays. Here is what I came up with: /* * RaNdom Password Generator */ const crypto = require('crypto'); const PASSWORD_LENGTH = 18; const LOWERCASE_ALPHABET = 'abcdefghijklmnopqrstuvwxyz'; // 26 chars const UPPERCASE_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; // 26 chars const NUMBERS = '0123456789'; // 10 chars const SYMBOLS
Node.JS How to parse JSON files in Node.JS? Quick tip using fs: let data = fs.readFileSync("myFile.json", 'utf8'); console.log(typeof JSON.parse(data)); There is another way to do this that requires less effort. Using require directly, like so: const data = require("myFile.json"); console.log(typeof data); Or you can create a function like this
Node.JS How to pack Node.JS project into a tarball? Today I needed to install my brand new NodeJS-developed CLI application in my computer. Important! Don’t forget to add this to your package.json file: "bin": { "skynet": "./bin/skynetApp.js" }, It’s basically the name of the command and the NodeJS file it will execute, in this case the
Node.JS How to generate RSA key pair in Node.JS? Very straight forward process. Not complicated at all, I would say that the most difficult part would be to actually tweak the options, however the code below actually works and provides you the time it took to generate the key pair as well, just for informational purposes. Run the following
Node.JS How to hash files in Node.JS The process is quite simple actually. Unfortunately ,as much things in Node.js, it works asynchronously, for some applications this will be perfect, but for some others it will be a mess to wait for the freaking hash to do something else, anyway I am not going to discuss that
Node.JS How to create a Node.JS application with Express Generator I am assuming that you already have Node.JS installed. If you don’t, worry not! you can get your own copy today from the Node.JS download section! Using ExpressJS is very quick and easy. It generates all the file structure needed for a web application. You can select
Node.JS Base64 URL Safe Encoding / Decoding (RFC-7515) A few days back I was required to encode some string in base 64 URL safe. After some research I found that there are several variants of base 64. The one implemented in the code below corresponds to RFC-7515. Here is the code: function encodeBase64Url(str) { let s = new Buffer(
Node.JS How to generate random bytes in Node.JS Easy! I discovered this recently and it is interesting to me. Here is a quick example: const crypto = require('crypto'); console.log(crypto.randomBytes(1).toString("hex")); console.log(crypto.randomBytes(2).toString("hex")); console.log(crypto.randomBytes(4).toString("hex")); console.log(crypto.randomBytes(8).toString("hex")); console.log(
Node.JS How to Sign data and Verify signature in Node.JS Another example that occurred to me. Very useful. What will we need? * Private key in PEM format * Public key in PEM format To get those you will have use OpenSSL, please take a look at this post How to generate RSA public and private keys with OpenSSL. For this example
Node.JS Elliptic Curve Diffie-Hellman and AES Example in Node.JS Recently I learned how to generate shared secrets using ECDH in Node.JS, but I still had to know how to use this shared secret. Here is one application for it. Use the ECDH to generate a shared secret and then use that shared secret to cipher/decipher messages between
Node.JS How to generate Diffie Hellman key pair in Node.JS Super easy using crypto library. You can create a file called DiffieHellmanKeyPairGeneration.js for example and paste this code inside: console.log('\n- --- ( Diffie Hellman Key Pair Generator ) --- -'); var crypto = require('crypto'); var bitSize = 2048; var dh = crypto.createDiffieHellman(bitSize); dh.generateKeys(); console.log('Private Key:
Node.JS How to list supported Ciphers, Hashes, and Curves for Crypto in Node.JS Not a complicated task. Here is the code: var crypto = require('crypto'); console.log(); console.log('[ SUPPORTED CIPHERS ]\n') console.log(crypto.getCiphers()); console.log(); console.log('[ SUPPORTED HASHES ]\n') console.log(crypto.getHashes()); console.log(); console.log('[ SUPPORTED CURVES ]\n') console.log(crypto.getCurves()); Your output