License3j Documentation

repository·master·Indexed 20 days ago

https://github.com/verhas/license3j

A free, open-source Java library for managing electronically signed license files. License3j allows developers to create, read, and verify licenses containing custom features with support for BINARY, BASE64, and STRING storage formats. It includes the license3jrepl tool for key pair generation and license management, as well as SimpleLicense for generating and validating short, fixed-length license keys.

Tokens
2.3K
Snippets
8
Records
12
Agent score
69%

What's inside License3j

  1. What is a License3j license and its features

    master

    A license is a collection of features. Each feature consists of a name, a type, and a value.

    Predefined Feature Names

    • licenseId: Unique ID (UUID)
    • licenseSignature: The signature (BINARY)
    • signatureDigest: The digest of the license (STRING)
    • expiryDate: The expiry date (DATE)

    Supported Feature Types

    • BINARY: byte[] array
    • STRING: java.lang.String
    • BYTE: single byte
    • SHORT: single short
    • INT: integer
    • LONG: long
    • FLOAT: float
    • DOUBLE: double
    • BIGINTEGER: java.math.BigInteger
    • BIGDECIMAL: java.math.BigDecimal
    • DATE: java.util.Date
    • UUID: java.util.UUID

    License Storage Formats

    • BINARY: Most compact, suitable for files, not human-readable.
    • BASE64: Binary format encoded in Base64 for printable characters.
    • STRING: Human-readable UTF-8 text, suitable for manual editing.
  2. Install License3j via Maven

    master

    To use License3j in your Java project, add the following dependency to your pom.xml. Check the Sonatype central repository for the latest version.

    <dependency>
        <groupId>com.javax0.license3j</groupId>
        <artifactId>license3j</artifactId>
        <version>3.3.0</version>
    </dependency>
  3. Generate key pairs using License3j REPL

    master

    To manage licenses (create, sign, etc.), use the license3jrepl application. To generate a new RSA key pair, run the following command within the REPL prompt (L3j> $):

    generateKeys algorithm=RSA size=1024 format=BINARY public=public.key private=private.key

    After generation, you can use the dumpPublicKey command in the REPL to get the Java code required to embed the public key directly into your application.

    $ java -jar license3jrepl.jar
    L3j> $ generateKeys algorithm=RSA size=1024 format=BINARY public=public.key private=private.key
  4. How License3j license features work

    master

    A license in License3j is a collection of features. Each feature consists of a name, a type, and a value.

    Supported feature types include:

    • BINARY: Arbitrary byte[] array.
    • STRING: java.lang.String.
    • BYTE, SHORT, INT, LONG, FLOAT, DOUBLE, BIGINTEGER, BIGDECIMAL, DATE, UUID: Corresponding Java objects or primitives.

    Note that there is no automatic conversion between different feature types; you must retrieve the value using the method corresponding to its type.

  5. License file formats: Binary, Base64, and Text

    master

    License3j supports three storage formats:

    1. BINARY: The most compact and shortest format. It starts with magic bytes 0xCE, 0x21, 0x5E, 0x4E (LICENSE). It is not human-readable and is best for file storage.
    2. BASE64: The binary format encoded in Base64. This ensures the license contains only printable characters, making it suitable for email or web transmission.
    3. STRING (Text): A human-readable, UTF-8 encoded format suitable for manual editing.

    Text Format Syntax: Each line represents a feature: name:TYPE=value.

    • The type (e.g., STRING, INT) is optional for STRING types.
    • For multi-line values (like long strings), use the << syntax:
      description:STRING=<<
      This is a multi-line
      description.
      >>
  6. Verify a license signature

    master

    Reading a license file does not automatically verify its authenticity. To ensure the license has not been tampered with, you must verify its digital signature using a public key.

    It is highly recommended to hard-code the public key into your application as a byte[] array rather than loading it from an external file. To verify, use the isSigned(byte[] key) method (or similar verification logic provided by the library) which returns true if the signature is valid for the provided key.

    // Returns true if the license is signed and the signature is valid for the provided key
    if (license.isSigned(publicKeyBytes)) {
        // Proceed with using license features
    } else {
        // Handle invalid/tampered license
    }
  7. Use the License3j REPL for key and license management

    master

    The License3j REPL is a separate interactive application used to generate key pairs, create/sign licenses, and manage them via command line. It is not included in the main license3j.jar.

    Installation: Download the REPL JAR from https://github.com/verhas/license3jrepl.

    Common Commands:

    • generateKeys algorithm=RSA size=1024 format=BINARY public=public.key private=private.key: Creates a new RSA key pair.
    • dumpPublicKey: Dumps the Java code required to embed the public key into your application.
    • newLicense: Creates a new license in memory.
    • feature name:TYPE=value: Adds a feature to the current license.
    • sign: Signs the current license using the loaded private key.
    • saveLicense [format] fileName: Saves the current license to a file.
    • help: Displays available commands.
    $ java -jar license3jrepl.jar
    L3j> $
  8. Create a simple license with SimpleLicense

    master

    Use SimpleLicense to generate short, fixed-length license keys (four blocks of six characters separated by hyphens, e.g., DCWI3U-6RDTB8-EBMPTJ-TVURQ7). This is ideal for environments where you do not need to store specific features or electronic signatures.

    To create a license, use .withSecret(String) to provide a private key that should be hidden in your code, and .forValue(String) to provide a user-specific identifier (like a user ID or license ID).

    Note: The license key does not store the value itself; it is a shortened hash of the secret and the value.

    final var lic = SimpleLicense
                      .withSecret("abraka dabra")
                      .forValue("my special user");
    final var code = lic.toString();
  9. Validate a simple license code

    master

    To verify if a license code provided by a user is valid, use the isOK(String) method on a SimpleLicense instance configured with the same secret and value used during generation.

    Example using Assertions.assertTrue for validation:

    Assertions.assertTrue(lic.isOK(code));
  10. Retrieve feature values from a license

    master

    Once a license is verified, you can retrieve specific features by their name using license.get(name). To get the actual value, call the type-specific method (e.g., .getDate(), .getString(), .getInt()) on the returned feature object.

    Note that there is no automatic conversion between feature types; you must call the method corresponding to the feature's defined type.

    // get a feature and from the feature type-specific data, like date, int, long, String...
    Date birthday = license.get("bd").getDate();
  11. License3j REPL command reference

    master

    The license3jrepl is an interactive tool for managing keys and licenses. Commands can be abbreviated if the abbreviation is unique.

    CommandDescription
    helpShow available commands
    feature name:TYPE=valueDefine a new feature
    licenseLoad [format=TEXT*|BINARY|BASE64] fileNameLoad a license from a file
    saveLicense [format=TEXT*|BINARY|BASE64] fileNameSave the current license to a file
    loadPrivateKey [format=BINARY|BASE64] keyFile=xxxLoad a private key
    loadPublicKey [format=BINARY|BASE64] keyFile=xxxLoad a public key
    sign [digest=SHA-512]Sign the current license
    verifyVerify the current license signature
    generateKeys [algorithm=RSA] [size=2048] [format=BINARY|BASE64] public=xxx private=xxxGenerate a new key pair
    newLicenseCreate a new empty license
    dumpDump current license/key info
    digestPublicKeyShow the SHA-512 digest of the public key

    Note: Commands starting with ! are passed to the underlying operating system (e.g., !ls or !dir).

  12. Load a license from a file

    master

    Use the javax0.license3j.io.LicenseReader class to read a license file.

    By default, the read() method assumes the file is in BINARY format. If the file is in BASE64 or STRING (text) format, you must specify the format using the IOFormat parameter.

    If the file is unreadable or the format is incorrect, an IOException or IllegalArgumentException will be thrown.

    // Example of reading a binary license
    LicenseReader reader = new LicenseReader(new File("license.bin"));
    License license = reader.read();
    
    // Example of reading a text license
    License license = reader.read(IOFormat.STRING);