Base32Kit
Disclaimer: This library is in its early development phase and should not yet be used in production. All the code in this repository might change, including its APIs. This library is not very well optimised. If you are looking for the absolute best performance this library is probably not for you.
Base32Kit is a simple pure Swift Library for the Base32 encoding as defined by RFC 4648 that is implemented without Apples Foundation framework.
API reference documentation is available at https://jenslauterbach.github.io/Base32Kit/
Table of Contents
Installation
Swift Package Manager
// swift-tools-version:5.2
import PackageDescription
let package = Package(
dependencies: [
.package(url: "https://github.com/jenslauterbach/Base32Kit.git", from: "0.2.0"),
]
)
Usage
Encoding
import Base32Kit
let encoded = Base32.encode(string: "foobar")
print(encoded) // prints MZXW6YTBOI======
Decoding
All decode*
methods can throw an error. In general you have two options of dealing with this. If you are not interested in the error and just need to decode the value or do “nothing”, you can use a simple try?
statement:
import Base32Kit
if let decoded = try? Base32.decode(string: "MZXW6YTBOI======") {
print(decoded) // prints "foobar"
}
If you want to handle any possible error in a more generlised way, you can use the following simple do-catch
statement:
import Base32Kit
do {
let decoded = try Base32.decode(string: "MZXW6YTBOI======")
print(decoded) // prints "foobar"
} catch {
print("Error!")
}
The above code does not allow to handle all the different errors that may be thrown separately. To handle errors separately, you can use an extended version of the above do-catch
statement:
import Base32Kit
do {
let decoded = try Base32.decode(string: "MZXW6YTBOI======")
print(decoded) // prints "foobar"
} catch Base32.DecodingError.invalidLength {
print("Encoded string has invalid length!")
} catch Base32.DecodingError.invalidPaddingCharacters {
print("Encoded string uses illegal padding characters!")
} catch Base32.DecodingError.invalidCharacter(let illegalCharacters) {
print("Encoded string contained the following illegal characters: \(illegalCharacters)")
} catch Base32.DecodingError.missingCharacter {
print("During decoding there was an unexpected missing character!")
} catch {
print("Error!")
}
Design Goals
The primary design goals of this Swift package are:
- 100% RFC 4648 compliance.
- Cross-platform (run everywhere where Swift runs).
- The code is easy to read and understand.
Furthermore, we try to create a comprehensive test suite to verify the package with a big variety of test data on all supported platforms.
Alternatives
- Base32 by Norio Nomura
- Base32 by std-swift
Versioning
We use SemVer for versioning. For the versions available, see the releases page.
Authors
- Jens Lauterbach - Main author - (@jenslauterbach)
License
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
Acknowledgments
- Fabian Fett - For his inspiring Base64Kit implementation