forked from margelo/react-native-quick-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHybridRandom.cpp
More file actions
48 lines (40 loc) · 1.65 KB
/
HybridRandom.cpp
File metadata and controls
48 lines (40 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <openssl/err.h>
#include <openssl/rand.h>
#include "HybridRandom.hpp"
#include "QuickCryptoUtils.hpp"
namespace margelo::nitro::crypto {
size_t checkSize(double size) {
if (!CheckIsUint32(size)) {
throw std::runtime_error("size must be uint32");
}
if (static_cast<uint32_t>(size) > pow(2, 31) - 1) {
throw std::runtime_error("size must be less than 2^31 - 1");
}
return static_cast<size_t>(size);
}
size_t checkOffset(double size, double offset) {
if (!CheckIsUint32(offset)) {
throw std::runtime_error("offset must be uint32");
}
if (offset > size) {
throw std::runtime_error("offset must be less than size");
}
return static_cast<size_t>(offset);
}
std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>> HybridRandom::randomFill(const std::shared_ptr<ArrayBuffer>& buffer, double dOffset,
double dSize) {
// get owned NativeArrayBuffer before passing to sync function
auto nativeBuffer = ToNativeArrayBuffer(buffer);
return Promise<std::shared_ptr<ArrayBuffer>>::async(
[this, nativeBuffer, dOffset, dSize]() { return this->randomFillSync(nativeBuffer, dOffset, dSize); });
};
std::shared_ptr<ArrayBuffer> HybridRandom::randomFillSync(const std::shared_ptr<ArrayBuffer>& buffer, double dOffset, double dSize) {
size_t size = checkSize(dSize);
size_t offset = checkOffset(dSize, dOffset);
uint8_t* data = buffer.get()->data();
if (RAND_bytes(data + offset, (int)size) != 1) {
throw std::runtime_error("error calling RAND_bytes: " + std::to_string(ERR_get_error()));
}
return buffer;
};
} // namespace margelo::nitro::crypto