1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:27:35 +00:00

LibCrypto: Add ::import_data() and ::export_data() to UnsignedBigInteger

These functions allow conversion to-and-from big-endian buffers
This commit also adds a ""_bigint operator for easy bigint use
This commit is contained in:
AnotherTest 2020-04-10 01:00:37 +04:30 committed by Andreas Kling
parent c52d3e65b9
commit 6b742c69bd
3 changed files with 66 additions and 0 deletions

View file

@ -312,4 +312,32 @@ UnsignedBigInteger UnsignedBigInteger::create_invalid()
invalid.invalidate();
return invalid;
}
// FIXME: in great need of optimisation
UnsignedBigInteger UnsignedBigInteger::import_data(const u8* ptr, size_t length)
{
UnsignedBigInteger integer { 0 };
for (size_t i = 0; i < length; ++i) {
auto part = UnsignedBigInteger { ptr[length - i - 1] }.shift_left(8 * i);
integer = integer.add(part);
}
return integer;
}
size_t UnsignedBigInteger::export_data(AK::ByteBuffer& data)
{
UnsignedBigInteger copy { *this };
size_t size = trimmed_length() * sizeof(u32);
size_t i = 0;
for (; i < size; ++i) {
if (copy.length() == 0)
break;
data[size - i - 1] = copy.m_words[0] & 0xff;
copy = copy.divide(256).quotient;
}
return i;
}
}