bool OTASCIIArmor::WriteArmoredFile( const String& foldername, const String& filename, const // for "-----BEGIN OT LEDGER-----", str_type would contain "LEDGER" std::string str_type, // There's no default, to force you to enter the right // string. bool bEscaped) const { OT_ASSERT(foldername.Exists()); OT_ASSERT(filename.Exists()); String strOutput; if (WriteArmoredString(strOutput, str_type, bEscaped) && strOutput.Exists()) { // WRITE IT TO THE FILE // StorePlainString will attempt to create all the folders leading up to // the path // for the output file. // bool bSaved = OTDB::StorePlainString(strOutput.Get(), foldername.Get(), filename.Get()); if (!bSaved) { otErr << "OTASCIIArmor::WriteArmoredFile" << ": Failed saving to file: %s%s%s\n\n Contents:\n\n" << strOutput << "\n\n", foldername.Get(), Log::PathSeparator(), filename.Get(); return false; } return true; } return false; }
bool OTASCIIArmor::SaveTo_ofstream(std::ofstream& fout) { String strOutput; std::string str_type("DATA"); // -----BEGIN OT ARMORED DATA----- if (WriteArmoredString(strOutput, str_type) && strOutput.Exists()) { // WRITE IT TO THE FILE // fout << strOutput; if (fout.fail()) { otErr << __FUNCTION__ << ": Failed saving to file.\n Contents:\n\n" << strOutput << "\n\n"; return false; } return true; } return false; }
// static bool LegacySymmetric::Encrypt( const LegacySymmetric& theKey, const String& strPlaintext, String& strOutput, const String& pstrDisplay, bool bBookends, const OTPassword* pAlreadyHavePW) { if (!theKey.IsGenerated()) { LogDetail(OT_METHOD)(__FUNCTION__)( ": Failure: theKey.IsGenerated() was false. (The calling " "code probably should have checked that key already...).") .Flush(); return false; } if (!strPlaintext.Exists()) { LogDetail(OT_METHOD)(__FUNCTION__)( ": Plaintext is empty. Please supply. (Failure).") .Flush(); return false; } // By this point, we know we have a plaintext and a symmetric Key. // std::unique_ptr<OTPassword> pPassUserInput; if (nullptr == pAlreadyHavePW) { const char* szDisplay = "Password-protecting a plaintext."; const auto strDisplay = String::Factory( (!pstrDisplay.Exists()) ? szDisplay : pstrDisplay.Get()); pPassUserInput.reset( GetPassphraseFromUser(strDisplay)); // bAskTwice=false // by default. } else pPassUserInput.reset(new OTPassword(*pAlreadyHavePW)); auto ascOutput = Armored::Factory(); bool bSuccess = false; if (nullptr != pPassUserInput) // Success retrieving the passphrase from // the user. (Now let's encrypt...) { OTEnvelope theEnvelope; if (theEnvelope.Encrypt( strPlaintext, const_cast<LegacySymmetric&>(theKey), *pPassUserInput) && theEnvelope.GetCiphertext(ascOutput)) { bSuccess = true; if (bBookends) { return ascOutput->WriteArmoredString( strOutput, "SYMMETRIC MSG", // todo hardcoding. false); // bEscaped=false } else { strOutput.Set(ascOutput->Get()); } } else { LogDetail(OT_METHOD)(__FUNCTION__)( ": Failed trying to encrypt. (Sorry).") .Flush(); } } else LogDetail(OT_METHOD)(__FUNCTION__)( ": Sorry, unable to retrieve passphrase from user. (Failure).") .Flush(); return bSuccess; }