From a4f7e0fe73d4323c8361c0abff7e28ad9db7ac79 Mon Sep 17 00:00:00 2001 From: bdemsky Date: Fri, 2 Mar 2018 11:38:44 -0800 Subject: [PATCH] Add missing files --- version2/src/C/ByteBuffer.cc | 81 ++++++++++++++++++++++++++++++++++++ version2/src/C/Mac.cc | 19 +++++++++ 2 files changed, 100 insertions(+) create mode 100644 version2/src/C/ByteBuffer.cc create mode 100644 version2/src/C/Mac.cc diff --git a/version2/src/C/ByteBuffer.cc b/version2/src/C/ByteBuffer.cc new file mode 100644 index 0000000..e1f6d4b --- /dev/null +++ b/version2/src/C/ByteBuffer.cc @@ -0,0 +1,81 @@ +#include "ByteBuffer.h" +#include + +ByteBuffer::ByteBuffer(Array *array) : + buffer(array), + offset(0) { +} + +void ByteBuffer::put(char c) { + buffer->set(offset++ , c); +} + +void ByteBuffer::putInt(int32_t l) { + buffer->set(offset++, (char)(l >> 24)); + buffer->set(offset++, (char)((l >> 16) & 0xff)); + buffer->set(offset++, (char)((l >> 8) & 0xff)); + buffer->set(offset++, (char)(l & 0xff)); +} + +void ByteBuffer::putLong(int64_t l) { + buffer->set(offset++, (char)(l >> 56)); + buffer->set(offset++, (char)((l >> 48) & 0xff)); + buffer->set(offset++, (char)((l >> 40) & 0xff)); + buffer->set(offset++, (char)((l >> 32) & 0xff)); + buffer->set(offset++, (char)((l >> 24) & 0xff)); + buffer->set(offset++, (char)((l >> 16) & 0xff)); + buffer->set(offset++, (char)((l >> 8) & 0xff)); + buffer->set(offset++, (char)(l & 0xff)); +} + +void ByteBuffer::put(Array *array) { + memcpy(&buffer->internalArray()[offset], array->internalArray(), array->length()); + offset+=array->length(); +} + +int64_t ByteBuffer::getLong() { + char * array = &buffer->internalArray()[offset]; + offset+=8; + return (((int64_t)array[0]) << 56) | + (((int64_t)array[1]) << 48) | + (((int64_t)array[2]) << 40) | + (((int64_t)array[3]) << 32) | + (((int64_t)array[4]) << 24) | + (((int64_t)array[5]) << 16) | + (((int64_t)array[6]) << 8) | + (((int64_t)array[7])); +} + +int32_t ByteBuffer::getInt() { + char * array = &buffer->internalArray()[offset]; + offset+=4; + return (((int32_t)array[0]) << 24) | + (((int32_t)array[1]) << 16) | + (((int32_t)array[2]) << 8) | + (((int32_t)array[3])); +} + +char ByteBuffer::get() { + return buffer->get(offset++); +} + +void ByteBuffer::get(Array *array) { + memcpy(array->internalArray(), &buffer->internalArray()[offset], array->length()); + offset += array->length(); +} + +void ByteBuffer::position(int32_t newPosition) { + offset = newPosition; +} + +Array *ByteBuffer::array() { + return buffer; +} + +ByteBuffer *ByteBuffer_wrap(Array *array) { + return new ByteBuffer(array); +} + +ByteBuffer *ByteBuffer_allocate(uint size) { + return new ByteBuffer(new Array(size)); +} diff --git a/version2/src/C/Mac.cc b/version2/src/C/Mac.cc new file mode 100644 index 0000000..65c2c25 --- /dev/null +++ b/version2/src/C/Mac.cc @@ -0,0 +1,19 @@ +#include "Mac.h" +#include "Crypto.h" + +Mac::Mac() { +} + +void Mac::update(Array *array, int32_t offset, int32_t len) { + sha2_hmac_update(&ctx, (const unsigned char *) &array->internalArray()[offset], len); +} + +Array * Mac::doFinal() { + Array * hmac = new Array(32); + sha2_hmac_finish(&ctx, (unsigned char *) hmac->internalArray()); + return hmac; +} + +void Mac::init(AESKey *key) { + sha2_hmac_starts(&ctx, (const unsigned char *) key->getKey()->internalArray(), key->getKey()->length(), false); +} -- 2.34.1