AuroraRuntime/Source/Parse/Base32.cpp

53 lines
1.4 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Base32.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 "Base32.hpp"
#include <tomcrypt.h>
namespace Aurora::Parse
{
2022-01-20 16:37:22 +00:00
AUKN_SYM bool Base32Decode(const AuString &in, AuByteBuffer &decoded)
2021-06-27 21:25:29 +00:00
{
unsigned long length = in.size();
2022-03-30 14:18:56 +00:00
if (!AuTryResize(decoded, length))
2021-06-27 21:25:29 +00:00
{
2022-03-30 14:18:56 +00:00
SysPushErrorMem();
return false;
2021-06-27 21:25:29 +00:00
}
2022-03-30 14:18:56 +00:00
auto status = base32_decode(in.data(), in.size(), reinterpret_cast<unsigned char *>(&decoded[0]), &length, BASE32_RFC4648);
if (!AuTryResize(decoded, length))
2021-06-27 21:25:29 +00:00
{
2022-03-30 14:18:56 +00:00
SysPushErrorMem();
return false;
2021-06-27 21:25:29 +00:00
}
2022-03-30 14:18:56 +00:00
return status == CRYPT_OK;
2021-06-27 21:25:29 +00:00
}
AUKN_SYM bool Base32Encode(const void *buffer, AuMach length, AuString &encoded)
{
unsigned long outLength = (length * 8 + 4) / 5;
2022-03-30 14:18:56 +00:00
if (!AuTryResize(encoded, length))
2021-06-27 21:25:29 +00:00
{
2022-03-30 14:18:56 +00:00
SysPushErrorMem();
return false;
2021-06-27 21:25:29 +00:00
}
2022-03-30 14:18:56 +00:00
auto status = base32_encode(reinterpret_cast<const unsigned char *>(buffer), length, &encoded[0], &outLength, BASE32_RFC4648);
if (!AuTryResize(encoded, length))
2021-06-27 21:25:29 +00:00
{
2022-03-30 14:18:56 +00:00
SysPushErrorMem();
return false;
2021-06-27 21:25:29 +00:00
}
2022-03-30 14:18:56 +00:00
return status == CRYPT_OK;
2021-06-27 21:25:29 +00:00
}
}