diff --git a/AddressBook.cpp b/AddressBook.cpp index 75ca5761..dfd788bf 100644 --- a/AddressBook.cpp +++ b/AddressBook.cpp @@ -80,7 +80,7 @@ void AddressBook::LoadHosts () getline(f, s); if (!s.length()) - break; + continue; // skip empty line size_t pos = s.find('='); @@ -90,8 +90,11 @@ void AddressBook::LoadHosts () std::string addr = s.substr(pos); Identity ident; - Base64ToByteStream (addr.c_str(), addr.length(), (uint8_t *)&ident, sizeof (ident)); - m_Addresses[name] = CalculateIdentHash (ident); + if (!ident.FromBase64(addr)) { + LogPrint ("hosts.txt: ignore ", name); + continue; + } + m_Addresses[name] = ident.Hash(); numAddresses++; } } diff --git a/Identity.cpp b/Identity.cpp index 387eef50..435c0c9b 100644 --- a/Identity.cpp +++ b/Identity.cpp @@ -6,6 +6,7 @@ #include #include "CryptoConst.h" #include "Identity.h" +#include "base64.h" namespace i2p { @@ -17,6 +18,19 @@ namespace data memcpy (publicKey, keys.publicKey, sizeof (publicKey) + sizeof (signingKey)); memset (certificate, 0, sizeof (certificate)); return *this; + } + + bool Identity::FromBase64 (const std::string& s) + { + size_t count = Base64ToByteStream (s.c_str(), s.length(), reinterpret_cast (this), sizeof (Identity)); + return count == sizeof(Identity); + } + + IdentHash Identity::Hash() + { + IdentHash hash; + CryptoPP::SHA256().CalculateDigest(reinterpret_cast(&hash), reinterpret_cast (this), sizeof (Identity)); + return hash; } PrivateKeys& PrivateKeys::operator=(const Keys& keys) diff --git a/Identity.h b/Identity.h index 1dafdef4..902153dc 100644 --- a/Identity.h +++ b/Identity.h @@ -9,6 +9,8 @@ namespace i2p { namespace data { + class IdentHash; + #pragma pack(1) struct DHKeysPair // transient keys for transport sessions @@ -32,6 +34,8 @@ namespace data uint8_t certificate[3]; Identity& operator=(const Keys& keys); + bool FromBase64(const std::string&); + IdentHash Hash(); }; struct PrivateKeys // for eepsites