Skip to content

Commit c11499d

Browse files
author
LamNguyen176
committed
first commit
1 parent 83c3668 commit c11499d

28 files changed

+1424
-201
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,11 @@ lib/
8080
# React Native Codegen
8181
ios/generated
8282
android/generated
83+
84+
# Example
85+
example/ios/.xcode.env
86+
example/ios/build
87+
example/ios/Podfile.lock
88+
89+
yarn.lock
90+
.yarn/*

CODE_OF_CONDUCT.md

Lines changed: 0 additions & 133 deletions
This file was deleted.

README.md

Lines changed: 133 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,155 @@
11
# react-native-crypto-algorithm
22

3-
Native Module using Kotlin & Swift for React-Native
3+
[![](https://img.shields.io/badge/yarn-v1.0.0-blue)](https://www.npmjs.com/package/react-native-encryption-algorithm)
4+
[![](https://img.shields.io/badge/native_language-Kotlin_&_Swift-green)](https://www.npmjs.com/package/react-native-encryption-algorithm)
5+
[![](https://img.shields.io/badge/size-72.7_kB-red)](https://www.npmjs.com/package/react-native-encryption-algorithm)
6+
[![](https://img.shields.io/badge/license-MIT-8A2BE2)](https://github.com/LamNguyen17/react-native-encryption-algorithm/blob/master/LICENSE)
7+
[![](https://img.shields.io/badge/author-Forest_Nguyen-f59642)](https://github.com/LamNguyen17)
48

59
## Installation
10+
```sh
11+
npm install react-native-encryption-algorithm
12+
```
13+
or
14+
```sh
15+
yarn add react-native-encryption-algorithm
16+
```
17+
18+
### Installation (iOS)
19+
20+
##### Using CocoaPods (React Native 0.60 and higher)
621

722
```sh
8-
npm install react-native-crypto-algorithm
23+
cd ios
24+
pod install
25+
```
26+
27+
##### Using React Native Link (React Native 0.59 and lower)
28+
29+
Run `react-native link react-native-encryption-algorithm` after which you should be able to use this library on iOS.
30+
31+
### Installation (Android)
32+
33+
##### React Native 0.60 and higher
34+
35+
- Linking is done automatically
36+
37+
##### Using React Native Link (React Native 0.59 and lower)
38+
39+
- In `android/settings.gradle`
40+
41+
```gradle
42+
...
43+
include ':react-native-encryption-algorithm'
44+
project(':react-native-encryption-algorithm').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-encryption-algorithm/android')
45+
```
46+
47+
- In `android/app/build.gradle`
48+
49+
```gradle
50+
...
51+
dependencies {
52+
...
53+
compile project(':react-native-encryption-algorithm')
54+
}
55+
```
56+
- register module (in MainApplication.kt)
57+
58+
```kt
59+
......
60+
import com.encryptionalgorithm.EncryptionAlgorithmPackage;
61+
......
62+
63+
override fun getPackages(): List<ReactPackage> =
64+
PackageList(this).packages.apply {
65+
add(EncryptionAlgorithmPackage());
66+
}
967
```
1068

69+
---
1170
## Usage
71+
### Methods
72+
#### 🚀 AES
73+
- Custom 'secretKey' -> maximum 32 characters
74+
. Custom 'ivKey' (optional) -> maximum 16 characters
75+
- 🍁 `encryptAES(value: string, secretKey: string, ivKey?: string)`
76+
- 🍁 `decryptAES(value: string, secretKey: string, ivKey?: string)`
77+
```js
78+
import Encryption from 'react-native-encryption-algorithm';
79+
80+
// Encrypt
81+
let encryptData = await Encryption.encryptAES('my message', 'my private key', 'my iv key(optional maximum 16 characters)');
1282

83+
// Decrypt
84+
let decryptData = await Encryption.decryptAES(encryptData, 'my private key', 'my iv key(optional maximum 16 characters)');
85+
```
86+
87+
#### 🚀 SHA256
88+
- 🍁 `hashSHA256(value: string)`
89+
```js
90+
import Encryption from 'react-native-encryption-algorithm';
91+
92+
// Hash SHA256
93+
let hashData = await Encryption.hashSHA256('my hash data');
94+
```
1395

96+
#### 🚀 RSA
97+
- 🍁 `genRSAKeyPair()`
98+
- 🍁 `encryptRSA(value: string, publicKey: string)`
99+
- 🍁 `decryptRSA(value: string, privateKey: string)`
14100
```js
15-
import { multiply } from 'react-native-crypto-algorithm';
101+
import Encryption from 'react-native-encryption-algorithm';
16102

17-
// ...
103+
// Generate RSA Key Pair
104+
let keyPair = await Encryption.genRSAKeyPair();
18105

19-
const result = await multiply(3, 7);
106+
// Encrypt RSA
107+
let encryptData = await Encryption.encryptRSA('my message', keyPair.publicKey);
108+
109+
// Decrypt RSA
110+
let decryptData = await Encryption.decryptRSA(encryptData, keyPair.privateKey);
20111
```
21112

113+
#### 🚀 HMAC / HMAC_AES
114+
- 🍁 `genHmacSecretKey() -> use with all HMAC & HMAC_AES`
115+
- 🍁 `encryptHmacAes(value: string, publicKey: string) -> use only for HMAC_AES`
116+
- 🍁 `decryptHmacAes(value: string, privateKey: string) -> use only for HMAC_AES`
117+
- 🍁 `verifyHmac(value: string, privateKey: string) -> use only for HMAC`
118+
```js
119+
import Encryption from 'react-native-encryption-algorithm';
22120

23-
## Contributing
121+
// Generate HMAC & HMAC_AES
122+
let genHmacSecretKey = await Encryption.genHmacSecretKey();
24123

25-
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
124+
// Encrypt HMAC_AES
125+
let encryptData = await Encryption.encryptHmacAes('my message', genHmacSecretKey);
26126

27-
## License
127+
// Decrypt HMAC_AES
128+
let decryptData = await Encryption.decryptHmacAes(encryptData, genHmacSecretKey);
28129

29-
MIT
130+
// VerifyHmac HMAC
131+
let verifyHmacData: boolean = await Encryption.verifyHmac('my message', genHmacSecretKey);
132+
```
30133

31134
---
32-
33-
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
135+
## API
136+
### List of Algorithms
137+
- [x] ```AES(Advanced Encryption Standard)```
138+
- [x] ```SHA-256 (Secure Hash Algorithm)```
139+
- [x] ```RSA (Rivest-Shamir-Adleman)```
140+
- [ ] ```ChaCha20```
141+
- [ ] ```Blowfish```
142+
- [x] ```HMAC (Hash-based Message Authentication Code)```
143+
- [ ] ```PBKDF2 (Password-Based Key Derivation Function 2)```
144+
- [ ] ```ECC (Elliptic Curve Cryptography)```
145+
- [ ] ```Scrypt```
146+
- [ ] ```XChaCha20-Poly1305```
147+
---
148+
## Author
149+
Forest Nguyen
150+
Email: devlamnt176@gmail.com
151+
---
152+
## License
153+
MIT License
154+
Copyright (c) 2024 Forest Nguyen
155+
---

android/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,7 @@ dependencies {
9595
//noinspection GradleDynamicVersion
9696
implementation "com.facebook.react:react-native:+"
9797
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
98+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1'
99+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.1'
98100
}
99101

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.cryptoalgorithm
2+
3+
import javax.crypto.*
4+
import android.util.*
5+
import com.cryptoalgorithm.CryptoHelper.AES
6+
7+
class AesAlgorithm {
8+
private val cryptoHelper = CryptoHelper()
9+
10+
fun encrypt(value: String, privateKey: String, ivKey: String?): String {
11+
val ivSpec = cryptoHelper.genIvKey(ivKey)
12+
val secretKey = cryptoHelper.genSecretKey(AES.ALGORITHM, privateKey)
13+
val cipher = Cipher.getInstance(CryptoHelper.AES.TRANSFORMATION)
14+
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec)
15+
val encryptedBytes = cipher.doFinal(value.toByteArray())
16+
return Base64.encodeToString(encryptedBytes, Base64.DEFAULT)
17+
}
18+
19+
fun decrypt(value: String, privateKey: String, ivKey: String?): String {
20+
return try {
21+
val ivSpec = cryptoHelper.genIvKey(ivKey)
22+
val secretKey = cryptoHelper.genSecretKey(AES.ALGORITHM, privateKey)
23+
val decodedBytes = Base64.decode(value, Base64.DEFAULT)
24+
val cipher = Cipher.getInstance(CryptoHelper.AES.TRANSFORMATION)
25+
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec)
26+
val decryptedBytes = cipher.doFinal(decodedBytes)
27+
String(decryptedBytes, Charsets.UTF_8)
28+
} catch (e: BadPaddingException) {
29+
CryptoHelper.CONSTANTS.DECRYPTION_ERR_VALID_KEY
30+
} catch (e: IllegalBlockSizeException) {
31+
CryptoHelper.CONSTANTS.DECRYPTION_ERR_INCORRECT_BLOCK_SIZE
32+
} catch (e: Exception) {
33+
CryptoHelper.CONSTANTS.DECRYPTION_ERR_UNEXPECTED_ERROR
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)