Browse Source

from/to base64

pull/113/head
orignal 10 years ago
parent
commit
9acf80e563
  1. 31
      Identity.cpp
  2. 6
      Identity.h
  3. 55
      SAM.cpp

31
Identity.cpp

@ -194,6 +194,16 @@ namespace data
return FromBuffer (buf, len); 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 size_t IdentityEx::GetSigningPublicKeyLen () const
{ {
if (!m_Verifier) CreateVerifier (); if (!m_Verifier) CreateVerifier ();
@ -319,6 +329,27 @@ namespace data
return ret; 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 void PrivateKeys::Sign (const uint8_t * buf, int len, uint8_t * signature) const
{ {
if (m_Signer) if (m_Signer)

6
Identity.h

@ -129,9 +129,10 @@ namespace data
IdentityEx& operator=(const IdentityEx& other); IdentityEx& operator=(const IdentityEx& other);
IdentityEx& operator=(const Identity& standard); IdentityEx& operator=(const Identity& standard);
size_t FromBase64(const std::string& s);
size_t FromBuffer (const uint8_t * buf, size_t len); size_t FromBuffer (const uint8_t * buf, size_t len);
size_t ToBuffer (uint8_t * buf, size_t len) const; 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 Identity& GetStandardIdentity () const { return m_StandardIdentity; };
const IdentHash& GetIdentHash () const { return m_IdentHash; }; const IdentHash& GetIdentHash () const { return m_IdentHash; };
size_t GetFullLen () const { return m_ExtendedLen + DEFAULT_IDENTITY_SIZE; }; 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 FromBuffer (const uint8_t * buf, size_t len);
size_t ToBuffer (uint8_t * buf, size_t len) const; 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); static PrivateKeys CreateRandomKeys (SigningKeyType type = SIGNING_KEY_TYPE_DSA_SHA1);
private: private:

55
SAM.cpp

@ -287,10 +287,8 @@ namespace client
m_Session = m_Owner.FindSession (id); m_Session = m_Owner.FindSession (id);
if (m_Session) if (m_Session)
{ {
uint8_t ident[1024];
size_t l = i2p::data::Base64ToByteStream (destination.c_str (), destination.length (), ident, 1024);
i2p::data::IdentityEx dest; i2p::data::IdentityEx dest;
dest.FromBuffer (ident, l); dest.FromBase64 (destination);
context.GetAddressBook ().InsertAddress (dest); context.GetAddressBook ().InsertAddress (dest);
auto leaseSet = i2p::data::netdb.FindLeaseSet (dest.GetIdentHash ()); auto leaseSet = i2p::data::netdb.FindLeaseSet (dest.GetIdentHash ());
if (leaseSet) if (leaseSet)
@ -364,19 +362,12 @@ namespace client
auto localDestination = i2p::client::context.CreateNewLocalDestination (); auto localDestination = i2p::client::context.CreateNewLocalDestination ();
if (localDestination) if (localDestination)
{ {
uint8_t buf[1024]; auto priv = localDestination->GetPrivateKeys ().ToBase64 ();
char priv[1024], pub[1024]; auto pub = localDestination->GetIdentity ().ToBase64 ();
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;
#ifdef _MSC_VER #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 #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 #endif
SendMessageReply (m_Buffer, len, true); SendMessageReply (m_Buffer, len, true);
} }
@ -418,17 +409,13 @@ namespace client
void SAMSocket::SendNamingLookupReply (const i2p::data::IdentityEx& identity) void SAMSocket::SendNamingLookupReply (const i2p::data::IdentityEx& identity)
{ {
uint8_t buf[1024]; auto base64 = identity.ToBase64 ();
char pub[1024];
size_t l = identity.ToBuffer (buf, 1024);
size_t l1 = i2p::data::ByteStreamToBase64 (buf, l, pub, 1024);
pub[l1] = 0;
#ifdef _MSC_VER #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 #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 #endif
SendMessageReply (m_Buffer, l2, false); SendMessageReply (m_Buffer, l, false);
} }
void SAMSocket::ExtractParams (char * buf, size_t len, std::map<std::string, std::string>& params) void SAMSocket::ExtractParams (char * buf, size_t len, std::map<std::string, std::string>& params)
@ -534,19 +521,16 @@ namespace client
void SAMSocket::HandleI2PDatagramReceive (const i2p::data::IdentityEx& ident, const uint8_t * buf, size_t len) void SAMSocket::HandleI2PDatagramReceive (const i2p::data::IdentityEx& ident, const uint8_t * buf, size_t len)
{ {
uint8_t identBuf[1024]; auto base64 = ident.ToBase64 ();
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;
#ifdef _MSC_VER #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 #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 #endif
if (len < SAM_SOCKET_BUFFER_SIZE - l2) if (len < SAM_SOCKET_BUFFER_SIZE - l)
{ {
memcpy (m_StreamBuffer + l2, buf, len); memcpy (m_StreamBuffer + l, buf, len);
boost::asio::async_write (m_Socket, boost::asio::buffer (m_StreamBuffer, len + l2), boost::asio::async_write (m_Socket, boost::asio::buffer (m_StreamBuffer, len + l),
std::bind (&SAMSocket::HandleWriteI2PData, shared_from_this (), std::placeholders::_1)); std::bind (&SAMSocket::HandleWriteI2PData, shared_from_this (), std::placeholders::_1));
} }
else else
@ -627,11 +611,8 @@ namespace client
ClientDestination * localDestination = nullptr; ClientDestination * localDestination = nullptr;
if (destination != "") 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; i2p::data::PrivateKeys keys;
keys.FromBuffer (buf, l); keys.FromBase64 (destination);
delete[] buf;
localDestination = i2p::client::context.CreateNewLocalDestination (keys, true, params); localDestination = i2p::client::context.CreateNewLocalDestination (keys, true, params);
} }
else // transient else // transient
@ -700,10 +681,8 @@ namespace client
auto session = FindSession (sessionID); auto session = FindSession (sessionID);
if (session) if (session)
{ {
uint8_t ident[1024];
size_t l = i2p::data::Base64ToByteStream (destination, strlen(destination), ident, 1024);
i2p::data::IdentityEx dest; i2p::data::IdentityEx dest;
dest.FromBuffer (ident, l); dest.FromBase64 (destination);
auto leaseSet = i2p::data::netdb.FindLeaseSet (dest.GetIdentHash ()); auto leaseSet = i2p::data::netdb.FindLeaseSet (dest.GetIdentHash ());
if (leaseSet) if (leaseSet)
session->localDestination->GetDatagramDestination ()-> session->localDestination->GetDatagramDestination ()->

Loading…
Cancel
Save