uint256 ProofOfWork::solve (int maxIterations) const { if (!isValid ()) throw std::runtime_error ("invalid proof of work target/iteration"); uint256 nonce; RandomNumbers::getInstance ().fill (&nonce); std::vector<uint256> buf2; buf2.resize (mIterations); std::vector<uint256> buf1; buf1.resize (3); buf1[0] = mChallenge; while (maxIterations > 0) { buf1[1] = nonce; buf1[2].zero (); for (int i = (mIterations - 1); i >= 0; --i) { buf1[2] = getSHA512Half (buf1); buf2[i] = buf1[2]; } if (getSHA512Half (buf2) <= mTarget) return nonce; ++nonce; --maxIterations; } return uint256 (); }
bool ProofOfWork::checkSolution (uint256 const& solution) const { if (mIterations > kMaxIterations) return false; std::vector<uint256> buf1; buf1.push_back (mChallenge); buf1.push_back (solution); buf1.push_back (uint256 ()); std::vector<uint256> buf2; buf2.resize (mIterations); for (int i = (mIterations - 1); i >= 0; --i) { buf1[2] = getSHA512Half (buf1); buf2[i] = buf1[2]; } return getSHA512Half (buf2) <= mTarget; }
std::pair<uint256, bool> makeSharedValue (SSL* ssl, beast::Journal journal) { std::pair<uint256, bool> result = { {}, false }; unsigned char sha1[64]; unsigned char sha2[64]; if (!hashLastMessage(ssl, sha1, SSL_get_finished)) { journal.error << "Cookie generation: local setup not complete"; return result; } if (!hashLastMessage(ssl, sha2, SSL_get_peer_finished)) { journal.error << "Cookie generation: peer setup not complete"; return result; } // If both messages hash to the same value (i.e. match) something is // wrong. This would cause the resulting cookie to be 0. if (memcmp (sha1, sha2, sizeof (sha1)) == 0) { journal.error << "Cookie generation: identical finished messages"; return result; } for (size_t i = 0; i < sizeof (sha1); ++i) sha1[i] ^= sha2[i]; // Finally, derive the actual cookie for the values that // we have calculated. result.first = getSHA512Half (sha1, sizeof(sha1)); result.second = true; return result; }