You might be operating a small software program enterprise that sells digital downloads – apps, plugins, and even templates. When the client completes the acquisition, you must present them with a license key that they will use to activate and validate the software program.
Right here’s how one can implement such a licensing system in your software program:
- Generate a private and non-private key pair utilizing the RSA algorithm.
- Signal a message with the personal key. The message comprises the client’s e mail handle and the software program SKU.
- The signed message is the license key that’s despatched again to the client’s e mail handle.
- When the client prompts the software program, the license secret’s verified utilizing the general public key.
The Benefits
The benefit right here is that the general public key may be included within the software program’s supply code, there’s no want to make use of a database, and the client can confirm the license key offline with out the necessity to hook up with your server.
Let’s now undergo the implementation steps intimately.
1. Generate Public and Non-public Key Pair
We’ll generate a private and non-private key pair utilizing the RSA algorithm. Launch the terminal and run the next openssl command.
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private_key.pem
It’s going to generate a 2048-bit RSA personal key and put it aside to a file referred to as private_key.pem
within the present folder. Subsequent, we’ll write a command to generate a public key from the personal key.
openssl rsa -pubout -in private_key.pem -out public_key.pem
Now that now we have our keys, let’s print them to the console as we’ll want them within the subsequent step.
openssl pkey -in private_key.pem && openssl pkey -pubin -in public_key.pem
2. Generate a License Key
We’ll write a easy Node.js script to generate a license key. It makes use of the crypto
module to signal the message with the personal key and the fs
module to learn the personal key from the file system.
const crypto = require('crypto');
const fs = require('fs');
// Learn personal key from file system
const privateKey = fs.readFileSync('private_key.pem', 'utf8');
const buyerEmailAddress = 'amit@labnol.org';
const knowledge = Buffer.from(buyerEmailAddress);
const signature = crypto.signal('sha256', knowledge, {
key: privateKey,
padding: crypto.constants.RSA_PKCS1_PSS_PADDING
});
// Convert the signature to base64
const licenseKey = signature.toString('base64');
// Output the consequence
console.log(licenseKey);
3. Confirm a License Key
The license key generated within the earlier step is shipped to the client’s e mail handle and we have to confirm it when the client prompts the software program.
This once more is a straightforward Node.js script that makes use of the crypto
module to confirm the license key with the general public key.
const crypto = require('crypto');
const fs = require('fs');
const buyerEmailAddress = '<>' ;
const licenseKey = '<>' ;
const publicKey = fs.readFileSync('public_key.pem', 'utf8');
const signatureBuffer = Buffer.from(licenseKey, 'base64');
const licenseStatus = crypto.confirm(
'sha256',
Buffer.from(buyerEmailAddress),
{
key: Buffer.from(publicKey),
padding: crypto.constants.RSA_PKCS1_PSS_PADDING
},
signatureBuffer
);
console.log(licenseStatus ? 'Activated' : 'Invalid license key');
License Activation in Google Apps Script
If you’re planning to incorporate activation inside Google Workspace add-ons, you possibly can construct a Google Cloud Operate or a Google Cloud Run service to deal with the license activation.
Your Apps Script code could make a UrlFetch POST request to the online service with the license key and get again the activation standing. In such a case, the general public key needn’t be embedded within the script. Additionally, the consumer’s e mail handle may be simply retrieved utilizing the Session.getActiveUser().getEmail()
methodology.
The Limitations
That is clearly a primary implementation of a software program licensing system that doesn’t deal with all the sting instances. It may be a place to begin however there are various different issues to think about like:
- Find out how to set expiration dates for the license keys.
- Find out how to revoke a license key.
- Find out how to forestall key sharing between customers.