Skip to content

Instantly share code, notes, and snippets.

@erdesigns-eu
Last active July 13, 2023 15:15
Show Gist options
  • Save erdesigns-eu/82f0ab2d8f851fc87ed29723add93967 to your computer and use it in GitHub Desktop.
Save erdesigns-eu/82f0ab2d8f851fc87ed29723add93967 to your computer and use it in GitHub Desktop.
License key generator and validator in Javascript
/*
This generator does not generate valid keys for any software, its merely a demonstration of
how to create a simple license key registration and validation system. You could store the
generated license keys on the server, and add online-validation when the user wants to use
the entered key to register your application.
** DO NOT ** use these as is in your production code (DUHH!)
*/
// Master key is the sum of the license key characters. This sum is the same for every key.
const masterKey = 399;
// These are the characters that are valid for the license key, change these as you like.
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
// This is the prefix that is used to start your license key. You can change this for different products.
const prefix = 'SAMP';
/**
* Generate license key.
* This function generates multiple license keys.
*
* ** Disclaimer **
* This is just a very simple license mechanism that can be integrated into
* your own software. You can change the charset, length and code to generate
* your own license keys - but update the validation function accordingly.
*/
function generateRegCode() {
// Add the prefix to the licensekey.
let result = prefix;
let j = 0;
// Keep trying to find a valid license key.
while (j !== masterKey) {
result = prefix;
j = 0;
// We need 15 characters for our license key + the prefix of 4 characters + 3 dashes = 22 characters total.
for (let i = 1; i <= 15; i++) {
const k = Math.floor(Math.random() * charset.length);
j += k;
result += charset[k];
}
}
// Add the dashes to make the license key easier to read/write.
result = result.slice(0, 4) + '-' + result.slice(4, 8) + '-' + result.slice(8, 12) + '-' + result.slice(12);
// Return the generated license key.
return result;
}
/**
* Validate license key.
* This function validates generated license keys and can be used
* in your website to check if the user entered a valid license key.
* ** DO NOT ** put this code on the client-side!!
*/
function validateRegCode(regCode) {
// Total length should be 22 characters long.
if (regCode.length !== 22) {
return false;
}
// If there are no dashes on these positions, the formatting is wrong.
if (
regCode[4] !== '-' ||
regCode[9] !== '-' ||
regCode[14] !== '-'
) {
return false;
}
// Calculate the sum of the license key.
let j = 0;
for (let i = 5; i < regCode.length; i++) {
if (regCode[i] !== '-') {
const k = charset.indexOf(regCode[i]);
if (k === -1) {
return false;
}
j += k;
}
}
// Validate the sum with the masterkey.
return j === masterKey;
}
/**
* Generate a list of License keys.
*/
const licenseKeys = Array.from({ length: 10 }).map(() => (generateRegCode()));
/**
* Validate the generated list of license keys.
*/
console.log('Valid generated license keys:');
licenseKeys.forEach((licenseKey) => {
if (validateRegCode(licenseKey)) {
console.log(`${licenseKey} = VALID`);
} else {
console.log(`${licenseKey} = INVALID!`);
}
});
/**
* List of BAD (random) license keys.
*/
const badLicenseKeys = [
'SAMP-111S-3069-GT7862Q',
'SAMP-661S-1234-GT7862A',
'SAMP-6254-4A4A-GHS62Q6',
'SAMP-4R2U-23X9-84E367X',
'SAMP-W9A1-X88O-ZYV8P42'
];
/**
* Validate list of BAD license keys.
*/
console.log('\nBad random license keys:')
badLicenseKeys.forEach((licenseKey) => {
if (validateRegCode(licenseKey)) {
console.log(`${licenseKey} = VALID`);
} else {
console.log(`${licenseKey} = INVALID!`);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment