Discussion: Base64 encode and decode in the Standard Library


I would like to start the discussion around implementing Base64 in pure Swift and making the implementation part of the Standard Library.

Currently the only "official" way is using Foundation and Foundation's Data type. To reduce the number of conversions between data types the swift on the server community includes an own implementation of Base64 in performance critical libraries. For example:

Since no-one should import SwiftNIO, Vapor or Noze just to get some Base64 functionality I just implemented another Base64 encoder and decoder for use in AWS client libraries, that I'm a part of. The code is public on GitHub and has been peer reviewed by @johannesweiss from the SwiftNIO team. The encoder is also now vendored in SwiftNIO. It is pure swift and uses no unsafe apis. Further it is generic, which makes the API more swifty.

  public static func encode<Buffer: Collection>(bytes: Buffer, options: EncodingOptions = [])
    -> String where Buffer.Element == UInt8

Especially considering the performance benefit on Linux and the generic approach I wonder if the community is interested in getting this functionality into the Standard Library as suggested once by @lukasa.

Update: Maybe this could be a candidate for the Standard Library Preview Package


It's definitely important, and Foundation's implementation is far from optimal, but I'm not sure this belongs in the standard library.

If we accepted this, we'd have 2 ways to do Base64 conversion - so either the Foundation one gets deprecated, or their implementation should be replaced to forward to this one. Either way, acceptance means this ends up in Foundation. Given that, I think it should become a part of Foundation directly.

I had a similar idea a while ago (to make base64 encoding generic). I never got any response from the Foundation team. Best of luck.

Thank you @fabianfett for bringing this up. Personally, I was wondering why Base32 and Base64 encoding/decoding is not part of the standard or core libraries.

For Base64 you at least have the option to use Data, but as far as I can tell there is no out-of-the-box way to work with Base32. I would appreciate if both could become part of the standard or core library in the future.