MCPcopy Create free account
hub / github.com/PurpleI2P/i2pd / LoadPrivateKeys

Method LoadPrivateKeys

libi2pd_client/ClientContext.cpp:268–1134  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

266 }
267
268 bool ClientContext::LoadPrivateKeys (i2p::data::PrivateKeys& keys, std::string_view filename,
269 i2p::data::SigningKeyType sigType, i2p::data::CryptoKeyType cryptoType)
270 {
271#if __cplusplus >= 202002L // C++20
272 if (filename.starts_with ("transient"))
273#else
274 std::string_view transient("transient");
275 if (!filename.compare (0, transient.length (), transient)) // starts with transient
276#endif
277 {
278 keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType, cryptoType, true);
279 LogPrint (eLogInfo, "Clients: New transient keys address ", m_AddressBook.ToAddress(keys.GetPublic ()->GetIdentHash ()), " created");
280 return true;
281 }
282
283 bool success = true;
284 std::string fullPath = i2p::fs::DataDirPath (filename);
285 std::ifstream s(fullPath, std::ifstream::binary);
286 if (s.is_open ())
287 {
288 s.seekg (0, std::ios::end);
289 size_t len = s.tellg();
290 s.seekg (0, std::ios::beg);
291 uint8_t * buf = new uint8_t[len];
292 s.read ((char *)buf, len);
293 if(!keys.FromBuffer (buf, len))
294 {
295 LogPrint (eLogCritical, "Clients: Failed to load keyfile ", filename);
296 success = false;
297 }
298 else
299 LogPrint (eLogInfo, "Clients: Local address ", m_AddressBook.ToAddress(keys.GetPublic ()->GetIdentHash ()), " loaded");
300 delete[] buf;
301 }
302 else
303 {
304 LogPrint (eLogInfo, "Clients: Can't open file ", fullPath, " Creating new one with signature type ", sigType, " crypto type ", cryptoType);
305 keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType, cryptoType, true);
306 std::ofstream f (fullPath, std::ofstream::binary | std::ofstream::out);
307 size_t len = keys.GetFullLen ();
308 uint8_t * buf = new uint8_t[len];
309 len = keys.ToBuffer (buf, len);
310 f.write ((char *)buf, len);
311 delete[] buf;
312
313 LogPrint (eLogInfo, "Clients: New private keys file ", fullPath, " for ", m_AddressBook.ToAddress(keys.GetPublic ()->GetIdentHash ()), " created");
314 }
315 return success;
316 }
317
318 std::vector<std::shared_ptr<DatagramSessionInfo> > ClientContext::GetForwardInfosFor(const i2p::data::IdentHash & destination)
319 {
320 std::vector<std::shared_ptr<DatagramSessionInfo> > infos;
321 std::lock_guard<std::mutex> lock(m_ForwardsMutex);
322 for(const auto & c : m_ClientForwards)
323 {
324 if (c.second->IsLocalDestination(destination))
325 {

Callers

nothing calls this directly

Calls 9

CreateRandomKeysFunction · 0.85
LogPrintFunction · 0.85
DataDirPathFunction · 0.85
ToAddressMethod · 0.80
GetPublicMethod · 0.80
writeMethod · 0.80
FromBufferMethod · 0.45
GetFullLenMethod · 0.45
ToBufferMethod · 0.45

Tested by

no test coverage detected