hash: prefer iterator style over base/length

This commit is contained in:
Danny Robson 2016-06-17 15:46:11 +10:00
parent ba4b22c208
commit aeba9033e0
16 changed files with 102 additions and 7 deletions

View File

@ -16,17 +16,33 @@
#include "bsdsum.hpp"
#include "../debug.hpp"
///////////////////////////////////////////////////////////////////////////////
uint16_t
bsdsum (const void *restrict _data, size_t size) {
const uint8_t *restrict data = static_cast<const uint8_t*> (_data);
bsdsum (const uint8_t *const restrict first, const uint8_t *const restrict last)
{
CHECK_LE (first, last);
uint16_t accum = 0;
for (size_t i = 0; i < size; ++i) {
for (auto cursor = first; cursor != last; ++cursor) {
accum = (accum >> 1u) | ((accum & 0x01u) << 15u);
accum += data[i];
accum += *cursor;
}
return accum;
}
///////////////////////////////////////////////////////////////////////////////
uint16_t
bsdsum (const void *restrict data, size_t size)
{
return bsdsum (
static_cast<const uint8_t*> (data),
static_cast<const uint8_t*> (data) + size
);
}

View File

@ -20,6 +20,7 @@
#include <cstdint>
#include <cstdlib>
uint16_t bsdsum (const void *restrict, size_t);
uint16_t bsdsum (const void *restrict data, size_t bytes);
uint16_t bsdsum (const uint8_t *restrict first, const uint8_t *restrict last);
#endif

View File

@ -19,6 +19,14 @@
#include "../endian.hpp"
#include "../debug.hpp"
uint32_t
crc32 (const uint8_t *restrict first, const uint8_t *restrict last) noexcept
{
not_implemented ();
}
uint32_t
crc32 (const void *restrict, size_t) {
not_implemented ();

View File

@ -20,7 +20,8 @@
#include <cstdint>
#include <cstdlib>
uint32_t crc32 (const void *restrict, size_t);
uint32_t crc32 (const void *restrict data, size_t bytes);
uint32_t crc32 (const uint8_t *restrict first, const uint8_t *restrict last) noexcept;
#endif

View File

@ -16,6 +16,7 @@
#include "md2.hpp"
#include "../debug.hpp"
#include "../types.hpp"
#include <array>
@ -72,6 +73,16 @@ MD2::reset (void)
///////////////////////////////////////////////////////////////////////////////
void
MD2::update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept
{
CHECK_LE (first, last);
update (first, last - first);
}
//-----------------------------------------------------------------------------
void
MD2::update (const void *data, size_t size)
{
update (static_cast<const uint8_t*> (data), size);

View File

@ -32,6 +32,7 @@ namespace util {
void update (const uint8_t *data, size_t len);
void update (const void *data, size_t len);
void update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept;
void finish (void);
digest_t digest (void) const;

View File

@ -17,8 +17,9 @@
#include "md4.hpp"
#include "../bitwise.hpp"
#include "../types.hpp"
#include "../debug.hpp"
#include "../endian.hpp"
#include "../types.hpp"
#include <cstring>
@ -71,6 +72,15 @@ MD4::reset (void) {
}
void
MD4::update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept
{
CHECK_LE (first, last);
update (first, last - first);
}
void
MD4::update (const void *data, size_t size)
{ update (static_cast<const uint8_t*> (data), size); }

View File

@ -31,6 +31,7 @@ namespace util {
void update (const void *data, size_t len);
void update (const uint8_t *data, size_t len);
void update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept;
void finish (void);
digest_t digest (void) const;

View File

@ -102,6 +102,15 @@ MD5::reset (void) {
}
void
MD5::update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept
{
CHECK_LE (first, last);
update (first, last - first);
}
void
MD5::update (const void *data, size_t len)
{ MD5::update (static_cast<const uint8_t*> (data), len); }

View File

@ -34,6 +34,7 @@ namespace util {
void update (const void *data, size_t len);
void update (const uint8_t *data, size_t len);
void update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept;
void finish (void);
digest_t digest (void) const;

View File

@ -49,6 +49,16 @@ RIPEMD::reset (void) {
///////////////////////////////////////////////////////////////////////////////
void
RIPEMD::update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept
{
CHECK_LE (first, last);
update (first, last - first);
}
//-----------------------------------------------------------------------------
void
RIPEMD::update (const uint8_t *data, size_t len) {
CHECK (data);

View File

@ -30,6 +30,8 @@ namespace util {
RIPEMD();
void update (const uint8_t*, size_t);
void update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept;
digest_t digest (void) const;
void finish (void);
void reset (void);

View File

@ -104,6 +104,16 @@ SHA1::reset (void) {
}
//-----------------------------------------------------------------------------
void
SHA1::update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept
{
CHECK_LE (first, last);
update (first, last - first);
}
//-----------------------------------------------------------------------------
void
SHA1::update (const uint8_t *data, size_t size) {

View File

@ -35,6 +35,8 @@ namespace util { namespace hash {
SHA1();
void update (const uint8_t *, size_t);
void update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept;
void finish (void);
digest_t digest (void) const;
void reset (void);

View File

@ -18,6 +18,7 @@
#include "sha2.hpp"
#include "../bitwise.hpp"
#include "../debug.hpp"
#include "../endian.hpp"
#include <algorithm>
@ -170,6 +171,15 @@ SHA256::SHA256 ():
}
void
SHA256::update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept
{
CHECK_LE (first, last);
update (first, last - first);
}
void
SHA256::update (const uint8_t *data, size_t length) {
while (length) {

View File

@ -31,6 +31,8 @@ namespace util {
SHA256();
void update (const uint8_t *, size_t);
void update (const uint8_t *restrict first, const uint8_t *restrict last) noexcept;
void finish (void);
digest_t digest (void) const;