Skip to content

Commit fa7245c

Browse files
committed
fix: PR feedback and test fix for string args
1 parent 5ad023c commit fa7245c

File tree

2 files changed

+31
-39
lines changed

2 files changed

+31
-39
lines changed

.rules

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,55 @@
11
# React Native Quick Crypto
22

3-
Every time you choose to apply a rule(s), explicitly state the rule(s) in the output You can abbreviate the rule description to a single word or phrase.
3+
Every time you choose to apply a rule(s), explicitly state the rule(s) in the output. You can abbreviate the rule description to a single word or phrase.
44

55
## Project Context
6-
- This is a react native project that offers cryptographic operations in native
7-
code.
6+
7+
- This is a React Native project that offers cryptographic operations in native code.
88
- It uses Nitro Modules to bridge JS & C++.
9-
- Use the documentation of Nitro Modules if you have access locally to its llms.txt file.
10-
- Part of the API strives to be a polyfill of the Node.js {crypto} module.
9+
- Use the documentation of Nitro Modules if you have access locally to its `llms.txt` file.
10+
- Part of the API strives to be a polyfill of the Node.js `{crypto}` module.
1111
- The goal is to migrate 0.x of this library that uses OpenSSL 1.1.1 to now use OpenSSL 3.3 and modern C++ with Nitro Modules.
1212

1313
## Tech Stack
14+
1415
- React Native
15-
- Typescript
16+
- TypeScript
1617
- Nitro Modules
1718
- C++ 20 and higher, modern
1819
- OpenSSL 3.3 and higher
1920
- TypeScript package manager is `bun` 1.2 or higher
20-
- don't ask to run tests. They have to be run in an example React Native app.
21+
- Don't ask to run tests. They have to be run in an example React Native app.
2122

2223
## Rules
23-
- for C++ includes, do not try to add absolute or relative paths. They have to be resolved by the build system.
24-
- use smart pointers in C++
25-
- use modern C++ features
24+
25+
- For C++ includes, do not try to add absolute or relative paths. They have to be resolved by the build system.
26+
- Use smart pointers in C++.
27+
- Use modern C++ features.
2628
- Attempt to reduce the amount of code rather than add more.
2729
- Prefer iteration and modularization over code duplication.
2830

29-
# TypeScript Best Practices
31+
## TypeScript Best Practices
3032

3133
- Use TypeScript for all code; prefer interfaces over types.
32-
- Use lowercase with dashes for directories (e.g., components/auth-wizard).
34+
- Use lowercase with dashes for directories (e.g., `components/auth-wizard`).
3335
- Favor named exports for components.
34-
- Avoid any and enums; use explicit types and maps instead.
36+
- Avoid `any` and enums; use explicit types and maps instead.
3537
- Use functional components with TypeScript interfaces.
3638
- Enable strict mode in TypeScript for better type safety.
3739
- Suggest the optimal implementation considering:
3840
- Performance impact
3941
- Maintenance overhead
4042
- Testing strategy
41-
- Code examples should follow Typescript best practices.
43+
- Code examples should follow TypeScript best practices.
4244

43-
# React Best Practices
45+
## React Best Practices
4446

45-
- Minimize the use of useEffect. They should be a last resort.
46-
- Use named functions for useEffects with a meaningful function name. Avoiding adding unnecessary comments on effect behavior.
47+
- Minimize the use of `useEffect`. They should be a last resort.
48+
- Use named functions for `useEffect`s with a meaningful function name. Avoid adding unnecessary comments on effect behavior.
4749

48-
# Syntax & Formatting
50+
## Syntax & Formatting
4951

50-
- Use the function keyword for pure functions.
52+
- Use the `function` keyword for pure functions.
5153
- Avoid unnecessary curly braces in conditionals; use concise syntax for simple statements.
5254
- Use declarative JSX.
5355
- Use Prettier for consistent code formatting.

example/src/tests/cipher/cipher_tests.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ function roundTripAuth(
9292
// Helper for non-authenticated modes
9393
function roundTrip(
9494
cipherName: string,
95-
key: Buffer,
96-
iv: Buffer,
95+
key: Buffer | string,
96+
iv: Buffer | string,
9797
plaintext: Buffer,
9898
) {
9999
// Encrypt
@@ -112,13 +112,7 @@ function roundTrip(
112112
expect(decrypted).eql(plaintext); // Use Chai's eql for deep equality
113113
}
114114

115-
// Helper function to generate random data (commented out as it's unused and incomplete)
116-
/* ... existing generateCipherData function ... */
117-
118115
// --- Tests ---
119-
const allCiphers = getCiphers();
120-
// .filter(c => c.includes('SIV'))
121-
// .filter(c => c.includes('CCM') || c.includes('OCB'))
122116
test(SUITE, 'valid algorithm', () => {
123117
expect(() => {
124118
createCipheriv('aes-128-cbc', Buffer.alloc(16), Buffer.alloc(16), {}); // Use alloc
@@ -135,24 +129,24 @@ test(SUITE, 'strings', () => {
135129
// roundtrip expects Buffers, convert strings first
136130
roundTrip(
137131
'aes-128-cbc',
138-
key, // Use globally defined key
139-
iv, // Use globally defined iv
140-
plaintextBuffer, // Use the correct plaintext buffer
132+
key.toString('hex'),
133+
iv.toString('hex'),
134+
plaintextBuffer,
141135
);
142136
});
143137

144138
test(SUITE, 'buffers', () => {
145139
roundTrip(
146140
'aes-128-cbc',
147-
key, // Use globally defined key
148-
iv, // Use globally defined iv
149-
plaintextBuffer, // Use the correct plaintext buffer
141+
key,
142+
iv,
143+
plaintextBuffer,
150144
);
151145
});
152146

153-
// --- Main test dispatcher ---
147+
// loop through each cipher and test roundtrip
148+
const allCiphers = getCiphers();
154149
allCiphers.forEach(cipherName => {
155-
// Define a test for each cipher algorithm
156150
test(SUITE, cipherName, () => {
157151
try {
158152
// Determine correct key length
@@ -162,7 +156,6 @@ allCiphers.forEach(cipherName => {
162156
} else if (cipherName.includes('192')) {
163157
keyLen = 24;
164158
}
165-
// Always use Uint8Array for testKey generation
166159
let testKey: Uint8Array;
167160
if (cipherName.includes('XTS')) {
168161
keyLen *= 2; // XTS requires double length key
@@ -200,14 +193,11 @@ allCiphers.forEach(cipherName => {
200193
cipherName.includes('Poly1305') ||
201194
cipherName.includes('SIV') // SIV modes also use auth
202195
) {
203-
// Pass aad buffer defined globally
204196
roundTripAuth(cipherName, key, iv, plaintextBuffer, aad);
205197
} else {
206198
roundTrip(cipherName, key, iv, plaintextBuffer);
207199
}
208200
} catch (e: unknown) {
209-
// console.error(`Error testing cipher ${cipherName}:`, e.message, e.stack);
210-
// Use Chai's expect to fail the test explicitly on error
211201
const message = e instanceof Error ? e.message : String(e);
212202
expect.fail(`Cipher ${cipherName} threw an error: ${message}`);
213203
}

0 commit comments

Comments
 (0)