From 9acf80e563a84a8950a23e38e8b30d5c99757c49 Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 1 Dec 2014 14:50:10 -0500 Subject: [PATCH] from/to base64 --- Identity.cpp | 33 ++++++++++++++++++++++++++++++- Identity.h | 6 +++++- SAM.cpp | 55 ++++++++++++++++------------------------------------ 3 files changed, 54 insertions(+), 40 deletions(-) diff --git a/Identity.cpp b/Identity.cpp index 65bcf84f..4d6ef18b 100644 --- a/Identity.cpp +++ b/Identity.cpp @@ -193,7 +193,17 @@ namespace data auto len = Base64ToByteStream (s.c_str(), s.length(), buf, 512); return FromBuffer (buf, len); } - + + std::string IdentityEx::ToBase64 () const + { + uint8_t buf[512]; + char str[1024]; + size_t l = ToBuffer (buf, 512); + size_t l1 = i2p::data::ByteStreamToBase64 (buf, l, str, 1024); + str[l1] = 0; + return std::string (str); + } + size_t IdentityEx::GetSigningPublicKeyLen () const { if (!m_Verifier) CreateVerifier (); @@ -319,6 +329,27 @@ namespace data return ret; } + size_t PrivateKeys::FromBase64(const std::string& s) + { + uint8_t * buf = new uint8_t[s.length ()]; + size_t l = i2p::data::Base64ToByteStream (s.c_str (), s.length (), buf, s.length ()); + size_t ret = FromBuffer (buf, l); + delete[] buf; + return ret; + } + + std::string PrivateKeys::ToBase64 () const + { + uint8_t * buf = new uint8_t[GetFullLen ()]; + char * str = new char[GetFullLen ()*2]; + size_t l = ToBuffer (buf, GetFullLen ()); + size_t l1 = i2p::data::ByteStreamToBase64 (buf, l, str, GetFullLen ()*2); + str[l1] = 0; + delete[] buf; + delete[] str; + return std::string (str); + } + void PrivateKeys::Sign (const uint8_t * buf, int len, uint8_t * signature) const { if (m_Signer) diff --git a/Identity.h b/Identity.h index 0bb6f3f1..1a245421 100644 --- a/Identity.h +++ b/Identity.h @@ -129,9 +129,10 @@ namespace data IdentityEx& operator=(const IdentityEx& other); IdentityEx& operator=(const Identity& standard); - size_t FromBase64(const std::string& s); size_t FromBuffer (const uint8_t * buf, size_t len); size_t ToBuffer (uint8_t * buf, size_t len) const; + size_t FromBase64(const std::string& s); + std::string ToBase64 () const; const Identity& GetStandardIdentity () const { return m_StandardIdentity; }; const IdentHash& GetIdentHash () const { return m_IdentHash; }; size_t GetFullLen () const { return m_ExtendedLen + DEFAULT_IDENTITY_SIZE; }; @@ -175,6 +176,9 @@ namespace data size_t FromBuffer (const uint8_t * buf, size_t len); size_t ToBuffer (uint8_t * buf, size_t len) const; + size_t FromBase64(const std::string& s); + std::string ToBase64 () const; + static PrivateKeys CreateRandomKeys (SigningKeyType type = SIGNING_KEY_TYPE_DSA_SHA1); private: diff --git a/SAM.cpp b/SAM.cpp index e6dc5847..3135f106 100644 --- a/SAM.cpp +++ b/SAM.cpp @@ -287,10 +287,8 @@ namespace client m_Session = m_Owner.FindSession (id); if (m_Session) { - uint8_t ident[1024]; - size_t l = i2p::data::Base64ToByteStream (destination.c_str (), destination.length (), ident, 1024); i2p::data::IdentityEx dest; - dest.FromBuffer (ident, l); + dest.FromBase64 (destination); context.GetAddressBook ().InsertAddress (dest); auto leaseSet = i2p::data::netdb.FindLeaseSet (dest.GetIdentHash ()); if (leaseSet) @@ -364,19 +362,12 @@ namespace client auto localDestination = i2p::client::context.CreateNewLocalDestination (); if (localDestination) { - uint8_t buf[1024]; - char priv[1024], pub[1024]; - size_t l = localDestination->GetPrivateKeys ().ToBuffer (buf, 1024); - size_t l1 = i2p::data::ByteStreamToBase64 (buf, l, priv, 1024); - priv[l1] = 0; - - l = localDestination->GetIdentity ().ToBuffer (buf, 1024); - l1 = i2p::data::ByteStreamToBase64 (buf, l, pub, 1024); - pub[l1] = 0; + auto priv = localDestination->GetPrivateKeys ().ToBase64 (); + auto pub = localDestination->GetIdentity ().ToBase64 (); #ifdef _MSC_VER - size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_DEST_REPLY, pub, priv); + size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_DEST_REPLY, pub.c_str (), priv.c_str ()); #else - size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_DEST_REPLY, pub, priv); + size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_DEST_REPLY, pub.c_str (), priv.c_str ()); #endif SendMessageReply (m_Buffer, len, true); } @@ -418,17 +409,13 @@ namespace client void SAMSocket::SendNamingLookupReply (const i2p::data::IdentityEx& identity) { - uint8_t buf[1024]; - char pub[1024]; - size_t l = identity.ToBuffer (buf, 1024); - size_t l1 = i2p::data::ByteStreamToBase64 (buf, l, pub, 1024); - pub[l1] = 0; + auto base64 = identity.ToBase64 (); #ifdef _MSC_VER - size_t l2 = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY, pub); + size_t l = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY, base64.c_str ()); #else - size_t l2 = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY, pub); + size_t l = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY, base64.c_str ()); #endif - SendMessageReply (m_Buffer, l2, false); + SendMessageReply (m_Buffer, l, false); } void SAMSocket::ExtractParams (char * buf, size_t len, std::map& params) @@ -534,19 +521,16 @@ namespace client void SAMSocket::HandleI2PDatagramReceive (const i2p::data::IdentityEx& ident, const uint8_t * buf, size_t len) { - uint8_t identBuf[1024]; - size_t l = ident.ToBuffer (identBuf, 1024); - size_t l1 = i2p::data::ByteStreamToBase64 (identBuf, l, m_Buffer, SAM_SOCKET_BUFFER_SIZE); - m_Buffer[l1] = 0; + auto base64 = ident.ToBase64 (); #ifdef _MSC_VER - size_t l2 = sprintf_s ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_DATAGRAM_RECEIVED, m_Buffer, len); + size_t l = sprintf_s ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_DATAGRAM_RECEIVED, base64.c_str (), len); #else - size_t l2 = snprintf ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_DATAGRAM_RECEIVED, m_Buffer, len); + size_t l = snprintf ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_DATAGRAM_RECEIVED, base64.c_str (), len); #endif - if (len < SAM_SOCKET_BUFFER_SIZE - l2) + if (len < SAM_SOCKET_BUFFER_SIZE - l) { - memcpy (m_StreamBuffer + l2, buf, len); - boost::asio::async_write (m_Socket, boost::asio::buffer (m_StreamBuffer, len + l2), + memcpy (m_StreamBuffer + l, buf, len); + boost::asio::async_write (m_Socket, boost::asio::buffer (m_StreamBuffer, len + l), std::bind (&SAMSocket::HandleWriteI2PData, shared_from_this (), std::placeholders::_1)); } else @@ -627,11 +611,8 @@ namespace client ClientDestination * localDestination = nullptr; if (destination != "") { - uint8_t * buf = new uint8_t[destination.length ()]; - size_t l = i2p::data::Base64ToByteStream (destination.c_str (), destination.length (), buf, destination.length ()); i2p::data::PrivateKeys keys; - keys.FromBuffer (buf, l); - delete[] buf; + keys.FromBase64 (destination); localDestination = i2p::client::context.CreateNewLocalDestination (keys, true, params); } else // transient @@ -700,10 +681,8 @@ namespace client auto session = FindSession (sessionID); if (session) { - uint8_t ident[1024]; - size_t l = i2p::data::Base64ToByteStream (destination, strlen(destination), ident, 1024); i2p::data::IdentityEx dest; - dest.FromBuffer (ident, l); + dest.FromBase64 (destination); auto leaseSet = i2p::data::netdb.FindLeaseSet (dest.GetIdentHash ()); if (leaseSet) session->localDestination->GetDatagramDestination ()->