Fixed output of BigNumber::AsByteArray when generated array is shorter than requested size thx Shauren

This commit is contained in:
zamalaev 2020-02-20 21:00:00 +00:00 committed by Antz
parent c8d020c7ea
commit c604fd3cc8

View file

@ -198,23 +198,20 @@ uint8* BigNumber::AsByteArray(int minSize)
uint8 *BigNumber::AsByteArray(int minSize, bool reverse)
{
int length = (minSize >= GetNumBytes()) ? minSize : GetNumBytes();
int numBytes = GetNumBytes();
int length = (minSize >= numBytes) ? minSize : numBytes;
if (_array)
{
delete[] _array;
_array = NULL;
}
delete[] _array;
_array = new uint8[length];
// If we need more bytes than length of BigNumber set the rest to 0
if (length > GetNumBytes())
if (length > numBytes)
memset((void*)_array, 0, length);
BN_bn2bin(_bn, (unsigned char *)_array);
BN_bn2bin(_bn, (unsigned char*)_array);
if (reverse)
std::reverse(_array, _array + length);
std::reverse(_array, _array + numBytes);
return _array;
}