Snippets of Javascript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/* node.js nonce: crypto-quality random session key (filename safe base64, rfc4648) */ | |
const btoa = require( 'btoa' ); | |
const crypto = require( 'crypto' ); | |
function nonce( length ) { | |
const rndLen = 1+(( (length)+(length<<1) +3)>>2); | |
const randomArray = crypto.randomBytes( rndLen ); | |
const b64 = btoa( String.fromCharCode.apply( null, randomArray ) ); | |
return b64 | |
.replace( /[\/]/g, '_' ) | |
.replace( /[+]/g, '-' ) | |
.replace( /[=]/g, '' ) | |
.substring( 0, length ); | |
} | |
module.exports = nonce; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment