React Native Simple Crypto
A simpler React-Native crypto library
Features
- AES-128-CBC
- HMAC-SHA256
- SHA1
- SHA256
- SHA512
- PBKDF2
- RSA
Installation
npm install react-native-simple-crypto # OR yarn add react-native-simple-crypto
Linking Automatically
react-native link react-native-simple-crypto
Linking Manually
iOS
- See Linking Libraries OR
- Drag RCTCrypto.xcodeproj to your project on Xcode.
- Click on your main project file (the one that represents the .xcodeproj) select Build Phases and drag libRCTCrypto.a from the Products folder inside the RCTCrypto.xcodeproj.
(Android)
- In
android/settings.gradle
...include ':react-native-simple-crypto'project(':react-native-simple-crypto').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-simple-crypto/android')
- In
android/app/build.gradle
...dependencies { ... compile project(':react-native-simple-crypto')}
- register module (in MainApplication.java)
...... ...... @Overrideprotected List<ReactPackage> { ...... new RNSCCryptoPackage(), ......}
API
All methods are asynchronous and return promises (except for convert utils)
- AES - encrypttext: ArrayBuffer, key: ArrayBuffer, iv: ArrayBuffer - decryptcipherText: ArrayBuffer, key: ArrayBuffer, iv: ArrayBuffer- SHA - sha1text: string - sha1text: ArrayBuffer - sha256text: string - sha256text: ArrayBuffer - sha512text: string - sha512text: ArrayBuffer- HMAC - hmac256text: ArrayBuffer, key: ArrayBuffer- PBKDF2 - hashpassword: string, salt: ArrayBuffer, iterations: number, keyLength: number, hash: string- RSA - generateKeyskeySize: number - encryptdata: string, key: string - signdata: string, key: string, hash: string - verifydata: string, secretToVerify: string, hash: string- utils - randomBytesbytes: number - convertArrayBufferToUtf8input: ArrayBuffer - convertUtf8ToArrayBufferinput: string - convertArrayBufferToBase64input: ArrayBuffer - convertBase64ToArrayBufferinput: string - convertArrayBufferToHexinput: ArrayBuffer - convertHexToArrayBufferinput: string
NOTE: Supported hashing algorithms for RSA and PBKDF2 are:
"Raw" (RSA-only) | "SHA1" | "SHA224" | "SHA256" | "SHA384" | "SHA512"
Example
Testing repository.
; const toHex = RNSimpleCryptoutilsconvertArrayBufferToHexconst toUtf8 = RNSimpleCryptoutilsconvertArrayBufferToUtf8 // -- AES ------------------------------------------------------------- //const message = "data to encrypt";const messageArrayBuffer = RNSimpleCryptoutils; const keyArrayBuffer = await RNSimpleCryptoutils;console; const ivArrayBuffer = await RNSimpleCryptoutils;console; const cipherTextArrayBuffer = await RNSimpleCryptoAES;console const decryptedArrayBuffer = await RNSimpleCryptoAES;console;if !== message console // -- HMAC ------------------------------------------------------------ // const keyHmac = await RNSimpleCryptoutils;const signatureArrayBuffer = await RNSimpleCryptoHMAC;console; // -- SHA ------------------------------------------------------------- // const sha1Hash = await RNSimpleCryptoSHA;console; const sha256Hash = await RNSimpleCryptoSHA;console; const sha512Hash = await RNSimpleCryptoSHA;console; const arrayBufferToHash = RNSimpleCryptoutils;const sha1ArrayBuffer = await RNSimpleCryptoSHA;console;if !== sha1Hash console const sha256ArrayBuffer = await RNSimpleCryptoSHA;console;if !== sha256Hash console const sha512ArrayBuffer = await RNSimpleCryptoSHA;console;if !== sha512Hash console // -- PBKDF2 ---------------------------------------------------------- // const password = "secret password";const salt = "my-salt"const iterations = 4096;const keyInBytes = 32;const hash = "SHA1";const passwordKey = await RNSimpleCryptoPBKDF2hash password salt iterations keyInBytes hash;console; const passwordKeyArrayBuffer = await RNSimpleCryptoPBKDF2hash RNSimpleCryptoutils RNSimpleCryptoutils iterations keyInBytes hash;console; if !== console const password2 = messageArrayBuffer;const salt2 = await RNSimpleCryptoutils;const iterations2 = 10000;const keyInBytes2 = 32;const hash2 = "SHA256"; const passwordKey2 = await RNSimpleCryptoPBKDF2hash password2 salt2 iterations2 keyInBytes2 hash2;console; // -- RSA ------------------------------------------------------------ // const rsaKeys = await RNSimpleCryptoRSA;console;console; const rsaEncryptedMessage = await RNSimpleCryptoRSA;console; const rsaSignature = await RNSimpleCryptoRSA;console; const validSignature = await RNSimpleCryptoRSA;console; const rsaDecryptedMessage = await RNSimpleCryptoRSA;console;if rsaDecryptedMessage !== message console