Last active
June 28, 2022 02:54
-
-
Save RobK/8172cd2a358efeca715e to your computer and use it in GitHub Desktop.
Generate Random Serial Keys
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
/** | |
* Created by Robert Kehoe on 09/06/2014. | |
* MIT Licensed. | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining | |
* a copy of this software and associated documentation files (the | |
* "Software"), to deal in the Software without restriction, including | |
* without limitation the rights to use, copy, modify, merge, publish, | |
* distribute, sublicense, and/or sell copies of the Software, and to | |
* permit persons to whom the Software is furnished to do so, subject to | |
* the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be | |
* included in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
* | |
*/ | |
function generateSerialKeys(length, separator) { | |
separator = separator || '-'; | |
var license = new Array(length + 1).join((Math.random().toString(36) + '00000000000000000').slice(2, 18)).slice(0, length); | |
return license.toUpperCase().replace(/(\w{4})/g, '$1' + separator).substr(0, length + Math.round(length/4)-1); | |
} | |
console.log(generateSerialKeys(24, ' ')); // JXG5 QDER DNXK O6R0 JXG5 QDER | |
console.log(generateSerialKeys(24)); // QY3R-9Q31-FF5K-A9K9-QY3R-9Q31 | |
console.log(generateSerialKeys(20)); // HJ0A-EUXD-7U95-DN29-HJ0A | |
console.log(generateSerialKeys(16)); // 41X3-U8D4-SKP2-2O6R |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just what I wanted! Thanks a lot!
Edit: FWIW, I've made an npm package for this, with some modifications.