Skip to content

Instantly share code, notes, and snippets.

@sayanriju
Forked from RobK/serial.js
Last active July 2, 2022 19:07
Show Gist options
  • Save sayanriju/ed14ef9bcae9b9188833e583313b0d21 to your computer and use it in GitHub Desktop.
Save sayanriju/ed14ef9bcae9b9188833e583313b0d21 to your computer and use it in GitHub Desktop.
Generate Random Serial Keys

Synopsis

This is a simple package to generate (pseudo) random strings of specified length separated into "blocks" of specified blockLengths by specified separator characters. This makes this package suitable for generating license or product or serial keys.

The similar looking characters (and numbers) I, 1, 0 and O are always left out of the generated strings to avoid confusion.

Note that the generated keys are just random strings and there's no provision to check their validity cryptographically.

Installation

From npm registry:

npm i -S generate-serial-key

OR

Directly from gist:

npm i -S gist:ed14ef9bcae9b9188833e583313b0d21

Usage Example

The package exposes a single function with signature generate(length = 16, separator = '-', blockLength = 4) which may be used as follows:

const serial = require("generate-serial-key")

serial.generate() // Outputs 'AG8D-ZGYG-A8YV-PVMK'
serial.generate(20, "_", 5) // Outputs 'UL3MV_BTPXB_MBEU9_QK3MC'

Acknowledgement

https://gist.github.com/RobK/8172cd2a358efeca715e

License

MIT

/**
* 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.
*
*/
const oneRandomKey = function () {
return Math.random()
.toString(36)
.toUpperCase()
.slice(2)
.replace(/[01IO]/g, '')
}
const randomKeyOfSpecificLength = function (len) {
let str = oneRandomKey()
while (str.length < len) {
str += oneRandomKey()
}
return str.slice(0, len)
}
exports.generate = function (length = 16, separator = '-', blockLength = 4) {
const license = randomKeyOfSpecificLength(length)
const regexp = new RegExp(`(\\w{${blockLength}})`, 'g')
return license.replace(regexp, `$1${separator}`).substr(0, (length + Math.round(length / blockLength)) - 1)
}
{
"name": "generate-serial-key",
"version": "2.0.0",
"description": "A simple package to generate random serial key strings",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "gist",
"url": "https://gist.github.com/sayanriju/ed14ef9bcae9b9188833e583313b0d21"
},
"keywords": [
"serial key",
"product key",
"license key",
"key"
],
"author": "Sayan \"Riju\" Chakrabarti <s26c.sayan@gmail.com> (https://sayanriju.github.io/blog)",
"license": "MIT",
"homepage": "https://gist.github.com/sayanriju/ed14ef9bcae9b9188833e583313b0d21"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment