hash/blake2: prefer views over cursors

This commit is contained in:
Danny Robson 2019-02-10 15:33:44 +11:00
parent 2e615b1b19
commit a8f718f1d9

View File

@ -143,33 +143,36 @@ blake2::blake2 (cruft::view<const u08 *> key)
//-----------------------------------------------------------------------------
blake2::digest_t
blake2::operator() (cruft::view<const u08 *> data) const noexcept
blake2::operator() (cruft::view<const u08 *> const data) const noexcept
{
auto h = ::traits::iv;
h[0] ^= 0x01010000 ^ (m_keylen << 8) ^ sizeof (digest_t);
if (m_keylen)
F (h, m_salt.val64.data (), ::traits::block_bytes, data.empty ());
else if (data.empty ()) {
CHECK_EQ (m_keylen, 0u);
// special case for the empty key and empty data
if (!m_keylen && data.empty ()) {
std::array<word_t,16> zeroes {};
F (h, zeroes.data (), 0, true);
}
u64 counter = m_keylen ? ::traits::block_bytes : 0;
auto cursor = data.begin ();
while (cursor + ::traits::block_bytes < data.end ()) {
auto remain = data;
while (remain.size () >= ::traits::block_bytes) {
counter += ::traits::block_bytes;
F (h, cruft::cast::alignment<word_t const*> (cursor), counter, false);
cursor += ::traits::block_bytes;
auto [head, tail] = remain.split (::traits::block_bytes);
F (h, head.cast<word_t const*> ().data (), counter, false);
remain = tail;
}
if (cursor != data.cend ()) {
if (!remain.empty ()) {
std::array<u64,16> tail {};
memcpy (tail.data(), data.data (), data.cend () - cursor);
counter += data.end () - cursor;
memcpy (tail.data(), remain.data (), remain.size ());
counter += remain.size ();
F (h, tail.data (), counter, true);
}