2021-06-27 21:25:29 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: HashStream.cpp
|
|
|
|
Date: 2021-6-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2021-06-27 21:25:29 +00:00
|
|
|
#include "HashStream.hpp"
|
|
|
|
#include <tomcrypt.h>
|
|
|
|
|
|
|
|
namespace Aurora::Hashing
|
|
|
|
{
|
|
|
|
class HashStream : public IHashStream
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
HashStream(EHashType type);
|
|
|
|
|
|
|
|
void Ingest(const void *buffer, AuUInt32 len) override;
|
2021-09-06 10:58:08 +00:00
|
|
|
AuUInt8 const * GetBytes(AuUInt32 &length) override;
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
private:
|
2021-09-06 10:58:08 +00:00
|
|
|
AuUInt8 buffer_[64] {};
|
|
|
|
hash_state state_ {};
|
|
|
|
EHashType type_ {};
|
|
|
|
bool finished_ {};
|
2021-06-27 21:25:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
HashStream::HashStream(EHashType type) : type_(type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case EHashType::eMD5:
|
|
|
|
md5_init(&state_);
|
|
|
|
break;
|
|
|
|
case EHashType::eSHA1:
|
|
|
|
sha1_init(&state_);
|
|
|
|
break;
|
|
|
|
case EHashType::eSHA2_32:
|
|
|
|
sha256_init(&state_);
|
|
|
|
break;
|
|
|
|
case EHashType::eSHA2_64:
|
|
|
|
sha512_init(&state_);
|
|
|
|
break;
|
|
|
|
case EHashType::eTiger:
|
|
|
|
tiger_init(&state_);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HashStream::Ingest(const void *buffer, AuUInt32 len)
|
|
|
|
{
|
2021-09-06 10:58:08 +00:00
|
|
|
if (finished_)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
switch (type_)
|
|
|
|
{
|
|
|
|
case EHashType::eMD5:
|
|
|
|
md5_process(&state_, reinterpret_cast<const unsigned char *>(buffer), len);
|
|
|
|
break;
|
|
|
|
case EHashType::eSHA1:
|
|
|
|
sha1_process(&state_, reinterpret_cast<const unsigned char *>(buffer), len);
|
|
|
|
break;
|
|
|
|
case EHashType::eSHA2_32:
|
|
|
|
sha256_process(&state_, reinterpret_cast<const unsigned char *>(buffer), len);
|
|
|
|
break;
|
|
|
|
case EHashType::eSHA2_64:
|
|
|
|
sha512_process(&state_, reinterpret_cast<const unsigned char *>(buffer), len);
|
|
|
|
break;
|
|
|
|
case EHashType::eTiger:
|
|
|
|
tiger_process(&state_, reinterpret_cast<const unsigned char *>(buffer), len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
AuUInt8 const* HashStream::GetBytes(AuUInt32 &length)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
switch (type_)
|
|
|
|
{
|
|
|
|
case EHashType::eMD5:
|
|
|
|
length = 16;
|
2021-09-06 10:58:08 +00:00
|
|
|
if (!std::exchange(finished_, true))
|
|
|
|
{
|
|
|
|
md5_done(&state_, reinterpret_cast<unsigned char *>(buffer_));
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
return buffer_;
|
|
|
|
case EHashType::eSHA1:
|
|
|
|
length = 20;
|
2021-09-06 10:58:08 +00:00
|
|
|
if (!std::exchange(finished_, true))
|
|
|
|
{
|
|
|
|
sha1_done(&state_, reinterpret_cast<unsigned char *>(buffer_));
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
return buffer_;
|
|
|
|
case EHashType::eSHA2_32:
|
|
|
|
length = 32;
|
2021-09-06 10:58:08 +00:00
|
|
|
if (!std::exchange(finished_, true))
|
|
|
|
{
|
|
|
|
sha256_done(&state_, reinterpret_cast<unsigned char *>(buffer_));
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
return buffer_;
|
|
|
|
case EHashType::eSHA2_64:
|
|
|
|
length = 64;
|
2021-09-06 10:58:08 +00:00
|
|
|
if (!std::exchange(finished_, true))
|
|
|
|
{
|
|
|
|
sha512_done(&state_, reinterpret_cast<unsigned char *>(buffer_));
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
return buffer_;
|
|
|
|
case EHashType::eTiger:
|
|
|
|
length = 24;
|
2021-09-06 10:58:08 +00:00
|
|
|
if (!std::exchange(finished_, true))
|
|
|
|
{
|
|
|
|
tiger_done(&state_, reinterpret_cast<unsigned char *>(buffer_));
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
return buffer_;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM IHashStream *HashStreamNew(EHashType type)
|
|
|
|
{
|
|
|
|
return _new HashStream(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM void HashStreamRelease(IHashStream *stream)
|
|
|
|
{
|
|
|
|
SafeDelete<HashStream*>(stream);
|
|
|
|
}
|
|
|
|
}
|