AuroraRuntime/Source/Hashing/HashStream.cpp
2021-06-27 22:25:29 +01:00

109 lines
3.0 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: HashStream.cpp
Date: 2021-6-12
Author: Reece
***/
#include <RuntimeInternal.hpp>
#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;
AuUInt8 *GetBytes(AuUInt32 &length) override;
private:
AuUInt8 buffer_[64]{};
hash_state state_{};
EHashType type_{};
};
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)
{
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;
}
}
AuUInt8 *HashStream::GetBytes(AuUInt32 &length)
{
switch (type_)
{
case EHashType::eMD5:
length = 16;
md5_done(&state_, reinterpret_cast<unsigned char *>(buffer_));
return buffer_;
case EHashType::eSHA1:
length = 20;
sha1_done(&state_, reinterpret_cast<unsigned char *>(buffer_));
return buffer_;
case EHashType::eSHA2_32:
length = 32;
sha256_done(&state_, reinterpret_cast<unsigned char *>(buffer_));
return buffer_;
case EHashType::eSHA2_64:
length = 64;
sha512_done(&state_, reinterpret_cast<unsigned char *>(buffer_));
return buffer_;
case EHashType::eTiger:
length = 24;
tiger_done(&state_, reinterpret_cast<unsigned char *>(buffer_));
return buffer_;
}
return nullptr;
}
AUKN_SYM IHashStream *HashStreamNew(EHashType type)
{
return _new HashStream(type);
}
AUKN_SYM void HashStreamRelease(IHashStream *stream)
{
SafeDelete<HashStream*>(stream);
}
}