Base32

public struct Base32

The Base32 struct contains methods to encode and decode data using the Base 32 encoding as defined by RFC 4648.

The API supports both alphabets defined in the specification for encoding and decoding data:

  1. The Base 32 Alphabet (see RFC 4648 Section 6)
  2. The “Extended Hex” Base 32 Alphabet (see RFC 4648 Section 7)

The API only supports the encoding and decoding of strings and no other data types. All the inputs are validated and the API will throw exceptions instead of ignoring illegal input.

All methods are static so no instance of the Base32 struct needs to be created.

Examples:

let encoded = Base32.encode(string: "foobar")
print(encoded) // prints "MZXW6YTBOI======"

if let decoded = try? Base32.decode(string: encoded) {
    print(decoded) // prints "foobar"
}

Note

Encoding and decoding methods are not optimized and might perform badly. Use another Swift package if performance is a primary concern.

Warning

This API is still under development. APIs are subject to change and error. Do not use in production.
  • Encodes the given string with the standard Base 32 alphabet as defined in section 6 of RFC 4648.

    If the given string is empty, the encoded String will also be empty. The result can contain padding (=), if the given string does not contain a multiple of 5 input characters.

    Examples:

    let encoded = Base32.encode(string: "foobar")
    print(encoded) // prints "MZXW6YTBOI======"
    

    Important

    This method is case insensitive.

    Declaration

    Swift

    public static func encode(string: String) -> String

    Parameters

    string

    The string to encode.

    Return Value

    Base 32 encoded String or empty String if the given string is empty.

  • Encodes the given string with the “Extended Hex” Base 32 alphabet as defined in section 7 of RFC 4648.

    If the given string is empty, the encoded String will also be empty. The result can contain padding (=), if the given string does not contain a multiple of 5 input characters.

    Examples:

    let encoded = Base32.encodeHex(string: "foobar")
    print(encoded) // prints "CPNMUOJ1E8======"
    

    Important

    This method is case insensitive.

    Declaration

    Swift

    public static func encodeHex(string: String) -> String

    Parameters

    string

    The string to encode.

    Return Value

    Base 32 Hex encoded String or empty String if the given string is empty.

  • Decodes the given string with the standard Base 32 alphabet.

    If the given string is empty, the decoded String will also be empty. This method is case in-senstive.

    Examples:

    if let decoded = try? Base32.decode(string: "MZXW6YTBOI======") {
        print(decoded) // prints "foobar"
    }
    

    Important

    This method is case insensitive.

    Throws

    Throws:

    Declaration

    Swift

    public static func decode(string: String) throws -> String

    Parameters

    string

    The string to decode.

    Return Value

    Decoded string or empty String if the given string is empty.

  • Decodes the given string with the “Extended Hex” Base 32 alphabet.

    If the given string is empty, the decoded String will also be empty.

    Examples:

    if let decoded = try? Base32.decodeHex(string: "CPNMUOJ1E8======") {
        print(decoded) // prints "foobar"
    }
    

    Important

    This method is case insensitive.

    Throws

    Throws:

    Declaration

    Swift

    public static func decodeHex(string: String) throws -> String

    Parameters

    string

    The string to decode.

    Return Value

    Decoded string or empty String if the given string is empty.

  • Errors that can be thrown during decoding of a Base 32 encoded string.

    See more

    Declaration

    Swift

    public enum DecodingError : Error, Equatable