Skip to content

Commit

Permalink
troubleshooting deleteFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
boorad committed Sep 18, 2024
1 parent 9571de9 commit f1ccfb4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
6 changes: 5 additions & 1 deletion example/src/testing/tests/random/random_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { Done } from 'mocha';
const { ab2str, abvToArrayBuffer } = crypto;

describe('random', () => {
/*
[crypto.randomBytes, crypto.pseudoRandomBytes].forEach(f => {
// TODO (Szymon)
// [undefined, null, false, true, {}, []].forEach((value) => {
Expand Down Expand Up @@ -210,14 +211,15 @@ describe('random', () => {
'before/after slices',
);
});

*/
it('randomFill - deepStringEqual - Buffer', (done: Done) => {
const buf = Buffer.alloc(10);
const before = buf.toString('hex');

crypto.randomFill(buf, 5, 5, (_err: Error | null, res: Buffer) => {
try {
const after = Buffer.from(res).toString('hex');
console.log('after', after);
assert.notStrictEqual(before, after, 'before/after');
assert.deepStrictEqual(
before.slice(0, 5),
Expand Down Expand Up @@ -250,6 +252,7 @@ describe('random', () => {
});
});

/*
// finish
// describe('errors checks', () => {
// [Buffer.alloc(10), new Uint8Array(new Array(10).fill(0))].forEach((buf) => {
Expand Down Expand Up @@ -625,4 +628,5 @@ describe('random', () => {
done();
});
});
*/
});
14 changes: 9 additions & 5 deletions packages/react-native-quick-crypto/cpp/random/HybridRandom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ std::future<std::shared_ptr<ArrayBuffer>>
HybridRandom::randomFill(const std::shared_ptr<ArrayBuffer>& buffer,
double dOffset,
double dSize) {
size_t size = checkSize(dSize);
size_t bufferSize = buffer.get()->size();
// copy the JSArrayBuffer that we do not own into a NativeArrayBuffer that we
// do own, before passing to sync function
uint8_t* data = new uint8_t[size];
memcpy(data, buffer.get()->data(), size);
std::shared_ptr<ArrayBuffer> nativeBuffer =
std::make_shared<NativeArrayBuffer>(data, size, [=]() { delete[] data; });
uint8_t* data = new uint8_t[bufferSize];
memcpy(data, buffer.get()->data(), bufferSize);
std::shared_ptr<NativeArrayBuffer> nativeBuffer =
// std::make_shared<NativeArrayBuffer>(data, bufferSize, nullptr);
std::make_shared<NativeArrayBuffer>(data, bufferSize, [=]() { delete[] data; });

return std::async(std::launch::async,
[this, nativeBuffer, dOffset, dSize]() {
Expand All @@ -27,14 +28,17 @@ std::shared_ptr<ArrayBuffer>
HybridRandom::randomFillSync(const std::shared_ptr<ArrayBuffer>& buffer,
double dOffset,
double dSize) {
// size_t bufferSize = buffer.get()->size();
size_t size = checkSize(dSize);
size_t offset = checkOffset(dSize, dOffset);
uint8_t* data = buffer.get()->data();

// printData(0, data, bufferSize);
if (RAND_bytes(data + offset, (int)size) != 1) {
throw std::runtime_error("error calling RAND_bytes" +
std::to_string(ERR_get_error()));
}
// printData(1, data, bufferSize);
return std::make_shared<NativeArrayBuffer>(data, size, nullptr);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ inline size_t checkOffset(double size, double offset) {
return static_cast<size_t>(offset);
}

inline void printData(int num, uint8_t* data, size_t size) {
printf("data %d - ", num);
for (size_t i = 0; i < size; i++) {
printf("%u ", data[i]);
}
printf("\n");
}

} // namespace margelo::nitro::crypto
3 changes: 2 additions & 1 deletion packages/react-native-quick-crypto/src/random.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Buffer } from '@craftzdog/react-native-buffer';
import type { ArrayBufferView, RandomCallback } from './utils';
import { abvToArrayBuffer } from './utils';
import { ab2str, abvToArrayBuffer } from './utils';
import { NitroModules } from 'react-native-nitro-modules';
import type { Random } from './specs/random.nitro';

Expand Down Expand Up @@ -57,6 +57,7 @@ export function randomFill(buffer: ArrayBufferView, ...rest: unknown[]): void {
getNative();
random.randomFill(abvToArrayBuffer(buffer), offset, size).then(
(res: ArrayBuffer) => {
console.log('res', ab2str(res));
callback(null, res);
},
(e: Error) => {
Expand Down

0 comments on commit f1ccfb4

Please sign in to comment.