Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
Base64.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Cryptography_Base64_h_
5#define _Stroika_Foundation_Cryptography_Base64_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <string>
10#include <vector>
11
13#include "Stroika/Foundation/Common/Common.h"
17
18/**
19 * \file
20 *
21 * TODO:
22 *
23 * @todo EncodeBase64 should return STROIKA string, or BLOB - not std::string? Or maybe std::string
24 * best - but document why - cuz always ascii data so more compact?? No - not good reason cuz
25 * we can construct a SUBTYPE of String that takes advantage of it.
26 *
27 * @todo DecodeBase64() should have an overload taking BinaryInputStream, and EncodeBase64()
28 * should have an overload with BinaryOutputStream (keeping existing overloads).
29 *
30 * @todo Tons todo optimizing this implementation (to not use temporary objects and
31 * avoiding copying).
32 *
33 */
34
35/**
36 * Base64 (RFC 4648, section 4): bytes as text, each character standing for 6 bits, and back.
37 *
38 * VERY WEAK encryption (Cryptography's ReadMe.md): the bytes made opaque - to a parser, and to most readers - and kept intact
39 * through a channel that would mangle them (mail, JSON, XML, an HTTP header); but secret from no one who recognizes it.
40 */
42
43 Memory::BLOB Decode (span<const char> s);
44 Memory::BLOB Decode (const string& s);
45 Memory::BLOB Decode (const u8string& s);
46 Memory::BLOB Decode (const Characters::String& s);
47 void Decode (const string& s, const Streams::OutputStream::Ptr<byte>& out);
48
49 /**
50 * What Encode puts after each 76 characters: a line break (as MIME's base64 has), or - eNone_LB - nothing, so the whole on
51 * one line (as base64 in an HTTP header, JSON, or an XML value usually is)
52 */
53 enum class LineBreak : uint8_t {
54 eLF_LB,
55 eCRLF_LB,
56 eNone_LB,
57 eAuto_LB = eCRLF_LB
58 };
59 struct Options {
60 LineBreak fLineBreak{LineBreak::eAuto_LB};
61 };
62
63 /**
64 * \note BLOB overload redundant, but provided since slight performance tweak (@todo maybe replace with span<byte> for that case/reason)
65 */
66 string Encode (const Streams::InputStream::Ptr<byte>& from, const Options& o = {});
67 string Encode (const Memory::BLOB& from, const Options& o = {});
68
69}
70
71namespace Stroika::Foundation::Cryptography::Encoding::Algorithm {
72
73 [[deprecated ("Since Stroika v3.0d5 use Base64::Decode")]] inline Memory::BLOB DecodeBase64 (span<const char> s)
74 {
75 return Base64::Decode (s);
76 }
77 [[deprecated ("Since Stroika v3.0d5 use Base64::Decode")]] inline Memory::BLOB DecodeBase64 (const string& s)
78 {
79 return Base64::Decode (s);
80 }
81 [[deprecated ("Since Stroika v3.0d5 use Base64::Decode")]] inline Memory::BLOB DecodeBase64 (const u8string& s)
82 {
83 return Base64::Decode (s);
84 }
85 [[deprecated ("Since Stroika v3.0d5 use Base64::Decode")]] inline Memory::BLOB DecodeBase64 (const Characters::String& s)
86 {
87 return Base64::Decode (s);
88 }
89 [[deprecated ("Since Stroika v3.0d5 use Base64::Decode")]] inline void DecodeBase64 (const string& s, const Streams::OutputStream::Ptr<byte>& out)
90 {
91 Base64::Decode (s, out);
92 }
93
94 using Base64::LineBreak; // deprecated
95 [[deprecated ("Since Stroika v3.0d5 use Base64::Encode")]] inline string EncodeBase64 (const Streams::InputStream::Ptr<byte>& from,
96 LineBreak lb = LineBreak::eAuto_LB)
97 {
98 return Base64::Encode (from, Base64::Options{.fLineBreak = lb});
99 }
100 [[deprecated ("Since Stroika v3.0d5 use Base64::Encode")]] inline string EncodeBase64 (const Memory::BLOB& from, LineBreak lb = LineBreak::eAuto_LB)
101 {
102 return Base64::Encode (from, Base64::Options{.fLineBreak = lb});
103 }
104
105}
106
107/*
108 ********************************************************************************
109 ***************************** Implementation Details ***************************
110 ********************************************************************************
111 */
112
113#endif /*_Stroika_Foundation_Cryptography_Base64_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
InputStream<>::Ptr is Smart pointer (with abstract Rep) class defining the interface to reading from ...
OutputStream<>::Ptr is Smart pointer to a stream-based sink of data.
string Encode(const Streams::InputStream::Ptr< byte > &from, const Options &o={})
Definition Base64.cpp:303