From 5377b45d9743bf78b8cd75a6ce0dd5f0cae21213 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Thu, 30 Jul 2015 14:58:35 +0200 Subject: [PATCH] Fixed signedness, simplified word-shift --- src/crypto/_digest_common.cpp | 28 ++++++++++++---------------- src/crypto/_digest_common.hpp | 2 +- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/crypto/_digest_common.cpp b/src/crypto/_digest_common.cpp index eeb1bc7..2c5c652 100644 --- a/src/crypto/_digest_common.cpp +++ b/src/crypto/_digest_common.cpp @@ -1,33 +1,29 @@ -#include #include #include "_digest_common.hpp" namespace fc { namespace detail { - static void shift_l( const uint32_t* in, uint32_t* out, std::size_t n, int i) { + static void shift_l( const uint8_t* in, uint8_t* out, std::size_t n, unsigned int i) { if (i < n) { - memcpy( out, in + i, 4*(n-i) ); + memcpy( out, in + i, n-i ); } else { i = n; } - memset( out + (n-i), 0, 4 * i ); + memset( out + (n-i), 0, i ); } - void shift_l( const char* in, char* out, std::size_t n, int i) { - FC_ASSERT( (n & 3) == 0 ); // all hashes are a multiple of 32 bit - n >>= 2; - const uint32_t* in32 = (uint32_t*) in; - uint32_t* out32 = (uint32_t*) out; + void shift_l( const char* in, char* out, std::size_t n, unsigned int i) { + const uint8_t* in8 = (uint8_t*) in; + uint8_t* out8 = (uint8_t*) out; - if (i >= 32) { - shift_l( in32, out32, n, i >> 5 ); - i &= 0x1f; - in32 = out32; + if (i >= 8) { + shift_l( in8, out8, n, i >> 3 ); + i &= 7; + in8 = out8; } std::size_t p; for( p = 0; p < n-1; ++p ) - out32[p] = boost::endian::native_to_big(boost::endian::big_to_native(in32[p]) << i - | (boost::endian::big_to_native(in32[p+1])>>(32-i))); - out32[p] = boost::endian::native_to_big(boost::endian::big_to_native(in32[p]) << i); + out8[p] = (in8[p] << i) | (in8[p+1]>>(8-i)); + out8[p] = in8[p] << i; } }} diff --git a/src/crypto/_digest_common.hpp b/src/crypto/_digest_common.hpp index a95b76d..8aa21b3 100644 --- a/src/crypto/_digest_common.hpp +++ b/src/crypto/_digest_common.hpp @@ -3,5 +3,5 @@ /* Common stuff for cryptographic hashes */ namespace fc { namespace detail { - void shift_l( const char* in, char* out, std::size_t n, int i); + void shift_l( const char* in, char* out, std::size_t n, unsigned int i); }}