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/
// swift-tools-version:5.2
import PackageDescription
let package = Package(
dependencies: [
.package(url: "https://github.com/jenslauterbach/Base32Kit.git", from: "0.2.0"),
]
)
import Base32Kit
let encoded = Base32.encode(string: "foobar")
print(encoded) // prints MZXW6YTBOI======
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!")
}
You can start playing around with Base32Kit
by using the Swift REPL on your local machine:
$ git clone https://github.com/jenslauterbach/Base32Kit.git
$ cd Base32Kit
$ swift run --repl
1> import Base32Kit
2> Base32.encode(string: "foobar")
$R0: String = "MZXW6YTBOI======"
3> try? Base32.decode(string: $R0)
$R1: String? = "foobar"
4> :quit
The primary design goals of this Swift package are:
Furthermore, we try to create a comprehensive test suite to verify the package with a big variety of test data on all supported platforms.
We use SemVer for versioning. For the versions available, see the releases page.
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
link |
Stars: 2 |
Last commit: 3 years ago |
Changes:
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics