Go to file
Tobias Locker 387b32f337
Merge pull request #7 from tvercaut/modpb64xover
Switch algorithm to match modp_b64 for performance purposes
2024-02-26 09:34:47 +01:00
.github/workflows attempting to fix big endian CI 2024-02-20 08:52:11 +00:00
include minor cleanup 2024-02-22 18:35:44 +00:00
scripts trying to fix big endian case 2024-02-20 10:39:19 +00:00
test trying to fix big endian case 2024-02-20 10:39:19 +00:00
.gitignore Initial commit 2019-05-27 23:47:26 +01:00
CMakeLists.txt reverting to verbose fetch output 2024-02-20 10:55:19 +00:00
CPPLINT.cfg relax cpplint 2024-02-20 10:44:25 +00:00
LICENSE Create LICENSE 2019-05-28 19:00:12 +01:00
README.md minor cleanup 2024-02-22 18:35:44 +00:00

base64

A simple approach to convert strings from and to base64. Header only c++ library (single header).

Usage

#include <iostream>

#include "base64.hpp"

int main() {
  auto encoded_str =  base64::to_base64("Hello, World!");
  std::cout << encoded_str << std::endl; // SGVsbG8sIFdvcmxkIQ==
  auto decoded_str = base64::from_base64("SGVsbG8sIFdvcmxkIQ==");
  std::cout << decoded_str << std::endl; // Hello, World!
}

Notes

This library relies on C++17 but will exploit some C++20 features if available (e.g. bit_cast).

There are many implementations available and it may be worth looking at those. A benchmark of various c/c++ base64 implementations can be found at https://github.com/gaspardpetit/base64/

This implementation here adopts the approach of Nick Galbreath's modp_b64 library also used by chromium (e.g. https://github.com/chromium/chromium/tree/main/third_party/modp_b64 ) but offers it as a c++ single header file. This choice was based on the good computational performance of the underpinning algorithm. We also decided to avoid relying on a c++ union to perform type punning as this, while working in practice, is strictly speaking undefined behaviour in c++: https://en.wikipedia.org/wiki/Type_punning#Use_of_union

Faster c/c++ implementations exist althrough these likely exploit simd / openmp or similar acceleration techniques:

Many other C++ centric appraches exists although they seem to focus on readibility or genericity at the cost of performance, e.g.: