Skip to content

Commit ef3e85c

Browse files
committed
troubleshooting
1 parent 4277d0b commit ef3e85c

5 files changed

Lines changed: 32 additions & 40 deletions

File tree

example/src/hooks/useTestsList.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,6 @@ import '../tests/cipher/cipher_tests';
66
import '../tests/ed25519/ed25519_tests';
77
import '../tests/pbkdf2/pbkdf2_tests';
88
import '../tests/random/random_tests';
9-
// import '../tests/HmacTests/HmacTests';
10-
// import '../tests/HashTests/HashTests';
11-
12-
// import '../tests/CipherTests/CipherTestFirst';
13-
// import '../tests/CipherTests/CipherTestSecond';
14-
// import '../tests/CipherTests/PublicCipherTests';
15-
// import '../tests/CipherTests/test398';
16-
// import '../tests/CipherTests/generateKey';
17-
// import '../tests/CipherTests/GenerateKeyPairTests';
18-
19-
// import '../tests/ConstantsTests/ConstantsTests';
20-
// import '../tests/SignTests/SignTests';
21-
// import '../tests/SmokeTests/bundlerTests';
22-
// import '../tests/webcryptoTests/deriveBits';
23-
// import '../tests/webcryptoTests/digest';
24-
// import '../tests/webcryptoTests/generateKey';
25-
// import '../tests/webcryptoTests/encrypt_decrypt';
26-
// import '../tests/webcryptoTests/import_export';
27-
// import '../tests/webcryptoTests/sign_verify';
289

2910
export const useTestsList = (): [
3011
TestSuites,

example/src/tests/cipher/cipher_tests.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
type CipherType,
88
type BinaryLikeNode,
99
type BinaryLike,
10+
type Cipher,
11+
type Decipher,
1012
} from 'react-native-quick-crypto';
1113
import { expect } from 'chai';
1214
import { test } from '../util';
@@ -52,24 +54,24 @@ test(SUITE, 'cipher - buffers', async () => {
5254
);
5355
});
5456

55-
// update/final
56-
ciphers.forEach(cipherName => {
57-
test(SUITE, `cipher - non-stream - ${cipherName}`, async () => {
58-
roundtrip(cipherName as CipherType, key, iv, plaintext);
59-
});
60-
});
57+
// // update/final
58+
// ciphers.forEach(cipherName => {
59+
// test(SUITE, `cipher - non-stream - ${cipherName}`, async () => {
60+
// roundtrip(cipherName as CipherType, key, iv, plaintext);
61+
// });
62+
// });
6163

6264
function roundtrip(
6365
cipherName: CipherType,
6466
lKey: BinaryLikeNode,
6567
lIv: BinaryLike,
6668
payload: string,
6769
) {
68-
const cipher = createCipheriv(cipherName, lKey, lIv, {});
69-
let ciph = cipher.update(payload, 'utf8', 'buffer') as Uint8Array;
70-
ciph = Buffer.concat([ciph, cipher.final('buffer') as Uint8Array]);
70+
const cipher: Cipher = createCipheriv(cipherName, lKey, lIv, {});
71+
let ciph = cipher.update(payload, 'utf8', 'buffer');
72+
ciph = Buffer.concat([ciph, cipher.final('buffer')]);
7173

72-
const decipher = createDecipheriv(cipherName, lKey, lIv, {});
74+
const decipher: Decipher = createDecipheriv(cipherName, lKey, lIv, {});
7375
let deciph = decipher.update(ciph, 'buffer', 'utf8');
7476
deciph += decipher.final('utf8') as string;
7577
expect(deciph).to.equal(plaintext);

packages/react-native-quick-crypto/cpp/cipher/HybridCipher.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <memory>
22
#include <string>
33
#include <vector>
4+
5+
#include <openssl/err.h>
46
#include <openssl/evp.h>
57

68
#include "HybridCipher.hpp"
@@ -109,7 +111,8 @@ HybridCipher::final() {
109111
ctx,
110112
tempBuf,
111113
&finalLen) != 1) {
112-
throw std::runtime_error("Failed to finalize cipher");
114+
throw std::runtime_error("Failed to finalize cipher: " +
115+
std::to_string(ERR_get_error()));
113116
}
114117

115118
// Create and return a new buffer of exact size needed

packages/react-native-quick-crypto/cpp/random/HybridRandom.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ HybridRandom::randomFillSync(const std::shared_ptr<ArrayBuffer>& buffer,
5050
size_t offset = checkOffset(dSize, dOffset);
5151
uint8_t* data = buffer.get()->data();
5252
if (RAND_bytes(data + offset, (int)size) != 1) {
53-
throw std::runtime_error("error calling RAND_bytes" +
53+
throw std::runtime_error("error calling RAND_bytes: " +
5454
std::to_string(ERR_get_error()));
5555
}
5656
return buffer;

packages/react-native-quick-crypto/src/cipher.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class CipherCommon extends Stream.Transform {
7878
data: BinaryLike,
7979
inputEncoding?: Encoding,
8080
outputEncoding?: Encoding,
81-
): ArrayBuffer | string {
81+
): Buffer | string {
8282
const defaultEncoding = getDefaultEncoding();
8383
inputEncoding = inputEncoding ?? defaultEncoding;
8484
outputEncoding = outputEncoding ?? defaultEncoding;
@@ -98,12 +98,12 @@ class CipherCommon extends Stream.Transform {
9898
return this.decoder!.write(SBuffer.from(ret) as any);
9999
}
100100

101-
return ret;
101+
return Buffer.from(ret);
102102
}
103103

104-
final(): ArrayBuffer;
104+
final(): Buffer;
105105
final(outputEncoding: BufferEncoding | 'buffer'): string;
106-
final(outputEncoding?: BufferEncoding | 'buffer'): ArrayBuffer | string {
106+
final(outputEncoding?: BufferEncoding | 'buffer'): Buffer | string {
107107
const ret = this.native.final();
108108

109109
if (outputEncoding && outputEncoding !== 'buffer') {
@@ -112,7 +112,7 @@ class CipherCommon extends Stream.Transform {
112112
return this.decoder!.end(SBuffer.from(ret) as any);
113113
}
114114

115-
return ret;
115+
return Buffer.from(ret);
116116
}
117117

118118
_transform(
@@ -184,6 +184,8 @@ class Cipheriv extends CipherCommon {
184184
}
185185
}
186186

187+
type Cipher = CipherCCM | CipherOCB | CipherGCM | Cipheriv;
188+
187189
class Decipheriv extends CipherCommon {
188190
constructor(
189191
cipherType: string,
@@ -201,6 +203,8 @@ class Decipheriv extends CipherCommon {
201203
}
202204
}
203205

206+
type Decipher = DecipherCCM | DecipherOCB | DecipherGCM | Decipheriv;
207+
204208
export function createDecipheriv(
205209
algorithm: CipherCCMTypes,
206210
key: BinaryLikeNode,
@@ -224,7 +228,7 @@ export function createDecipheriv(
224228
key: BinaryLikeNode,
225229
iv: BinaryLike,
226230
options?: Stream.TransformOptions,
227-
): DecipherCCM | DecipherOCB | DecipherGCM | Decipheriv;
231+
): Decipher;
228232
export function createDecipheriv(
229233
algorithm: string,
230234
key: BinaryLikeNode,
@@ -234,7 +238,7 @@ export function createDecipheriv(
234238
| CipherOCBOptions
235239
| CipherGCMOptions
236240
| Stream.TransformOptions,
237-
): DecipherCCM | DecipherOCB | DecipherGCM | Decipheriv {
241+
): Decipher {
238242
return new Decipheriv(
239243
algorithm,
240244
key,
@@ -266,7 +270,7 @@ export function createCipheriv(
266270
key: BinaryLikeNode,
267271
iv: BinaryLike,
268272
options?: Stream.TransformOptions,
269-
): CipherCCM | CipherOCB | CipherGCM | Cipheriv;
273+
): Cipher;
270274
export function createCipheriv(
271275
algorithm: string,
272276
key: BinaryLikeNode,
@@ -276,7 +280,7 @@ export function createCipheriv(
276280
| CipherOCBOptions
277281
| CipherGCMOptions
278282
| Stream.TransformOptions,
279-
): CipherCCM | CipherOCB | CipherGCM | Cipheriv {
283+
): Cipher {
280284
return new Cipheriv(
281285
algorithm,
282286
key,
@@ -290,3 +294,5 @@ export const cipherExports = {
290294
createDecipheriv,
291295
getCiphers,
292296
};
297+
298+
export type { Cipher, Decipher };

0 commit comments

Comments
 (0)