Base32Encoder is a pure Swift library that encodes Data to a Base32 String, and decodes a Base32String to Data.
Encodes data as per the [RFC 3548 / RFC 4648] specification for Base32 data encodings.
This library was written for a project that required encoding of small data packets and to learn bit bashing. This means the code is written for learning and has not been optimized for elegance or performance.
Simply add the requirement to your Cartfile:
github "markrenaud/Base32Encoder"
Copy Base32.swift
to your project
If the Base32Encoder was installed as a library, you must import it (this can be skipped if it was manually added to your project).
import Base32Encoder
You can then encode data (and optionally pad as per RFC 3548 2.2 - Padding of encoded data)
let data = "hi".data(using: .ascii)!
let base32 = Base32.encode(data: data) // = "NBUQ"
let base32padded = Base32.encode(data: data, padding: true) // = "NBUQ===="
And decode Base32 strings back to dataf
let decodeBase32Data = Base32.decode(string: "NBUQ")
let decodedString = String(data: decodeBase32DataPadded!, encoding: .ascii) // = "hi"
Or you can use the Data and String extensions
let base32 = data.base32String(padded: true) // = "NBUQ===="
if let decoded = try? "NBUQ====".decodeBase32(padded: true) {
let string = String(data: decoded, encoding: .ascii) // = "hi"
}
This project is licensed under the MIT license. See the LICENCE file for more info.