Documentation
¶
Overview ¶
Package base32 provides Crockford Base32 encoding and CRC-10 checksums for human-readable, error-correcting identifiers.
The Base32 implementation uses Crockford's alphabet (excludes I, L, O, U) and provides case-insensitive decoding with automatic error correction.
The CRC-10 checksum provides 99.9%+ error detection for single character errors, transpositions, and double errors.
Example:
// Encode a value
id := base32.EncodeBase32(12345, 8) // "0000C1P9"
// Add checksum
idWithChecksum := base32.AppendChecksum(id)
// Validate
if base32.ValidateChecksum(idWithChecksum) {
// Valid ID
}
Index ¶
- func AppendChecksum(data string) string
- func CalculateChecksum(data string) string
- func DecodeBase32(encoded string) (uint64, error)
- func EncodeBase32(value uint64, length int) string
- func EncodeBase32Compact(value uint64) string
- func ExtractChecksum(input string) string
- func IsValidBase32Char(c rune) bool
- func NormalizeBase32(input string) string
- func StripChecksum(input string) string
- func ValidateChecksum(input string) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendChecksum ¶
AppendChecksum adds a 2-character checksum to the end of the data.
This is the recommended way to create checksummed strings.
Example:
id := base32.EncodeBase32(12345, 6) // "00C1P9" idWithChecksum := base32.AppendChecksum(id) // "00C1P9XY"
Parameters:
- data: The Base32 string to checksum
Returns:
- The input string with a 2-character checksum appended
func CalculateChecksum ¶
CalculateChecksum computes a 2-character Base32 checksum using CRC-10.
The checksum provides 99.9%+ error detection for:
- Single character errors
- Character transpositions
- Double errors
- Most insertion/deletion errors
The CRC-10 algorithm processes each Base32 character (5 bits) and produces a 10-bit checksum, which is then encoded as 2 Base32 characters.
Example:
checksum := base32.CalculateChecksum("ABC123") // "XY"
withChecksum := base32.AppendChecksum("ABC123") // "ABC123XY"
Parameters:
- data: The Base32 string to checksum
Returns:
- A 2-character Base32 checksum
func DecodeBase32 ¶
DecodeBase32 decodes a Base32 string to an unsigned integer.
Returns an error if the string contains invalid characters. Supports case-insensitive input and common error corrections (I→1, L→1, O→0).
Example:
val, err := base32.DecodeBase32("C1P9") // 12345, nil
val, err := base32.DecodeBase32("c1p9") // 12345, nil (case-insensitive)
val, err := base32.DecodeBase32("C1PO") // 12345, nil (O→0 correction)
Parameters:
- encoded: The Base32-encoded string to decode
Returns:
- The decoded unsigned integer value
- An error if the input contains invalid characters
func EncodeBase32 ¶
EncodeBase32 encodes an unsigned integer to a Base32 string of specified length.
The encoded value is left-padded with '0's to reach the specified length. Uses Crockford's Base32 alphabet (0-9, A-Z excluding I, L, O, U).
Example:
base32.EncodeBase32(42, 4) // "0016" base32.EncodeBase32(999, 3) // "0ZZ" base32.EncodeBase32(32, 2) // "10"
Parameters:
- value: The unsigned integer to encode
- length: The minimum length of the output string
Returns:
- A Base32-encoded string of at least 'length' characters
func EncodeBase32Compact ¶
EncodeBase32Compact encodes a value to the minimum number of Base32 characters needed.
Unlike EncodeBase32, this function does not pad the output to a fixed length.
Example:
base32.EncodeBase32Compact(0) // "0" base32.EncodeBase32Compact(31) // "Z" base32.EncodeBase32Compact(32) // "10" base32.EncodeBase32Compact(12345) // "C1P9"
func ExtractChecksum ¶
ExtractChecksum extracts the last 2 characters (checksum) from a string.
Returns an empty string if the input has fewer than 2 characters.
Example:
checksum := base32.ExtractChecksum("ABC123XY") // "XY"
checksum := base32.ExtractChecksum("A") // ""
Parameters:
- input: The string with checksum appended
Returns:
- The last 2 characters of the input
func IsValidBase32Char ¶
IsValidBase32Char returns true if the character is valid in Base32 encoding.
Valid characters include: 0-9, A-Z (excluding I, L, O, U), and their lowercase equivalents. Also accepts I, L, O as they are auto-corrected to 1, 1, 0.
Example:
base32.IsValidBase32Char('A') // true
base32.IsValidBase32Char('a') // true
base32.IsValidBase32Char('O') // true (auto-corrected to 0)
base32.IsValidBase32Char('U') // false
func NormalizeBase32 ¶
NormalizeBase32 normalizes a Base32 string by:
- Converting to uppercase
- Removing dashes and spaces
- Correcting common mistakes (I→1, L→1, O→0)
This function is useful for processing user input to ensure consistency.
Example:
base32.NormalizeBase32("abc-def") // "ABCDEF"
base32.NormalizeBase32("1O 2I") // "1021"
base32.NormalizeBase32("hell0") // "HELL0"
func StripChecksum ¶
StripChecksum removes the last 2 characters (checksum) from a string.
Returns an empty string if the input has 2 or fewer characters.
Example:
data := base32.StripChecksum("ABC123XY") // "ABC123"
data := base32.StripChecksum("AB") // ""
Parameters:
- input: The string with checksum appended
Returns:
- The input string without the last 2 characters
func ValidateChecksum ¶
ValidateChecksum verifies that the checksum in a string is correct.
Expected format: [data][2 chars checksum]
This function is useful for validating user input or detecting data corruption.
Example:
valid := base32.ValidateChecksum("ABC123XY") // true if XY is correct checksum
valid := base32.ValidateChecksum("ABC123ZZ") // false if ZZ is wrong
Parameters:
- input: The string with checksum appended (minimum 3 characters)
Returns:
- true if the checksum is valid, false otherwise
Types ¶
This section is empty.