Crate rust_license_key

Crate rust_license_key 

Source
Expand description

§rust-license-key

A production-grade Rust library for creating and validating offline software licenses using Ed25519 cryptography.

§Overview

rust-license-key provides a secure, offline licensing system for software applications. It uses Ed25519 digital signatures to create tamper-proof licenses that can be verified without any network access.

§Key Features

  • Asymmetric Cryptography: Licenses are signed with a private key and verified with a public key. The client never has access to the signing key.
  • Offline Verification: No network calls required for license validation.
  • Rich Constraints: Support for expiration dates, feature flags, hostname restrictions, version limits, and custom constraints.
  • Tamper-Proof: Any modification to the license invalidates the signature.
  • Human-Readable: License payloads are JSON, making debugging easy.
  • Versioned Format: Built-in version checking for forward compatibility.

§Quick Start

§Publisher Side: Creating Licenses

use rust_license_key::prelude::*;
use chrono::Duration;

// Generate a key pair (do this once and store securely)
let key_pair = KeyPair::generate().expect("Key generation failed");

// Save these keys:
// - Private key (keep secret!): key_pair.private_key_base64()
// - Public key (embed in app): key_pair.public_key_base64()

// Create a license
let license_json = LicenseBuilder::new()
    .license_id("LIC-2024-001")
    .customer_id("ACME-CORP")
    .customer_name("Acme Corporation")
    .expires_in(Duration::days(365))
    .allowed_features(vec!["basic", "premium", "analytics"])
    .max_connections(100)
    .build_and_sign_to_json(&key_pair)
    .expect("License creation failed");

// Send license_json to the customer
println!("{}", license_json);

§Client Side: Validating Licenses

use rust_license_key::prelude::*;
use semver::Version;

// The public key embedded in your application
let public_key_base64 = "..."; // Your public key here

// The license file content
let license_json = "..."; // Customer's license file

// Create a validator
// let validator = LicenseValidator::from_public_key_base64(public_key_base64)
//     .expect("Invalid public key");

// Set up validation context
// let context = ValidationContext::new()
//     .with_hostname("myserver.example.com")
//     .with_software_version(Version::new(1, 2, 3))
//     .with_feature("premium");

// Validate the license
// let result = validator.validate_json(&license_json, &context)
//     .expect("Validation error");

// if result.is_valid {
//     println!("License valid! Days remaining: {:?}", result.days_remaining());
//     if result.is_feature_allowed("premium") {
//         println!("Premium features enabled!");
//     }
// } else {
//     for failure in &result.failures {
//         println!("Validation failed: {}", failure.message);
//     }
// }

§Module Organization

  • crypto - Ed25519 key generation, signing, and verification.
  • builder - Fluent API for creating and signing licenses.
  • parser - Loading and decoding signed licenses.
  • validator - Comprehensive license validation.
  • models - Data structures for licenses, constraints, and results.
  • error - Error types and validation failure information.

§Security Considerations

  • Private Key Security: The private key must be kept secret and should only exist on the license generation server. Never include it in client applications.
  • Public Key Distribution: The public key can be safely embedded in client applications. It can only verify signatures, not create them.
  • No Encryption: License payloads are signed but not encrypted. Do not store sensitive information in license metadata.
  • Offline Only: This library does not provide license revocation or online validation. For these features, implement a separate online check.

Re-exports§

pub use builder::LicenseBuilder;
pub use crypto::generate_key_pair_base64;
pub use crypto::KeyPair;
pub use crypto::PublicKey;
pub use error::LicenseError;
pub use error::Result;
pub use models::LicenseConstraints;
pub use models::LicensePayload;
pub use models::SignedLicense;
pub use models::ValidationContext;
pub use models::ValidationResult;
pub use parser::parse_license;
pub use parser::LicenseParser;
pub use validator::is_feature_allowed;
pub use validator::is_license_valid;
pub use validator::validate_license;
pub use validator::LicenseValidator;

Modules§

builder
License builder for creating and signing licenses.
crypto
Cryptographic operations for license signing and verification.
error
Error types for the rust-license-key library.
models
Data models for license representation and constraints.
parser
License parsing and decoding functionality.
prelude
Convenient re-exports of the most commonly used types.
validator
License validation logic.