Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am rdnsyh on github.
  • I am rdnsyh (https://keybase.io/rdnsyh) on keybase.
  • I have a public key ASAfeRr3hZBX47Eq7DtrZePbwyBTUIxPNjvJGbRmehaJkgo

To claim this, I am signing this object:

@rdnsyh
rdnsyh / random.md
Created August 20, 2017 18:33 — forked from joepie91/random.md
Secure random values (in Node.js)

Not all random values are created equal - for security-related code, you need a specific kind of random value.

A summary of this article, if you don't want to read the entire thing:

  • Don't use Math.random(). There are extremely few cases where Math.random() is the right answer. Don't use it, unless you've read this entire article, and determined that it's necessary for your case.
  • Don't use crypto.getRandomBytes directly. While it's a CSPRNG, it's easy to bias the result when 'transforming' it, such that the output becomes more predictable.
  • If you want to generate random tokens or API keys: Use uuid, specifically the uuid.v4() method. Avoid node-uuid - it's not the same package, and doesn't produce reliably secure random values.
  • If you want to generate random numbers in a range: Use random-number-csprng.

You should seriously consider reading the entire article, though - it's

//Trying forEach method
function filter(array, fn){
var result = [];
// TODO: return a new array with only the value that pass the fn test. (meaning the fn returned true)
// function logArrayElements(element, index, array) {
// if(fn(element)){
// result[index] = element;
// }
function filter(array, fn){
var result = [];
// TODO: return a new array with only the value that pass the fn test. (meaning the fn returned true)
// for (var i = 0; i < array.length; i++){
// if(fn(array[i])){
// result[i] = array[i];
// }
// }
// function logArrayElements(element, index, array) {
function filter(array, fn){
var result = [];
// TODO: return a new array with only the value that pass the fn test. (meaning the fn returned true)
for (var i = 0; i < array.length; i++){
if(even(array[i])){
result[i] = array[i];
console.log(result);
//return result;
}
console.log(result);
let items = [
{id: 'a1', name: 'pisang', price: 22},
{id: 'b2', name: 'keju', price: 34},
{id: 'c3', name: 'roti', price: 11},
];
//console.log(items[0]);
//var letItems = items;//why do this though?
//console.log(Object.keys(items));
//console.log(Object.keys(letItems));
let items = [
{id: 'a1', name: 'pisang', price: 22},
{id: 'b2', name: 'keju', price: 34},
{id: 'c3', name: 'roti', price: 11},
];
//console.log(items[0]);
var letItems = items;//why do this though?
//console.log(Object.keys(items));
//console.log(Object.keys(letItems));
let items = [
{id: 'a1', name: 'pisang', price: 22},
{id: 'b2', name: 'keju', price: 34},
{id: 'c3', name: 'roti', price: 11},
];
//console.log(items[0]);
var letItems = items;//why do this though?
//console.log(Object.keys(items));
//console.log(Object.keys(letItems));