license-key
A library for generating and verifying license keys without requiring an Internet connection. For further protection, you can of course validate the license key over the Internet.
Features
- Does not require an Internet connection.
- Easy to revoke specific license keys in a software update.
- Not possible to disassemble an application to gain insight into how to generate a 100% working key since the verification process doesn't check the whole license key.
For more information, read Implementing a Partial Serial Number Verification System in Delphi which this crate was based upon.
Anatomy of a license key
Every license key consists of a seed, a payload and a checksum. Each byte in the payload is an operation of the seed and an initialization vector. The 16-bit checksum is there to quickly check if the key is valid at all, while the seed is a 64-bit hash of something that identifies the license key owner such as an e-mail address or similar.
The size of the payload depends on how big the initialization vector is. In the example below, we are using a 5-byte intitialization vector which results in a 5-byte payload.
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│0x0│0x1│0x2│0x3│0x4│0x5│0x6│0x7│0x8│0x9│0xa│0xb│0xc│0xd│0xe│0xf│
├───┴───┴───┴───┴───┴───┴───┴───┴───┼───┴───┴───┴───┴───┼───┴───┤
│ SEED │ PAYLOAD │ CHECK │
│ │ │ SUM │
└───────────────────────────────────┴───────────────────┴───────┘
Generating a license key
use *;
// Define a hasher that will hash the seed and a initialization vector.
// DON'T USE THIS ONE. It's only for demonstrational purposes.
// Create a license generator
// We use only four triplets in our initialization vector,
// but in a real world scenario you would want to use a lot more.
let generator = new;
// Generate a license key using a seed.
// A seed is unique per license key, and could be a hash of an e-mail address or similar.
// You can later block individual seeds during verification.
let key = generator.generate;
// Write the key in hex format to the console.
// This will output something like: 112210F4B2D230A229552341B2E723
println!;
Verifying a license key
use *;
// Use the exact same hasher that we used when generating the key
// Create the license key verifier
let mut verifier = new;
// Block a specific seed.
// You might want to do this if a key was leaked or the the
// license key owner requested a refund.
verifier.block;
// Parse a key in hex format
let key = ;
// Verify the license key
match verifier.verify