const std::string getHash(std::string fname){ std::ifstream f; f.open(fname.c_str()); SHA256 digest; const size_t BufferSize = 144 * 7 * 1024; char * buffer = new char[BufferSize]; if (f != NULL){ f.read(buffer, BufferSize);//read(char *s, streamsize n) std::size_t numByte = size_t(f.gcount());//gcount(): return the number of characters extracted by the last unformatted input operation digest.add(buffer, numByte); } f.close(); delete[] buffer; return digest.getHash(); }
std::string GetSha256ForFile(std::filesystem::path Filename) { SHA256 SHA; std::ifstream InStream(Filename.string()); unsigned char tmpbuf[256]; if (!InStream.is_open()) return ""; while (!InStream.eof()) { InStream.read((char*)tmpbuf, 256); size_t cnt = InStream.gcount(); SHA.add(tmpbuf, cnt); } return std::string(SHA.getHash()); }
void TransactionFrame::storeTransaction(LedgerManager& ledgerManager, LedgerDelta const& delta, int txindex, SHA256& resultHasher) const { auto txBytes(xdr::xdr_to_opaque(mEnvelope)); auto txResultBytes(xdr::xdr_to_opaque(getResultPair())); resultHasher.add(txResultBytes); std::string txBody = base64::encode( reinterpret_cast<const unsigned char*>(txBytes.data()), txBytes.size()); std::string txResult = base64::encode( reinterpret_cast<const unsigned char*>(txResultBytes.data()), txResultBytes.size()); xdr::opaque_vec<> txMeta(delta.getTransactionMeta()); std::string meta = base64::encode( reinterpret_cast<const unsigned char*>(txMeta.data()), txMeta.size()); string txIDString(binToHex(getContentsHash())); auto timer = ledgerManager.getDatabase().getInsertTimer("txhistory"); soci::statement st = (ledgerManager.getDatabase().getSession().prepare << "INSERT INTO txhistory (txid, ledgerseq, txindex, txbody, " "txresult, txmeta) VALUES " "(:id,:seq,:txindex,:txb,:txres,:meta)", soci::use(txIDString), soci::use(ledgerManager.getCurrentLedgerHeader().ledgerSeq), soci::use(txindex), soci::use(txBody), soci::use(txResult), soci::use(meta)); st.execute(true); if (st.get_affected_rows() != 1) { throw std::runtime_error("Could not update data in SQL"); } }
std::string GetSha256ForFile(std::string Filename) { SHA256 SHA; #ifndef WIN32 std::ifstream InStream(Filename.c_str()); #else std::ifstream InStream(Utility::Widen(Filename).c_str()); #endif unsigned char tmpbuf[256]; if (!InStream.is_open()) return ""; while (!InStream.eof()) { InStream.read((char*)tmpbuf, 256); size_t cnt = InStream.gcount(); SHA.add(tmpbuf, cnt); } return std::string(SHA.getHash()); }