Skip to content

Commit 00b2d7a

Browse files
committed
simplify decoding, 108 pass, 23 fail
1 parent 1cc3092 commit 00b2d7a

6 files changed

Lines changed: 15 additions & 28 deletions

File tree

bun.lockb

-32 Bytes
Binary file not shown.

example/ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1940,7 +1940,7 @@ SPEC CHECKSUMS:
19401940
fmt: 10c6e61f4be25dc963c36bd73fc7b1705fe975be
19411941
glog: 08b301085f15bcbb6ff8632a8ebaf239aae04e6a
19421942
hermes-engine: 46f1ffbf0297f4298862068dd4c274d4ac17a1fd
1943-
NitroModules: c36d6f656038a56beb1b1bcab2d0252d71744013
1943+
NitroModules: 3a58d9bc70815a0d5de4476ed6a36eff05a6a0ae
19441944
OpenSSL-Universal: b60a3702c9fea8b3145549d421fdb018e53ab7b4
19451945
QuickCrypto: 11878b44cfc77fad2ea8f387a16e315841651305
19461946
RCT-Folly: 84578c8756030547307e4572ab1947de1685c599

example/src/tests/cipher/cipher_tests.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,36 @@ import { test } from '../util';
1414

1515
const SUITE = 'cipher';
1616
const ciphers = getCiphers();
17-
const key = 'secret';
17+
const key = randomFillSync(new Uint8Array(32));
1818
const iv = randomFillSync(new Uint8Array(16));
1919
const plaintext =
2020
'32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' +
2121
'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJ' +
2222
'jAfaFg**';
2323

24-
test(SUITE, 'cipher - valid algorithm', async () => {
24+
test(SUITE, 'valid algorithm', async () => {
2525
expect(() => {
2626
createCipheriv('aes-128-cbc', key, iv, {});
2727
}).to.not.throw();
2828
});
2929

30-
test(SUITE, 'cipher - invalid algorithm', async () => {
30+
test(SUITE, 'invalid algorithm', async () => {
3131
expect(() => {
3232
createCipheriv('aes-128-boorad', key, iv, {});
3333
}).to.throw(/Invalid Cipher Algorithm: aes-128-boorad/);
3434
});
3535

36-
test(SUITE, 'cipher - getSupportedCiphers', async () => {
36+
test(SUITE, 'getSupportedCiphers', async () => {
3737
expect(ciphers).to.be.instanceOf(Array);
3838
expect(ciphers).to.have.length.greaterThan(0);
3939
});
4040

4141
// different value types
42-
test(SUITE, 'cipher - strings', async () => {
42+
test(SUITE, 'strings', async () => {
4343
roundtrip('aes-128-cbc', '0123456789abcd0123456789', '12345678', plaintext);
4444
});
4545

46-
test(SUITE, 'cipher - buffers', async () => {
46+
test(SUITE, 'buffers', async () => {
4747
roundtrip(
4848
'aes-128-cbc',
4949
Buffer.from('0123456789abcd0123456789'),
@@ -54,7 +54,7 @@ test(SUITE, 'cipher - buffers', async () => {
5454

5555
// update/final
5656
ciphers.forEach(cipherName => {
57-
test(SUITE, `cipher - non-stream - ${cipherName}`, async () => {
57+
test(SUITE, `non-stream - ${cipherName}`, async () => {
5858
roundtrip(cipherName, key, iv, plaintext);
5959
});
6060
});

packages/react-native-quick-crypto/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
"events": "3.3.0",
7373
"react-native-quick-base64": "2.1.2",
7474
"readable-stream": "4.5.2",
75-
"string_decoder": "1.3.0",
7675
"util": "0.12.5"
7776
},
7877
"devDependencies": {

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { NitroModules } from 'react-native-nitro-modules';
22
import Stream, { type TransformOptions } from 'readable-stream';
3-
import { StringDecoder } from 'string_decoder';
43
import { Buffer } from '@craftzdog/react-native-buffer';
5-
import { Buffer as SBuffer } from 'safe-buffer';
64
import type {
75
CipherCCMOptions,
86
CipherCCMTypes,
@@ -12,10 +10,9 @@ import type {
1210
CipherOCBTypes,
1311
} from 'crypto'; // @types/node
1412
import type { Cipher as NativeCipher } from './specs/cipher.nitro';
15-
import { binaryLikeToArrayBuffer } from './utils';
13+
import { ab2str, binaryLikeToArrayBuffer } from './utils';
1614
import type { BinaryLike, BinaryLikeNode, Encoding } from './utils';
1715
import {
18-
getDecoder,
1916
getDefaultEncoding,
2017
getUIntOption,
2118
normalizeEncoding,
@@ -44,7 +41,6 @@ interface CipherArgs {
4441

4542
class CipherCommon extends Stream.Transform {
4643
private native: NativeCipher;
47-
private decoder: StringDecoder | undefined;
4844

4945
constructor({
5046
isCipher,
@@ -83,13 +79,12 @@ class CipherCommon extends Stream.Transform {
8379
throw new Error('Invalid data argument');
8480
}
8581

86-
data = binaryLikeToArrayBuffer(data, inputEncoding);
87-
const ret = this.native.update(data);
82+
const ret = this.native.update(
83+
binaryLikeToArrayBuffer(data, inputEncoding),
84+
);
8885

8986
if (outputEncoding && outputEncoding !== 'buffer') {
90-
this.decoder = getDecoder(this.decoder, outputEncoding);
91-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
92-
return this.decoder!.write(SBuffer.from(ret) as any);
87+
return ab2str(ret, outputEncoding);
9388
}
9489

9590
return Buffer.from(ret);
@@ -101,9 +96,7 @@ class CipherCommon extends Stream.Transform {
10196
const ret = this.native.final();
10297

10398
if (outputEncoding && outputEncoding !== 'buffer') {
104-
this.decoder = getDecoder(this.decoder, outputEncoding);
105-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
106-
return this.decoder!.end(SBuffer.from(ret) as any);
99+
return ab2str(ret, outputEncoding);
107100
}
108101

109102
return Buffer.from(ret);
@@ -289,4 +282,4 @@ export const cipherExports = {
289282
getCiphers,
290283
};
291284

292-
export type { Cipher, Decipher };
285+
export type { Cipher, Decipher };

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { StringDecoder } from 'string_decoder';
21
import type { Encoding } from './types';
32

43
// Mimics node behavior for default global encoding
@@ -49,10 +48,6 @@ export function validateEncoding(data: string, encoding: string) {
4948
}
5049
}
5150

52-
export function getDecoder(decoder?: StringDecoder, encoding?: BufferEncoding) {
53-
return decoder ?? new StringDecoder(encoding);
54-
}
55-
5651
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5752
export function getUIntOption(options: Record<string, any>, key: string) {
5853
let value;

0 commit comments

Comments
 (0)