dhairya0907 / react-native-encryption

Encryption/Decryption for React Native
https://www.npmjs.com/package/@dhairyasharma/react-native-encryption
MIT License
7 stars 3 forks source link
decryption encryption encryption-decryption file-encryption large-files npm-package react-native text-encryption

@dhairyasharma/react-native-encryption

Supports Android, iOS MIT License Maintenance Maintainer Generic badge

Twitter LinkedIn

Encryption/decryption for React Native.

 

Benchmark

File Details
File Link
http://bit.do/benchmarkfile
File Size
1.09 GB
IOS Benchmark
Average of 100 Encryption
4.859 seconds

Average of 100 Decryption
5.594 seconds

Check Log for all IOS encryption here.
Check Log for all IOS decryption here.
Android Benchmark
Average of 100 Encryption
20.373 seconds

Average of 100 Decryption
21.908 seconds

Check Log for all Android encryption here.
Check Log for all Android decryption here.

 

Features

 

Getting started

Install the library using either Yarn:

yarn add @dhairyasharma/react-native-encryption

or npm:

npm install --save @dhairyasharma/react-native-encryption

or git:

npm install git+https://github.com/dhairya0907/react-native-encryption.git

Using React Native >= 0.60

  Linking the package manually is not required anymore with Autolinking.

cd ios && pod install && cd ..

Usage

Import the library

import RNEncryptionModule from "@dhairyasharma/react-native-encryption";

Encrypt Text

RNEncryptionModule.encryptText(
    plainText,
    password
    ).then((res: any) => {
        if (res.status == "success") {
            console.log("success", res)
        } else {
            Alert.alert("Error", res);
        }
        }).catch((err: any) => {
            console.log(err);
        });
returns

// If text encryption is successful, it returns a JSON object with the following structure:
{
  "status": "success",
  "encryptedText": "encryptedText",
  "iv": "iv",
  "salt": "salt"
}

or

// If text encryption is unsuccessful, it returns a JSON object with the following structure:
{
  "status": "Fail",
  "error": "error"
}

Decrypt Text

RNEncryptionModule.decryptText(
    encryptedText,
    password,
    iv,
    salt).then((res: any) => {
        if (res.status == "success") {
            console.log("success", res)
        } else {
            Alert.alert("Error", res);
        }
        }).catch((err: any) => {
            console.log(err);
        });
returns

// If text decryption is successful, it returns a JSON object with the following structure:
{
  "status": "success",
  "decryptedText": "decryptedText"
}

or

// If text decryption is unsuccessful, it returns a JSON object with the following structure:
{
  "status": "Fail",
  "error": "error"
}

Encrypt File

 RNEncryptionModule.encryptFile(
      inputFilePath,
      outputEncryptedFilePath,
      password
    ).then((res: any) => {
        if (res.status == "success") {
            console.log("success", res)
        } else {
            console.log("error", res);
        }
        }).catch((err: any) => {
            console.log(err);
        });
returns

// If file encryption is successful, it returns a JSON object with the following structure:
{
  "status": "success",
  "iv": "iv",
  "salt": "salt"

}

or

// If file encryption is unsuccessful, it returns a JSON object with the following structure:
{
  "status": "Fail",
  "error": "error"
}

Decrypt File

 RNEncryptionModule.decryptFile(
      encryptedFilePath,
      outputDecryptedFilePath,
      password,
      iv,
      salt
    ).then((res: any) => {
        if (res.status == "success") {
            console.log("success", res)
        } else {
            console.log("error", res);
        }
        }).catch((err: any) => {
            console.log(err);
        });
returns

// If file decryption is successful, it returns a JSON object with the following structure:
{
  "status": "success",
  "message" : "File decrypted successfully"

}

or

// If file decryption is unsuccessful, it returns a JSON object with the following structure:
{
  "status": "Fail",
  "error": "error"
}

NOTE

On android, Input or Output file path does not support content://  type file path. Please use file://  type file path.

You can use react-native-fetch-blob stat to get original path. See example.

You will need Files and media permission for this.


 

Acknowledgements

 

Author

Dhairya Sharma | @dhairya0907

 

License

The library is released under the MIT license. For more information see LICENSE.