From 1ae432917c9c194b71e8a5422ea17cdb608c3650 Mon Sep 17 00:00:00 2001 From: noloader Date: Thu, 2 Jul 2015 00:58:24 +0000 Subject: Added Base64URLEncoder and decoder for web safe alphabet from RFC 4648, Section 5. Discussion at https://groups.google.com/d/msg/cryptopp-users/OF5RPXW-cHw/EDrOuA4-rRYJ git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@563 57ff6487-cd31-0410-9ec3-f628ee90f5f0 --- base64.cpp | 43 +++++++++++++++++++++++++++++++++++++++---- base64.h | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/base64.cpp b/base64.cpp index 7571f2b..8289c0c 100644 --- a/base64.cpp +++ b/base64.cpp @@ -5,8 +5,13 @@ NAMESPACE_BEGIN(CryptoPP) -static const byte s_vec[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +// Base64 +static const byte s_vec1[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +// Base64URL +static const byte s_vec2[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; static const byte s_padding = '='; void Base64Encoder::IsolatedInitialize(const NameValuePairs ¶meters) @@ -18,7 +23,24 @@ void Base64Encoder::IsolatedInitialize(const NameValuePairs ¶meters) m_filter->Initialize(CombinedNameValuePairs( parameters, - MakeParameters(Name::EncodingLookupArray(), &s_vec[0], false) + MakeParameters(Name::EncodingLookupArray(), &s_vec1[0], false) + (Name::PaddingByte(), s_padding) + (Name::GroupSize(), insertLineBreaks ? maxLineLength : 0) + (Name::Separator(), ConstByteArrayParameter(lineBreak)) + (Name::Terminator(), ConstByteArrayParameter(lineBreak)) + (Name::Log2Base(), 6, true))); +} + +void Base64URLEncoder::IsolatedInitialize(const NameValuePairs ¶meters) +{ + bool insertLineBreaks = parameters.GetValueWithDefault(Name::InsertLineBreaks(), true); + int maxLineLength = parameters.GetIntValueWithDefault(Name::MaxLineLength(), 72); + + const char *lineBreak = insertLineBreaks ? "\n" : ""; + + m_filter->Initialize(CombinedNameValuePairs( + parameters, + MakeParameters(Name::EncodingLookupArray(), &s_vec2[0], false) (Name::PaddingByte(), s_padding) (Name::GroupSize(), insertLineBreaks ? maxLineLength : 0) (Name::Separator(), ConstByteArrayParameter(lineBreak)) @@ -30,10 +52,23 @@ const int *Base64Decoder::GetDecodingLookupArray() { static volatile bool s_initialized = false; static int s_array[256]; + + if (!s_initialized) + { + InitializeDecodingLookupArray(s_array, s_vec1, 64, false); + s_initialized = true; + } + return s_array; +} +const int *Base64URLDecoder::GetDecodingLookupArray() +{ + static volatile bool s_initialized = false; + static int s_array[256]; + if (!s_initialized) { - InitializeDecodingLookupArray(s_array, s_vec, 64, false); + InitializeDecodingLookupArray(s_array, s_vec2, 64, false); s_initialized = true; } return s_array; diff --git a/base64.h b/base64.h index 5a9e184..428b62c 100644 --- a/base64.h +++ b/base64.h @@ -5,7 +5,8 @@ NAMESPACE_BEGIN(CryptoPP) -//! Base64 Encoder Class +//! Base64 Encoder Class +// https://tools.ietf.org/html/rfc4648#section-4 class Base64Encoder : public SimpleProxyFilter { public: @@ -14,19 +15,47 @@ public: { IsolatedInitialize(MakeParameters(Name::InsertLineBreaks(), insertLineBreaks)(Name::MaxLineLength(), maxLineLength)); } - + void IsolatedInitialize(const NameValuePairs ¶meters); }; -//! Base64 Decoder Class +//! Base64 Decoder Class +// https://tools.ietf.org/html/rfc4648#section-4 class Base64Decoder : public BaseN_Decoder { public: Base64Decoder(BufferedTransformation *attachment = NULL) : BaseN_Decoder(GetDecodingLookupArray(), 6, attachment) {} - + void IsolatedInitialize(const NameValuePairs ¶meters) {} + +private: + static const int * CRYPTOPP_API GetDecodingLookupArray(); +}; + +//! Base64 URL Encoder Class +// https://tools.ietf.org/html/rfc4648#section-5 +class Base64URLEncoder : public SimpleProxyFilter +{ +public: + Base64URLEncoder(BufferedTransformation *attachment = NULL, bool insertLineBreaks = false, int maxLineLength = -1) + : SimpleProxyFilter(new BaseN_Encoder(new Grouper), attachment) + { + IsolatedInitialize(MakeParameters(Name::InsertLineBreaks(), insertLineBreaks)(Name::MaxLineLength(), maxLineLength)); + } + + void IsolatedInitialize(const NameValuePairs ¶meters); +}; +//! Base64 URL Decoder Class +class Base64URLDecoder : public BaseN_Decoder +{ +public: + Base64URLDecoder(BufferedTransformation *attachment = NULL) + : BaseN_Decoder(GetDecodingLookupArray(), 6, attachment) {} + + void IsolatedInitialize(const NameValuePairs ¶meters) {} + private: static const int * CRYPTOPP_API GetDecodingLookupArray(); }; -- cgit v1.2.1