Swiftpack.co - jenslauterbach/Base32Kit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by jenslauterbach.
jenslauterbach/Base32Kit 0.2.1
RFC4648 compliant and cross-platform Base 32 Swift package implemented without Foundation.
⭐️ 2
🕓 3 years ago
.package(url: "https://github.com/jenslauterbach/Base32Kit.git", from: "0.2.1")

Base32Kit

Swift 5.0 | 5.1 | 5.2 CI Codacy Badge codecov

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

(Back to top)

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!")
}

Quick Start

(Back to top)

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

Design Goals

(Back to top)

The primary design goals of this Swift package are:

  1. 100% RFC 4648 compliance.
  2. Cross-platform (run everywhere where Swift runs).
  3. 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

(Back to top)

Versioning

(Back to top)

We use SemVer for versioning. For the versions available, see the releases page.

Authors

(Back to top)

  • Jens Lauterbach - Main author - (@jenslauterbach)

License

(Back to top)

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

Acknowledgments

(Back to top)

GitHub

link
Stars: 2
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

v.0.2.1
3 years ago

Changes:

  • [Improved] Validation of encoded data during decoding

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics