Last active
December 20, 2024 19:56
-
-
Save Skhmt/5870d0eb3d23d3c7a6ab91e7a2e7442e to your computer and use it in GitHub Desktop.
Revisions
-
Skhmt revised this gist
Dec 20, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ const decimalNumbers = '0123456789'; console.log(createRandomString(12, true, 'password', true)); function createRandomString(length = 16, useSymbols = true, prefix = '', useNumbers = true) { let possibleChars = charactersUpper + charactersLower + (useSymbols ? symbols : '') + (useNumbers ? decimalNumbers : ''); const requiredChars = [rand(charactersUpper, 1), rand(charactersLower, 1)]; -
Skhmt revised this gist
Dec 6, 2024 . 1 changed file with 5 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -28,13 +28,8 @@ function createRandomString(length = 8, useSymbols = false, prefix = '', useNumb } function shuffleArray(arr) { for (let i = arr.length - 1; i >= 0; i--) { const j = randomInt(i + 1); [arr[i], arr[j]] = [arr[j], arr[i]]; } @@ -49,4 +44,8 @@ function rand(str, num) { output.push(str[n % str.length]); } return output; } function randomInt(max) { return crypto.getRandomValues(new Uint32Array(1))[0] % max; } -
Skhmt revised this gist
Dec 6, 2024 . 1 changed file with 6 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ const charactersLower = 'abcdefghijklmnopqrstuvwxyz'; const symbols = '!@#$%&?^*.:,;_-'; const decimalNumbers = '0123456789'; console.log(createRandomString(12, true, 'password', true)); function createRandomString(length = 8, useSymbols = false, prefix = '', useNumbers = false) { let possibleChars = charactersUpper + charactersLower + (useSymbols ? symbols : '') + (useNumbers ? decimalNumbers : ''); @@ -14,9 +14,13 @@ function createRandomString(length = 8, useSymbols = false, prefix = '', useNumb if (useSymbols) requiredChars.push(rand(symbols, 1)); if (useNumbers) requiredChars.push(rand(decimalNumbers, 1)); if (length < requiredChars.length + prefix.length) { throw new Error('Length must be at least ' + (requiredChars.length + prefix.length)); } const randLength = length - requiredChars.length - prefix.length; const unshuffled = requiredChars.concat(rand(possibleChars, randLength)); const shuffled = shuffleArray(unshuffled); -
Skhmt revised this gist
Dec 6, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -16,7 +16,7 @@ function createRandomString(length = 8, useSymbols = false, prefix = '', useNumb const randLength = length - requiredChars.length - prefix.length; const unshuffled = (randLength > 0) ? requiredChars.concat(rand(possibleChars, randLength)) : requiredChars; const shuffled = shuffleArray(unshuffled); -
Skhmt revised this gist
Dec 6, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -16,7 +16,7 @@ function createRandomString(length = 8, useSymbols = false, prefix = '', useNumb const randLength = length - requiredChars.length - prefix.length; const unshuffled = (randLength > 0) ? requiredChars.concat(rand(possibleChars, randLength) : requiredChars; const shuffled = shuffleArray(unshuffled); -
Skhmt revised this gist
Dec 6, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ import * as crypto from 'node:crypto'; const charactersUpper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; const charactersLower = 'abcdefghijklmnopqrstuvwxyz'; const symbols = '!@#$%&?^*.:,;_-'; const decimalNumbers = '0123456789'; -
Skhmt created this gist
Dec 6, 2024 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ import * as crypto from 'node:crypto'; const charactersUpper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; const charactersLower = 'abcdefghijklmnopqrstuvwxyz'; const symbols = '!@#$%&?^*.:,;_-'; const decimalNumbers = '0123456789'; console.log(createRandomString(16, true, 'pw', true)); function createRandomString(length = 8, useSymbols = false, prefix = '', useNumbers = false) { let possibleChars = charactersUpper + charactersLower + (useSymbols ? symbols : '') + (useNumbers ? decimalNumbers : ''); const requiredChars = [rand(charactersUpper, 1), rand(charactersLower, 1)]; if (useSymbols) requiredChars.push(rand(symbols, 1)); if (useNumbers) requiredChars.push(rand(decimalNumbers, 1)); const randLength = length - requiredChars.length - prefix.length; const unshuffled = requiredChars.concat(rand(possibleChars, (randLength > 0) ? randLength : 0)); const shuffled = shuffleArray(unshuffled); return prefix + shuffled.join(''); } function shuffleArray(arr) { const randArr = new Uint32Array(arr.length + 1); crypto.getRandomValues(randArr); const randRangeArr = randArr.map(x => x % arr.length); for (let i = arr.length - 1; i >= 0; i--) { const j = randRangeArr[i]; [arr[i], arr[j]] = [arr[j], arr[i]]; } return arr; } function rand(str, num) { const randArr = new Uint32Array(num); crypto.getRandomValues(randArr); let output = []; for (let n of randArr) { output.push(str[n % str.length]); } return output; }