Skip to content

Commit 6838125

Browse files
committed
adding more methods
1 parent 5ecbb98 commit 6838125

File tree

5 files changed

+1048
-139
lines changed

5 files changed

+1048
-139
lines changed

README.md

Lines changed: 253 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,274 @@
1-
# react-native-encryption
1+
# 📚 **React Native Integration Guide for `rn-encryption` Library**
22

3-
Encryption
3+
This guide explains how to directly access methods from the `rn-encryption` library in a **React Native project**, including usage examples for AES, RSA, Hashing, HMAC, Random String, and Base64 utilities.
44

5-
## Installation
5+
---
6+
7+
## 📑 **Table of Contents**
8+
9+
1. [Library Installation](#1-library-installation)
10+
2. [New Architecture required](#2-new_arch_needed)
11+
3. [Setup in React Native](#3-setup-in-react-native)
12+
4. [Direct Method Import](#4-direct-method-import)
13+
5. [API Overview](#5-api-overview)
14+
6. [Usage Examples](#6-usage-examples)
15+
7. [Troubleshooting](#7-troubleshooting)
16+
8. [Best Practices](#8-best-practices)
17+
9. [FAQ](#9-faq)
18+
19+
---
20+
21+
## 🚀 **1. Library Installation**
622

7-
```sh
8-
npm install rn-encryption
23+
### 1.1 **Add Dependency**
24+
25+
Install the library using npm or yarn:
26+
27+
```bash
28+
npm install rn-encryption --save
29+
# OR
930
yarn add rn-encryption
1031
```
32+
### #2-new_arch_needed
33+
34+
New architecture required. React native >= 0.76.5 Works with Expo **Bare Workflow** & **Vanilla React Native**
35+
36+
### 1.2 **Rebuild the Project**
37+
38+
For Android:
39+
```bash
40+
cd android && ./gradlew clean && cd ..
41+
npx react-native run-android
42+
```
43+
44+
For iOS:
45+
```bash
46+
cd ios && pod install && cd ..
47+
npx react-native run-ios
48+
```
49+
50+
---
51+
52+
## ⚙️ **2. Setup in React Native**
53+
54+
No additional configuration is required. The methods can be **directly imported** and used.
55+
56+
---
57+
58+
## 📦 **3. Direct Method Import**
59+
60+
You can directly import the methods you need:
61+
62+
```tsx
63+
import {
64+
encryptAES,
65+
decryptAES,
66+
encryptRSA,
67+
decryptRSA,
68+
hashSHA256,
69+
hashSHA512,
70+
hmacSHA256,
71+
generateRandomString,
72+
base64Encode,
73+
base64Decode,
74+
} from 'rn-encryption';
75+
```
76+
77+
Each method can be accessed directly without a default object wrapper.
78+
79+
---
80+
81+
## 📚 **4. API Overview**
82+
83+
### 🔒 **AES Encryption/Decryption**
84+
- **`encryptAES(data: string, key: string): string`**
85+
- **`decryptAES(data: string, key: string): string`**
86+
87+
### 🔑 **RSA Encryption/Decryption**
88+
- **`encryptRSA(data: string, publicKey: string): string`**
89+
- **`decryptRSA(data: string, privateKey: string): string`**
90+
91+
### 🛡️ **SHA Hashing**
92+
- **`hashSHA256(input: string): string`**
93+
- **`hashSHA512(input: string): string`**
94+
95+
### 📝 **HMAC-SHA256**
96+
- **`hmacSHA256(data: string, key: string): string`**
97+
98+
### 🎲 **Random String Generation**
99+
- **`generateRandomString(input: number): string`**
100+
101+
### 📝 **Base64 Encoding/Decoding**
102+
- **`base64Encode(input: string): string`**
103+
- **`base64Decode(input: string): string`**
104+
105+
---
106+
107+
## 🛠️ **5. Usage Examples**
108+
109+
### 🔒 **5.1 AES Encryption and Decryption**
110+
111+
```tsx
112+
import { encryptAES, decryptAES } from 'rn-encryption';
113+
114+
const runAESExample = async () => {
115+
try {
116+
const plainText = "Hello, AES Encryption!";
117+
const key = "1234567890123456"; // Must be 16, 24, or 32 characters
118+
119+
const encryptedText = encryptAES(plainText, key);
120+
console.log("AES Encrypted Text:", encryptedText);
121+
122+
const decryptedText = decryptAES(encryptedText, key);
123+
console.log("AES Decrypted Text:", decryptedText);
124+
} catch (error) {
125+
console.error("AES Error:", error);
126+
}
127+
};
128+
129+
runAESExample();
130+
```
131+
132+
---
133+
134+
### 🔑 **5.2 RSA Encryption and Decryption**
135+
136+
```tsx
137+
import { encryptRSA, decryptRSA } from 'rn-encryption';
138+
139+
const runRSAExample = async () => {
140+
try {
141+
const plainText = "Hello, RSA Encryption!";
142+
const publicKey = "YOUR_RSA_PUBLIC_KEY";
143+
const privateKey = "YOUR_RSA_PRIVATE_KEY";
144+
145+
const encryptedText = encryptRSA(plainText, publicKey);
146+
console.log("RSA Encrypted Text:", encryptedText);
147+
148+
const decryptedText = decryptRSA(encryptedText, privateKey);
149+
console.log("RSA Decrypted Text:", decryptedText);
150+
} catch (error) {
151+
console.error("RSA Error:", error);
152+
}
153+
};
154+
155+
runRSAExample();
156+
```
157+
158+
---
159+
160+
### 🛡️ **5.3 SHA Hashing**
161+
162+
```tsx
163+
import { hashSHA256, hashSHA512 } from 'rn-encryption';
164+
165+
const runHashExample = async () => {
166+
const data = "Hash this string";
167+
168+
const sha256 = hashSHA256(data);
169+
console.log("SHA-256 Hash:", sha256);
170+
171+
const sha512 = hashSHA512(data);
172+
console.log("SHA-512 Hash:", sha512);
173+
};
174+
175+
runHashExample();
176+
```
177+
178+
---
179+
180+
### 📝 **5.4 HMAC-SHA256**
181+
182+
```tsx
183+
import { hmacSHA256 } from 'rn-encryption';
184+
185+
const runHMACExample = async () => {
186+
const message = "Authenticate this";
187+
const secretKey = "SecretKey";
188+
189+
const hmac = hmacSHA256(message, secretKey);
190+
console.log("HMAC-SHA256:", hmac);
191+
};
192+
193+
runHMACExample();
194+
```
195+
196+
---
197+
198+
### 🎲 **5.5 Random String Generation**
199+
200+
```tsx
201+
import { generateRandomString } from 'rn-encryption';
202+
203+
const runRandomStringExample = async () => {
204+
const randomString = generateRandomString(16);
205+
console.log("Random String:", randomString);
206+
};
207+
208+
runRandomStringExample();
209+
```
11210

12-
## Usage
211+
---
13212

213+
### 📝 **5.6 Base64 Encoding/Decoding**
14214

15-
```js
16-
import {encrypt, decrypt} from "rn-encryption"
215+
```tsx
216+
import { base64Encode, base64Decode } from 'rn-encryption';
17217

18-
// ...
19-
const encryptionKey = "1234567890123456";
218+
const runBase64Example = () => {
219+
const plainText = "Base64 this text";
20220

21-
const handleEncryption = async () => {
22-
23-
try {
24-
const encrypted = await encrypt("sometexthere", encryptionKey);
221+
const encoded = base64Encode(plainText);
222+
console.log("Base64 Encoded:", encoded);
25223

26-
const decrypted = await decrypt(encrypted, encryptionKey);
224+
const decoded = base64Decode(encoded);
225+
console.log("Base64 Decoded:", decoded);
226+
};
27227

28-
console.log(`Encrypted: ${encrypted}\nDecrypted: ${decrypted}`);
29-
} catch (err) {
30-
console.log('An error occurred during encryption/decryption.');
31-
}
32-
};
228+
runBase64Example();
33229
```
34230

35-
## I have just started to working on this repo
36-
#I am creating encryption on native side i.e kotlin and objective-C using the turbo modules
231+
---
232+
233+
## 🐞 **6. Troubleshooting**
37234

38-
## Contributing
235+
1. **Library Not Found:**
236+
- Run `npx react-native link rn-encryption`.
237+
- Clean and rebuild the project.
39238

40-
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
239+
2. **AES Key Size Error:**
240+
- Ensure the AES key is **16, 24, or 32 characters**.
41241

42-
## License
242+
3. **RSA Key Parsing Issue:**
243+
- Verify the RSA key is in **Base64-encoded PEM format**.
43244

44-
MIT
245+
4. **Permission Issues:**
246+
- Ensure native permissions are set correctly in **AndroidManifest.xml** or **iOS Podfile**.
45247

46248
---
47249

48-
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
250+
## **7. Best Practices**
251+
252+
1. **Do Not Hardcode Keys:** Use `.env` or secure storage for keys.
253+
2. **Handle Errors Gracefully:** Wrap calls in `try-catch` blocks.
254+
3. **Validate Key Sizes:** Ensure AES and RSA keys meet size requirements.
255+
256+
---
257+
258+
## **8. FAQ**
259+
260+
**Q: Does the library support both Android and iOS?**
261+
A: Partially, `rn-encryption` fully supports ios and encryptAES & decryptAES for Android platforms.
262+
263+
**Q: Can I use the library in Expo?**
264+
A: Yes, if you're using **Expo Bare Workflow**.
265+
266+
**Q: How do I debug encryption issues?**
267+
A: Add console logs and verify that keys and data are correctly passed.
268+
269+
---
270+
271+
## **9. Conclusion**
272+
273+
With **`rn-encryption`**, you can securely handle encryption, hashing, and cryptographic operations in your React Native app. Use the examples above to integrate and test various functionalities.
274+

0 commit comments

Comments
 (0)