Last active
September 23, 2020 23:45
-
-
Save AlastairTaft/294e88c5105a79d40fd57707856627d4 to your computer and use it in GitHub Desktop.
Generates a URL safe key with sufficient bit complexity
This file contains hidden or 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
/** | |
* Generate a URL safe key with sufficient bits of complexity. | |
*/ | |
const generateKey = function(bits){ | |
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
+ "abcdefghijklmnopqrstuvwxyz" | |
+ "012345678910" | |
+ "-_" | |
var expectedEntroy = Math.pow(2, bits) | |
var key = '' | |
while(Math.pow(alphabet.length, key.length) < expectedEntroy){ | |
key += alphabet[Math.floor(Math.random() * alphabet.length)] | |
} | |
return key | |
} | |
// Will result in at least 256 bits of entropy | |
console.log(generateKey(256)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment