How Random Letter Generation Works in SunLicense: ASCII Code Implementation

SunLicense generates random letters by selecting ASCII values between 65-90 for uppercase (A-Z) or 97-122 for lowercase (a-z) using PHP's rand() and chr() functions based on template placeholders.

The msbatal/php-license-key-generator repository provides a flexible PHP library for creating license keys using customizable templates. Understanding how random letter generation works in SunLicense is essential for developers implementing secure key generation systems, as the class uses specific ASCII code ranges to produce alphabetic characters deterministically based on the $case property.

Understanding the SunLicense Template System

SunLicense creates license keys based on a template string (e.g., X9XX99-XX99-9X9X-99XX9X). When the generator encounters a placeholder that matches a letter ([a‑zA‑Z]), it triggers the random letter generation routine. The actual logic lives in the license() method within SunLicense.php.

ASCII-Based Random Letter Generation Logic

The random letter generation in SunLicense relies on PHP's built-in rand() and chr() functions to convert numeric ASCII values into alphabetic characters.

Identifying Letter Placeholders

In SunLicense.php, the license() method first checks if the current template character is a letter placeholder using preg_match():

if (preg_match('/[a-zA-Z]/', $this->template[$i])) {
    // Letter generation logic follows
}

This regular expression identifies any alphabetic character in the template string, triggering the random letter generation routine.

Uppercase Letter Generation (ASCII 65-90)

When the $case property is set to 'upper' (the default), SunLicense generates uppercase letters by selecting random integers between 65 and 90, which correspond to ASCII codes for 'A' through 'Z':

$key .= chr(rand(65, 90));  // generate uppercase letters

The rand(65, 90) function returns a pseudo-random integer in the specified range, and chr() converts that integer to its corresponding ASCII character.

Lowercase Letter Generation (ASCII 97-122)

For lowercase output, the system uses ASCII range 97-122, representing 'a' through 'z':

$key .= chr(rand(97, 122)); // generate lowercase letters

This logic executes when the $case property is explicitly set to 'lower' during class instantiation.

Implementation in SunLicense.php

The complete random letter generation logic is implemented in SunLicense.php at lines 85-90 within the license() method:

if (preg_match('/[a-zA-Z]/', $this->template[$i])) {
    if ($this->case == 'lower') {
        $key .= chr(rand(97, 122)); // generate lowercase letters
    } else {
        $key .= chr(rand(65, 90));  // generate uppercase letters
    }
}

This code block demonstrates how the class combines regular expression matching, conditional case checking, and ASCII-based random generation to produce license key segments.

Practical Code Examples

Generate a Single Key with Default Uppercase Letters

require 'SunLicense.php';

$lic = new SunLicense();          // defaults: no prefix, default template, uppercase
echo $lic->generate();            // e.g. "M5JH84-QP84-5J9J-84QJ9J"

Generate a Key with Lowercase Letters

require 'SunLicense.php';

$lic = new SunLicense(null, null, 'lower');
echo $lic->generate();            // e.g. "m5jh84-qp84-5j9j-84qj9j"

Custom Template with Mixed Placeholders

require 'SunLicense.php';

$template = 'AA-99-zz';           // 'A' and 'z' are placeholders for letters
$lic = new SunLicense(null, $template, 'lower');
echo $lic->generate();            // e.g. "ab-42-xy"

Generate Multiple Unique Keys

require 'SunLicense.php';

$lic = new SunLicense(null, null, 'upper', 5); // request 5 keys
$keys = $lic->generate();        // returns an array of 5 distinct keys
print_r($keys);

Summary

  • SunLicense generates random letters using ASCII code ranges: 65-90 for uppercase (A-Z) and 97-122 for lowercase (a-z).
  • The license() method in SunLicense.php (lines 85-90) uses preg_match('/[a-zA-Z]/', ...) to identify letter placeholders in the template.
  • PHP's rand() function generates the random integer within the specified ASCII range, and chr() converts it to the corresponding character.
  • The $case property controls whether the output uses uppercase or lowercase letters, defaulting to uppercase when not specified.

Frequently Asked Questions

What ASCII ranges does SunLicense use for random letter generation?

SunLicense uses ASCII 65-90 for uppercase letters (A-Z) and ASCII 97-122 for lowercase letters (a-z). These ranges correspond to standard ASCII character codes that PHP's chr() function converts into alphabetic characters.

How does SunLicense decide between uppercase and lowercase output?

The decision is controlled by the $case property passed to the constructor. When $case is set to 'lower', the generator uses rand(97, 122) to produce lowercase letters. For any other value (including the default), it uses rand(65, 90) to generate uppercase letters.

Is the random letter generation cryptographically secure?

No, SunLicense uses PHP's rand() function, which is not cryptographically secure. The rand() function generates pseudo-random numbers using a linear congruential generator algorithm. For cryptographic purposes requiring secure random generation, PHP's random_int() or random_bytes() functions would be more appropriate.

Where is the letter generation logic located in the source code?

The core logic resides in SunLicense.php at lines 85-90 within the license() method. This section contains the preg_match() check for letter placeholders and the conditional rand()/chr() calls that generate the actual random characters based on the selected case setting.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →