Skip to content

Commit 097b89a

Browse files
committed
refactor
1 parent 51139d9 commit 097b89a

File tree

4 files changed

+195
-134
lines changed

4 files changed

+195
-134
lines changed

README.md

Lines changed: 192 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,12 @@ Each method can be accessed directly without a default object wrapper.
8585
## 📚 **5. API Overview**
8686

8787
### 🔒 **AES Encryption/Decryption**
88+
- **`generateAESKey(keySize: number): string`**
8889
- **`encryptAES(data: string, key: string): string`**
8990
- **`decryptAES(data: string, key: string): string`**
9091

9192
### 🔑 **RSA Encryption/Decryption**
93+
- **`generateRSAKeyPair(): keypair`**
9294
- **`encryptRSA(data: string, publicKey: string): string`**
9395
- **`decryptRSA(data: string, privateKey: string): string`**
9496

@@ -106,130 +108,204 @@ Each method can be accessed directly without a default object wrapper.
106108
- **`base64Encode(input: string): string`**
107109
- **`base64Decode(input: string): string`**
108110

109-
---
110-
111-
## 🛠️ **6. Usage Examples**
112-
113-
### 🔒 **6.1 AES Encryption and Decryption**
114-
115-
```tsx
116-
import { encryptAES, decryptAES } from 'rn-encryption';
117-
118-
const runAESExample = async () => {
119-
try {
120-
const plainText = "Hello, AES Encryption!";
121-
const key = "1234567890123456"; // Must be 16, 24, or 32 characters
122-
123-
const encryptedText = encryptAES(plainText, key);
124-
console.log("AES Encrypted Text:", encryptedText);
125-
126-
const decryptedText = decryptAES(encryptedText, key);
127-
console.log("AES Decrypted Text:", decryptedText);
128-
} catch (error) {
129-
console.error("AES Error:", error);
130-
}
131-
};
132-
133-
runAESExample();
134-
```
111+
### 🔒 **ECDA Encryption/Decryption**
112+
- **`generateECDSAKeyPair(): keypair`**
113+
- **`signDataECDSA(data: string, key: string): string`**
114+
- **`verifySignatureECDSA(data: string,signatureBase64: string, key: string): boolean`**
135115

136116
---
137117

138-
### 🔑 **6.2 RSA Encryption and Decryption**
118+
## 🛠️ **6. Usage Examples**
139119

140120
```tsx
141-
import { encryptRSA, decryptRSA } from 'rn-encryption';
142-
143-
const runRSAExample = async () => {
144-
try {
145-
const plainText = "Hello, RSA Encryption!";
146-
const publicKey = "YOUR_RSA_PUBLIC_KEY";
147-
const privateKey = "YOUR_RSA_PRIVATE_KEY";
148-
149-
const encryptedText = encryptRSA(plainText, publicKey);
150-
console.log("RSA Encrypted Text:", encryptedText);
151-
152-
const decryptedText = decryptRSA(encryptedText, privateKey);
153-
console.log("RSA Decrypted Text:", decryptedText);
154-
} catch (error) {
155-
console.error("RSA Error:", error);
121+
import { useState } from 'react';
122+
import { View, StyleSheet, Text, Button } from 'react-native';
123+
import {
124+
encryptAES,
125+
decryptAES,
126+
encryptRSA,
127+
decryptRSA,
128+
hashSHA256,
129+
hashSHA512,
130+
hmacSHA256,
131+
base64Encode,
132+
base64Decode,
133+
generateRandomString,
134+
generateAESKey,
135+
generateRSAKeyPair,
136+
generateECDSAKeyPair,
137+
signDataECDSA,
138+
verifySignatureECDSA,
139+
} from 'rn-encryption';
140+
interface EncryptionError {
141+
name: string;
142+
message: string;
143+
}
144+
export default function DashboardScreen() {
145+
const [result, setResult] = useState(''); // Encryption/Decryption result
146+
147+
async function handleRSAEncryption() {
148+
const plaintext = 'Hello, RSA Encryption!';
149+
const generatedKeys = generateRSAKeyPair();
150+
try {
151+
// Step 1: Encrypt the plaintext using the Public Key
152+
const encryptedData = await encryptRSA(
153+
plaintext,
154+
generatedKeys.publicKey
155+
);
156+
// Step 2: Decrypt the encrypted data using the Private Key
157+
const decryptedData = await decryptRSA(
158+
encryptedData,
159+
generatedKeys.privateKey
160+
);
161+
// Step 3: Validation
162+
if (decryptedData === plaintext) {
163+
console.log('✅ RSA Encryption and Decryption Successful!');
164+
} else {
165+
console.error('❌ Decrypted data does not match original plaintext!');
166+
}
167+
} catch (error) {
168+
console.error('⚠️ RSA Error:', error);
169+
}
156170
}
157-
};
158-
159-
runRSAExample();
160-
```
161-
162-
---
163-
164-
### 🛡️ **6.3 SHA Hashing**
165-
166-
```tsx
167-
import { hashSHA256, hashSHA512 } from 'rn-encryption';
168-
169-
const runHashExample = async () => {
170-
const data = "Hash this string";
171-
172-
const sha256 = hashSHA256(data);
173-
console.log("SHA-256 Hash:", sha256);
174-
175-
const sha512 = hashSHA512(data);
176-
console.log("SHA-512 Hash:", sha512);
177-
};
178-
179-
runHashExample();
180-
```
181-
182-
---
183-
184-
### 📝 **6.4 HMAC-SHA256**
185-
186-
```tsx
187-
import { hmacSHA256 } from 'rn-encryption';
188-
189-
const runHMACExample = async () => {
190-
const message = "Authenticate this";
191-
const secretKey = "SecretKey";
192-
193-
const hmac = hmacSHA256(message, secretKey);
194-
console.log("HMAC-SHA256:", hmac);
195-
};
196-
197-
runHMACExample();
198-
```
199-
200-
---
201-
202-
### 🎲 **6.5 Random String Generation**
203-
204-
```tsx
205-
import { generateRandomString } from 'rn-encryption';
206-
207-
const runRandomStringExample = async () => {
208-
const randomString = generateRandomString(16);
209-
console.log("Random String:", randomString);
210-
};
211-
212-
runRandomStringExample();
213-
```
214-
215-
---
216-
217-
### 📝 **6.6 Base64 Encoding/Decoding**
218-
219-
```tsx
220-
import { base64Encode, base64Decode } from 'rn-encryption';
221-
222-
const runBase64Example = () => {
223-
const plainText = "Base64 this text";
224-
225-
const encoded = base64Encode(plainText);
226-
console.log("Base64 Encoded:", encoded);
227171

228-
const decoded = base64Decode(encoded);
229-
console.log("Base64 Decoded:", decoded);
230-
};
172+
const handleAESEncryption = async () => {
173+
const sampleObject = {
174+
name: 'John Doe',
175+
age: 30,
176+
roles: ['admin', 'editor'],
177+
};
178+
try {
179+
const generatedKey = generateAESKey(256);
180+
const jsonString = JSON.stringify(sampleObject);
181+
const encryptedString = encryptAES(jsonString, generatedKey);
182+
183+
// Decrypt and parse JSON
184+
const decryptedJsonString = decryptAES(encryptedString, generatedKey);
185+
const decryptedObject = JSON.parse(decryptedJsonString);
186+
console.log('Decrypted Object:', decryptedObject);
187+
} catch (err: unknown) {
188+
if (err instanceof Error) {
189+
let error = err.cause as EncryptionError;
190+
console.log('❌ Error:123', error.message);
191+
} else {
192+
console.log('❌ Unknown Error:', err);
193+
}
194+
setResult('An error occurred during encryption/decryption.');
195+
}
196+
};
197+
198+
const hashing = async () => {
199+
try {
200+
console.log('--- Hashing ---');
201+
const sha256Hash = hashSHA256('Hello Hashing');
202+
console.log('SHA-256 Hash:', sha256Hash);
203+
204+
const sha512Hash = hashSHA512('Hello Hashing');
205+
console.log('SHA-512 Hash:', sha512Hash);
206+
} catch (err) {
207+
console.log('error is', err);
208+
}
209+
};
210+
211+
const hmac = async () => {
212+
try {
213+
console.log('--- HMAC ---');
214+
const hmac = hmacSHA256('Hello HMAC', 'MyHMACKey');
215+
console.log('HMAC-SHA256:', hmac);
216+
} catch (err) {
217+
console.log('error is', err);
218+
}
219+
};
220+
221+
const signData = async () => {
222+
const keyPair = generateECDSAKeyPair();
223+
const data = 'Hello, ECDSA!';
224+
const signature = signDataECDSA(data, keyPair.privateKey);
225+
const isValid = verifySignatureECDSA(data, signature, keyPair.publicKey);
226+
227+
console.log('Signature:', signature);
228+
console.log('Is Valid Signature:', isValid);
229+
};
230+
231+
const base64 = async () => {
232+
try {
233+
console.log('--- Base64 Encoding/Decoding ---');
234+
const base64Encoded = base64Encode('Hello Base64 Encoding');
235+
console.log('Base64 Encoded:', base64Encoded);
236+
237+
const base64Decoded = base64Decode(base64Encoded);
238+
console.log('Base64 Decoded:', base64Decoded);
239+
} catch (err) {
240+
console.log('error is', err);
241+
}
242+
};
243+
244+
const createRandomString = async () => {
245+
try {
246+
console.log('--- Utilities ---');
247+
const randomString = generateRandomString(16);
248+
console.log('Random String:', randomString);
249+
} catch (err) {
250+
console.log('error is', err);
251+
}
252+
};
253+
254+
return (
255+
<View style={{ flex: 1, alignItems: 'center', paddingTop: 80 }}>
256+
<Button title="Encrypt & Decrypt AES" onPress={handleAESEncryption} />
257+
258+
<Button title="Encrypt & Decrypt RSA" onPress={handleRSAEncryption} />
259+
260+
<Button title="Hashing" onPress={hashing} />
261+
262+
<Button title="HMAC" onPress={hmac} />
263+
264+
<Button title="Base64 Encoding" onPress={base64} />
265+
266+
<Button title="Generate random" onPress={createRandomString} />
267+
268+
<Button title="Sign & Validate data" onPress={signData} />
269+
270+
<Text style={styles.resultText}>{result}</Text>
271+
</View>
272+
);
273+
}
274+
275+
const styles = StyleSheet.create({
276+
inputContainer: {
277+
marginVertical: 20,
278+
alignItems: 'center',
279+
width: '80%',
280+
},
281+
textInput: {
282+
borderWidth: 1,
283+
borderColor: '#ccc',
284+
borderRadius: 5,
285+
padding: 10,
286+
width: '100%',
287+
marginTop: 10,
288+
},
289+
resultText: {
290+
marginVertical: 20,
291+
textAlign: 'center',
292+
fontSize: 16,
293+
},
294+
counterWrapper: {
295+
height: 150,
296+
justifyContent: 'center',
297+
alignItems: 'center',
298+
},
299+
counterView: {
300+
width: 280,
301+
height: 140,
302+
},
303+
text: {
304+
marginBottom: 20,
305+
fontSize: 16,
306+
},
307+
});
231308

232-
runBase64Example();
233309
```
234310

235311
---

example/src/App.tsx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,18 @@ export default function DashboardScreen() {
2626

2727
async function handleRSAEncryption() {
2828
const plaintext = 'Hello, RSA Encryption!';
29-
30-
// Base64-encoded RSA Keys (as strings)
31-
3229
const generatedKeys = generateRSAKeyPair();
33-
//console.log("RSA key pair", generatedKeys)
34-
3530
try {
3631
// Step 1: Encrypt the plaintext using the Public Key
3732
const encryptedData = await encryptRSA(
3833
plaintext,
3934
generatedKeys.publicKey
4035
);
41-
console.log('🔐 Encrypted Data (Base64):', encryptedData);
42-
4336
// Step 2: Decrypt the encrypted data using the Private Key
4437
const decryptedData = await decryptRSA(
4538
encryptedData,
4639
generatedKeys.privateKey
4740
);
48-
console.log('🔓 Decrypted Data:', decryptedData);
49-
5041
// Step 3: Validation
5142
if (decryptedData === plaintext) {
5243
console.log('✅ RSA Encryption and Decryption Successful!');
@@ -66,7 +57,6 @@ export default function DashboardScreen() {
6657
};
6758
try {
6859
const generatedKey = generateAESKey(256);
69-
console.log('generatedKey:', generatedKey);
7060
const jsonString = JSON.stringify(sampleObject);
7161
const encryptedString = encryptAES(jsonString, generatedKey);
7262

src/NativeEncryption.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import type { TurboModule } from 'react-native';
22
import { TurboModuleRegistry } from 'react-native';
3-
4-
interface keypair {
3+
export interface keypair {
54
publicKey: string;
65
privateKey: string;
76
}
8-
97
export interface Spec extends TurboModule {
108
generateAESKey(keySize: number): string;
119
encryptAES(data: string, key: string): string;

src/index.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import Encryption from './NativeEncryption';
2-
interface keypair {
3-
publicKey: string;
4-
privateKey: string;
5-
}
1+
import Encryption, { type keypair } from './NativeEncryption';
2+
63
export function generateAESKey(input: number): string {
74
return Encryption.generateAESKey(input);
85
}

0 commit comments

Comments
 (0)