Skip to content

Repository files navigation

React Native Simple Crypto npm version

A simpler React-Native crypto library

Features

  • AES-128-CBC
  • HMAC-SHA256
  • SHA1
  • SHA256
  • SHA512
  • PBKDF2
  • RSA

Installation

npm install react-native-simple-crypto

For React Native 0.60+, autolinking handles native setup automatically. For iOS, run cd ios && pod install.

API

All methods are asynchronous and return promises (except for convert utils)

-AES-encrypt(text: ArrayBuffer,key: ArrayBuffer,iv: ArrayBuffer)-decrypt(cipherText: ArrayBuffer,key: ArrayBuffer,iv: ArrayBuffer)-SHA-sha1(text: string)-sha1(text: ArrayBuffer)-sha256(text: string)-sha256(text: ArrayBuffer)-sha512(text: string)-sha512(text: ArrayBuffer)-HMAC-hmac256(text: ArrayBuffer,key: ArrayBuffer)-PBKDF2-hash(password: string,salt: ArrayBuffer,iterations: number,keyLength: number,hash: string)-RSA-generateKeys(keySize: number)-sign(data: string,key: string,hash: string)-verify(data: string,secretToVerify: string,hash: string)-encrypt(data: string,key: string)(ExpectsUTF8 string datainputs)-decrypt(data: string,key: string)(ReturnsUTF8 string)-encrypt64(data: string,key: string)(ExpectsBase64 string datainputs)-decrypt64(data: string,key: string)(ReturnsBase64 string)-utils-randomBytes(bytes: number)-convertArrayBufferToUtf8(input: ArrayBuffer)-convertUtf8ToArrayBuffer(input: string)-convertArrayBufferToBase64(input: ArrayBuffer)-convertBase64ToArrayBuffer(input: string)-convertArrayBufferToHex(input: ArrayBuffer)-convertHexToArrayBuffer(input: string)

NOTE: Supported hashing algorithms for RSA and PBKDF2 are:

"Raw" (RSA-only) | "SHA1" | "SHA224" | "SHA256" | "SHA384" | "SHA512"

Example

importRNSimpleCryptofrom"react-native-simple-crypto";// -- AES ------------------------------------------------------------- //constmessage="data to encrypt";constmessageBuffer=RNSimpleCrypto.utils.convertUtf8ToArrayBuffer(message);constkey=awaitRNSimpleCrypto.utils.randomBytes(16);constiv=awaitRNSimpleCrypto.utils.randomBytes(16);constcipherText=awaitRNSimpleCrypto.AES.encrypt(messageBuffer,key,iv);constdecrypted=awaitRNSimpleCrypto.AES.decrypt(cipherText,key,iv);console.log(RNSimpleCrypto.utils.convertArrayBufferToUtf8(decrypted));// "data to encrypt"// -- SHA ------------------------------------------------------------- //constsha256=awaitRNSimpleCrypto.SHA.sha256("test");// -- HMAC ------------------------------------------------------------ //consthmacKey=awaitRNSimpleCrypto.utils.randomBytes(32);constsignature=awaitRNSimpleCrypto.HMAC.hmac256(messageBuffer,hmacKey);// -- PBKDF2 ---------------------------------------------------------- //constderivedKey=awaitRNSimpleCrypto.PBKDF2.hash("password","salt",4096,32,"SHA1");// -- RSA ------------------------------------------------------------- //constrsaKeys=awaitRNSimpleCrypto.RSA.generateKeys(2048);constencrypted=awaitRNSimpleCrypto.RSA.encrypt(message,rsaKeys.public);constdecryptedRsa=awaitRNSimpleCrypto.RSA.decrypt(encrypted,rsaKeys.private);constsig=awaitRNSimpleCrypto.RSA.sign(message,rsaKeys.private,"SHA256");constvalid=awaitRNSimpleCrypto.RSA.verify(sig,message,rsaKeys.public,"SHA256");

Running E2E Tests

The test suite uses a React Native example app with Maestro for E2E testing. Tests cover AES, SHA, HMAC, PBKDF2, RSA, and utility functions (22+ assertions).

Prerequisites

curl -Ls "https://get.maestro.mobile.dev"| bash

Setup

# Install library dependencies (needed for base64-js)
npm install
# Install example app dependenciescd example
npm install
cd ios && bundle exec pod install &&cd ..

Run

# Terminal 1: Start Metrocd example
npx react-native start --reset-cache
# Terminal 2: Build and run the app on simulatorcd example
npx react-native run-ios --simulator="iPhone 16 Pro"# Terminal 3: Run Maestro tests (once the app is loaded)
maestro test maestro/flows/run_all_tests.yaml

You can also run individual test modules:

maestro test maestro/flows/aes_tests.yaml
maestro test maestro/flows/sha_tests.yaml
maestro test maestro/flows/hmac_tests.yaml
maestro test maestro/flows/pbkdf2_tests.yaml
maestro test maestro/flows/rsa_tests.yaml
maestro test maestro/flows/utils_tests.yaml

Forked Libraries

About

A simpler React-Native crypto library

Resources

Stars

40 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages