CreateToken Method  
 

Generates a unique, cryptographically secure token.

Syntax

object.CreateToken( [BitLength], [EncodingType] )

Parameters

BitLength
An optional integer value which specifies the number of bits of entropy to use when generating the token. This value must be between 64 and 4096. If the value is not a power of two, it will be automatically rounded up to the nearest power of two. If the value is out of bounds, it will be clamped to the closest valid value. If this parameter is omitted or zero, a 128 bit random value will be used to generate the token.
EncodingType
An optional value which specifies the encoding algorithm used to convert the randomized data into a printable string. If this parameter is specified, it must be one of the following encoding constants:
Value Description
dataEncodeBase64 Use the Base64 algorithm, a widely supported encoding for binary data. Produces compact output, but includes characters like +, /, and = which may require escaping in URLs or filenames. Useful when interoperability is a priority.
dataEncodeBase58 Use the Base58 algorithm, designed to generate compact, human-friendly tokens. It excludes ambiguous characters (0, O, l, I), making it ideal for printed or typed identifiers and safe for use in URLs.
dataEncodeBase32 Use the Base32 algorithm, which encodes data using 32 unambiguous alphanumeric characters. A good choice for case-insensitive systems or manually entered codes, such as backup codes or license keys.
dataEncodeBase85 Use the Base85 algorithm for high-density output using the full printable ASCII range. Generates shorter tokens than Base64, but includes punctuation characters, which may not be suitable for all systems without additional escaping.

Return Value

If the method is successful it returns a unique string token based on the parameters specified. If the token cannot be created, an empty string will be returned.

Remarks

The CreateToken method generates a unique, cryptographically secure token string using the specified encoding format. The token is based on high-entropy random data generated using a secure operating system API and encoded into a compact, printable string. This method is suitable for generating session identifiers, authentication tokens, API keys, or any other unique identifier that must be unguessable and globally unique.

If the EncodingType parameter is omitted or has a value of zero, the method defaults to Base58 encoding, which provides a short, URL-safe, and visually unambiguous format. This is ideal for user-facing or embedded tokens. Each supported encoding format has distinct characteristics:

  • Base64 is compact and compatible with email and web services, but may require escaping for use in URLs or filenames. These tokens can use digits, upper and lower case letters, the + and / symbols.
  • Base58 avoids ambiguous characters and is well-suited for tokens shown to or entered by users. These tokens use digits and upper and lower case letters without any symbols.
  • Base32 is case-insensitive and easy to read, with predictable output length, making it ideal for printed codes. These tokens only use digits and lower case letters without any symbols.
  • Base85 minimizes token length while remaining printable, but the inclusion of symbols may limit usability in some contexts. These tokens use digits, upper and lower case letters and punctuation characters.

Trailing padding characters (=) are automatically removed from Base64 and Base32 encoding to create more compact tokens since decoding is not required.

The BitLength value determines the amount of entropy used to generate the token. Values are normalized to the nearest power of two and limited to a maximum of 4096 bits (512 bytes) to ensure performance and memory efficiency. In most cases, a token with 128 bits of entropy is more than sufficient. With 128 bits, over 5.1 quintillion tokens can be safely generated before the chance of a collision becomes statistically significant.

If you wish to create a token which embeds a specific custom value and uses 96 bits of entropy, you can use the CreateUuid8 method. This method generates a custom 128-bit UUID which embeds an application-defined value and returns a standard version 8 UUID in the standard dash notation.

Example

' Generate a unique 256-bit token encoded using Base32
Dim strToken As String
strToken = FileEncoder1.CreateToken(256, dataEncodeBase32)

If Len(strToken) = 0 Then
    MsgBox "Unable to generate a unique token", vbExclamation
    Exit Sub
End If

MsgBox strToken, vbInformation, "Token"

See Also

CreateUuid7 Method, CreateUuid8 Method