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
string
with the standard Base 32 alphabet as defined in section 6 of RFC 4648.If the given
string
is empty, the encodedString
will also be empty. The result can contain padding (=
), if the givenstring
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 emptyString
if the givenstring
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 encodedString
will also be empty. The result can contain padding (=
), if the givenstring
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 emptyString
if the givenstring
is empty. -
Decodes the given
string
with the standard Base 32 alphabet.If the given
string
is empty, the decodedString
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:
Base32.DecodingError.invalidLength
if the encoded string has invalid length (is not a multiple of 8 or empty).Base32.DecodingError.illegalCharactersFound
if the encoded string contains one or more illegal characters.Base32.DecodingError.invalidPaddingCharacters
if the encoded string contains a padding character (=
) at an illegal position.Base32.DecodingError.missingCharacter
if no character can be read even though there a character is expected.
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 givenstring
is empty. -
Decodes the given
string
with the “Extended Hex” Base 32 alphabet.If the given
string
is empty, the decodedString
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:
Base32.DecodingError.invalidLength
if the encoded string has invalid length (is not a multiple of 8 or empty).Base32.DecodingError.illegalCharactersFound
if the encoded string contains one or more illegal characters.Base32.DecodingError.invalidPaddingCharacters
if the encoded string contains a padding character (=
) at an illegal position.Base32.DecodingError.missingCharacter
if no character can be read even though there a character is expected.
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 givenstring
is empty. -
Errors that can be thrown during decoding of a Base 32 encoded string.
See moreDeclaration
Swift
public enum DecodingError : Error, Equatable