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(crypto.randomBytes(16).toString(“hex”)); console.log(crypto.randomBytes(32).toString(“hex”)); console.log(crypto.randomBytes(64).toString(“hex”)); When you run this code you might see something similar to this: E:\NodeJS>node randomCrypto.js d8 dccd 0709fee2 67d6d7c51cc59f0d af19069e601e47a9141d27d7d828a9d0 f83b7547452099462061734791da57443cc60828de7dcb8f7494eedceb3c889c e2e4a1faaf4190898aaf4fa786e20fe076237854ae4628127cb2eda4bafd3000b42788e347ade94d0c98614a40586b71ded3044879f791adedd6c0d4bb22e9ac The argument passed to the function is the length in bytes, when …

How to Encode / Decode to / from base64 in Node.JS

Today I ran into this basic problem. A quick search on google gave me the answer, I tested it, it worked, and the rest is history. Here is the code. let plainText = “Hello World!”; console.log(“Plaint text = ” + plainText); // Encoding to base64 let base64 = new Buffer(plainText).toString(‘base64’); console.log(“Encoded to base64 = ” + base64); // Decoding from …

How to fold / unfold all regions in Visual Studio Code

With JSON files of around 900 lines, this option is very useful to have an organized view. I highly recommend to check the links, there is a lot of things you can do with Visual Studio Code, my version is 1.10.2, and the following shortcuts are working. Ctrl+K Ctrl+0 Fold (collapse) all regions Ctrl+K Ctrl+J Unfold (uncollapse) all regions Sources …