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:
- The Base 32 Alphabet (see RFC 4648 Section 6)
- 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
stringwith the standard Base 32 alphabet as defined in section 6 of RFC 4648.If the given
stringis empty, the encodedStringwill also be empty. The result can contain padding (=), if the givenstringdoes 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) -> StringParameters
stringThe string to encode.
Return Value
Base 32 encoded
Stringor emptyStringif the givenstringis empty. -
Encodes the given
stringwith the “Extended Hex” Base 32 alphabet as defined in section 7 of RFC 4648.If the given
stringis empty, the encodedStringwill also be empty. The result can contain padding (=), if the givenstringdoes 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) -> StringParameters
stringThe string to encode.
Return Value
Base 32 Hex encoded
Stringor emptyStringif the givenstringis empty. -
Decodes the given
stringwith the standard Base 32 alphabet.If the given
stringis empty, the decodedStringwill 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:
Base32.DecodingError.invalidLengthif the encoded string has invalid length (is not a multiple of 8 or empty).Base32.DecodingError.illegalCharactersFoundif the encoded string contains one or more illegal characters.Base32.DecodingError.invalidPaddingCharactersif the encoded string contains a padding character (=) at an illegal position.Base32.DecodingError.missingCharacterif no character can be read even though there a character is expected.
Declaration
Swift
public static func decode(string: String) throws -> StringParameters
stringThe string to decode.
Return Value
Decoded string or empty
Stringif the givenstringis empty. -
Decodes the given
stringwith the “Extended Hex” Base 32 alphabet.If the given
stringis empty, the decodedStringwill also be empty.Examples:
if let decoded = try? Base32.decodeHex(string: "CPNMUOJ1E8======") { print(decoded) // prints "foobar" }Important
This method is case insensitive.
Throws
Throws:
Base32.DecodingError.invalidLengthif the encoded string has invalid length (is not a multiple of 8 or empty).Base32.DecodingError.illegalCharactersFoundif the encoded string contains one or more illegal characters.Base32.DecodingError.invalidPaddingCharactersif the encoded string contains a padding character (=) at an illegal position.Base32.DecodingError.missingCharacterif no character can be read even though there a character is expected.
Declaration
Swift
public static func decodeHex(string: String) throws -> StringParameters
stringThe string to decode.
Return Value
Decoded string or empty
Stringif the givenstringis empty. -
Errors that can be thrown during decoding of a Base 32 encoded string.
See moreDeclaration
Swift
public enum DecodingError : Error, Equatable
View on GitHub
Base32 Structure Reference