Last active
August 29, 2015 14:20
-
-
Save kjlape/e19a6f6c8a2832324dc1 to your computer and use it in GitHub Desktop.
Crypto/Rando js stuff... Now isomorphic!
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
| if (typeof crypto === 'undefined' || crypto == null) crypto = require('crypto') | |
| var _MAX_CRYPTO_BYTES = 65536 | |
| var _BYTES_IN_UINT32 = 4 | |
| var _MAX_CRYPTO_UINT32 = Math.floor(_MAX_CRYPTO_BYTES / _BYTES_IN_UINT32) | |
| if (!crypto.getRandomValues) | |
| crypto.getRandomValues = function (buffer) { | |
| var randomBytes = crypto.randomBytes(buffer.buffer.byteLength) | |
| var dv = new DataView(buffer.buffer) | |
| for (var i = 0; i < randomBytes.length; ++i) | |
| dv.setInt8(i, randomBytes[i]) | |
| return buffer | |
| } | |
| var expoBackoffMemoCryptoGetRandomUint32 = function (capacity, max) { | |
| capacity = +capacity || 1 | |
| if (max > _MAX_CRYPTO_UINT32) throw new Error("This max value will exceed the crypto values quota.") | |
| max = +max || _MAX_CRYPTO_UINT32 | |
| var i = 0 | |
| var buffer = void 0 | |
| return function () { | |
| return (!buffer || i >= buffer.length) ? | |
| (buffer = new Uint32Array(Math.min((buffer && buffer.length * 2) || capacity, max)), crypto.getRandomValues(buffer), buffer[(i = 1) && 0]) : | |
| buffer[i++] | |
| } | |
| } | |
| function generatePasswordGenerator(consonants, vowels, rndInt) { | |
| return function randoWordNumber(length) { | |
| length = +length || 8 | |
| password = "" | |
| for (var i = 0; i < length; ++i) { | |
| password += rndInt(9) > (1 + (i % 2) * 7) ? | |
| consonants[rndInt(consonants.length - 1)] : | |
| vowels[rndInt(vowels.length - 1)] | |
| } | |
| return password | |
| } | |
| } | |
| var _cryptoRandoPasswordGenerator = generatePasswordGenerator("bcdfghjklmnprstvwxyz", "aeiou", cryptoRndInt) | |
| var _randoPasswordGenerator = generatePasswordGenerator("bcdfghjklmnprstvwxyz", "aeiou", rndInt) | |
| var randoPasswordGeneratorAppendNumber = function (generator, rndInt) { | |
| return function (length) { | |
| length = typeof length === 'undefined' || length === null ? 8 : length | |
| return (length === 0 ? '' : generator(length - 2)) + rndInt(10, 99) | |
| } | |
| } | |
| var cryptoRandoPasswordGenerator = randoPasswordGeneratorAppendNumber(_cryptoRandoPasswordGenerator, cryptoRndInt) | |
| var randoPasswordGenerator = randoPasswordGeneratorAppendNumber(_randoPasswordGenerator, rndInt) | |
| var cryptoRandoPasswordNumGenerator = generatePasswordGenerator("bcdfghjklmnprstvwxyz2589", "aeiou130", cryptoRndInt) | |
| var randoPasswordNumGenerator = generatePasswordGenerator("bcdfghjklmnprstvwxyz2589", "aeiou130", rndInt) | |
| function rndInt(a, b) { | |
| if (b < a) throw new Error("First argument should be less than second in range.") | |
| if (!b) (b = a), (a = 0) | |
| return Math.floor(Math.random() * (b - a + 1)) + a | |
| } | |
| getRandomValue = expoBackoffMemoCryptoGetRandomUint32() | |
| function cryptoRndInt(a, b) { | |
| if (b < a) throw new Error("First argument should be less than second in range.") | |
| if (!b) (b = a), (a = 0) | |
| return getRandomValue() % (b - a + 1) + a | |
| } | |
| console.log( | |
| cryptoRandoPasswordGenerator(rndInt(8, 16)), | |
| randoPasswordGenerator(rndInt(8, 16)), | |
| cryptoRandoPasswordNumGenerator(rndInt(8, 16)), | |
| randoPasswordNumGenerator(rndInt(8, 16)) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment