Skip to content

LamNguyen17/react-native-crypto-algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

14 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

react-native-crypto-algorithm

Installation

npm install react-native-crypto-algorithm

or

yarn add react-native-crypto-algorithm

Installation (iOS)

Using CocoaPods (React Native 0.60 and higher)
cd ios
pod install
Using React Native Link (React Native 0.59 and lower)

Run react-native link react-native-crypto-algorithm after which you should be able to use this library on iOS.

Installation (Android)

React Native 0.60 and higher
  • Linking is done automatically
Using React Native Link (React Native 0.59 and lower)
  • In android/settings.gradle
...
include ':react-native-crypto-algorithm'
project(':react-native-crypto-algorithm').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-crypto-algorithm/android')
  • In android/app/build.gradle
...
dependencies {
    ...
    compile project(':react-native-crypto-algorithm')
}
  • register module (in MainApplication.kt)
......
import com.cryptoalgorithm.CryptoAlgorithmPackage;
......

override fun getPackages(): List<ReactPackage> =
  PackageList(this).packages.apply {
    add(CryptoAlgorithmPackage());
  }

Usage

Methods

πŸš€ AES

  • Custom 'secretKey' -> maximum 32 characters . Custom 'ivKey' (optional) -> maximum 16 characters
  • 🍁 encryptAES(value: string, secretKey: string, ivKey?: string)
  • 🍁 decryptAES(value: string, secretKey: string, ivKey?: string)
import Crypto from 'react-native-crypto-algorithm';

// Encrypt
let encryptData = await Crypto.encryptAES('my message', 'my private key', 'my iv key(optional maximum 16 characters)');

// Decrypt
let decryptData = await Crypto.decryptAES(encryptData, 'my private key', 'my iv key(optional maximum 16 characters)');

πŸš€ SHA256

  • 🍁 hashSHA256(value: string)
import Crypto from 'react-native-crypto-algorithm';

// Hash SHA256
let hashData = await Crypto.hashSHA256('my hash data');

πŸš€ RSA

  • 🍁 genRSAKeyPair()
  • 🍁 encryptRSA(value: string, publicKey: string)
  • 🍁 decryptRSA(value: string, privateKey: string)
import Crypto from 'react-native-crypto-algorithm';

// Generate RSA Key Pair
let keyPair = await Crypto.genRSAKeyPair();

// Encrypt RSA
let encryptData = await Crypto.encryptRSA('my message', keyPair.publicKey);

// Decrypt RSA
let decryptData = await Crypto.decryptRSA(encryptData, keyPair.privateKey);

πŸš€ HMAC / HMAC_AES

  • 🍁 genHmacSecretKey() -> use with all HMAC & HMAC_AES
  • 🍁 encryptHmacAes(value: string, publicKey: string) -> use only for HMAC_AES
  • 🍁 decryptHmacAes(value: string, privateKey: string) -> use only for HMAC_AES
  • 🍁 verifyHmac(value: string, privateKey: string) -> use only for HMAC
import Crypto from 'react-native-crypto-algorithm';

// Generate HMAC & HMAC_AES
let genHmacSecretKey = await Crypto.genHmacSecretKey();

// Encrypt HMAC_AES
let encryptData = await Crypto.encryptHmacAes('my message', genHmacSecretKey);

// Decrypt HMAC_AES
let decryptData = await Crypto.decryptHmacAes(encryptData, genHmacSecretKey);

// VerifyHmac HMAC
let verifyHmacData: boolean = await Crypto.verifyHmac('my message', genHmacSecretKey);

API

List of Algorithms

  • AES(Advanced Encryption Standard)
  • SHA-256 (Secure Hash Algorithm)
  • RSA (Rivest-Shamir-Adleman)
  • ChaCha20
  • Blowfish
  • HMAC (Hash-based Message Authentication Code)
  • PBKDF2 (Password-Based Key Derivation Function 2)
  • ECC (Elliptic Curve Cryptography)
  • Scrypt
  • XChaCha20-Poly1305

Author

Forest Nguyen
Email: devlamnt176@gmail.com

License

MIT License
Copyright (c) 2024 Forest Nguyen