1 个不稳定版本
0.1.0 | 2020年7月13日 |
---|
#13 in #generate-keys
58 每月下载量
19KB
208 代码行
license-key
一个用于生成和验证许可证密钥的库,无需互联网连接。当然,您也可以通过互联网验证许可证密钥以获得进一步的保护。
功能
- 无需互联网连接。
- 在软件更新中轻松撤销特定的许可证密钥。
- 由于验证过程不检查整个许可证密钥,因此无法通过反汇编应用程序来了解如何生成100%有效的密钥。
有关更多信息,请阅读在 Delphi 中实现部分序列号验证系统,本库基于该系统。
许可证密钥的结构
每个许可证密钥由一个种子、一个有效载荷和一个校验和组成。有效载荷中的每个字节都是种子和初始化向量的操作。16位校验和用于快速检查密钥是否有效,而种子是用于标识许可证密钥所有者(如电子邮件地址或类似)的某物的64位哈希值。
有效载荷的大小取决于初始化向量的大小。在下面的示例中,我们使用了一个5字节的初始化向量,因此有效载荷为5字节。
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│0x0│0x1│0x2│0x3│0x4│0x5│0x6│0x7│0x8│0x9│0xa│0xb│0xc│0xd│0xe│0xf│
├───┴───┴───┴───┴───┴───┴───┴───┴───┼───┴───┴───┴───┴───┼───┴───┤
│ SEED │ PAYLOAD │ CHECK │
│ │ │ SUM │
└───────────────────────────────────┴───────────────────┴───────┘
生成许可证密钥
use license_key::*;
// Define a hasher that will hash the seed and a initialization vector.
// DON'T USE THIS ONE. It's only for demonstrational purposes.
struct DummyHasher { }
impl KeyHasher for DummyHasher {
fn hash(&self, seed: u64, a: u64, b: u64, c: u64) -> u8 {
((seed ^ a ^ b ^ c) & 0xFF) as u8
}
}
// 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 = Generator::new(
DummyHasher { },
vec![
// DON'T USE THIS ONE.
// Generate your own.
(114, 83, 170),
(60, 208, 27),
(69, 14, 202),
(61, 232, 54)
],
);
// 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(1234567891011121314_u64);
// Write the key in hex format to the console.
// This will output something like: 112210F4B2D230A229552341B2E723
println!("{}", key.serialize::<HexFormat>());
验证许可证密钥
use license_key::*;
// Use the exact same hasher that we used when generating the key
struct DummyHasher { }
impl KeyHasher for DummyHasher {
fn hash(&self, seed: u64, a: u64, b: u64, c: u64) -> u8 {
((seed ^ a ^ b ^ c) & 0xFF) as u8
}
}
// Create the license key verifier
let mut verifier = Verifier::new(
DummyHasher { },
vec![
// Use the first byte (zero indexed) from the initialization vector.
// If a third-party key generator is created for the app, simply change this
// to another byte and any forged keys won't work anymore.
ByteCheck::new(0, (114, 83, 170)),
],
);
// 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(11111111_u64);
// Parse a key in hex format
let key = LicenseKey::parse::<HexFormat>("112210F4B2D230A229552341E723");
// Verify the license key
match verifier.verify(&key) {
Status::Valid => println!("Key is valid!"),
Status::Invalid => println!("Key is invalid!"),
Status::Blocked => println!("Key has been blocked!"),
Status::Forged => println!("Key has been forged!"),
}
依赖项
~22KB