npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@egendata/react-native-simple-crypto

v1.0.2

Published

A simpler React-Native crypto library

Downloads

40

Readme

React Native Simple Crypto

License Dependabot Travis CI Github release npm version

A simpler React-Native crypto library

This is a fork of react-native-crypto which is a fork of @trackforce/react-native-crypto.

The reason for this fork is that crypto is at the core of the Egendata platform which is built to let individuals control and protect potentially sensitive, personal information. We aim to stay up to date with any significant updates in the aforementioned forks but want to do so upon close inspection of any changes made. Basically: paranoia ;)

Features

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

Installation

npm install @egendata/react-native-simple-crypto

# OR

yarn add @egendata/react-native-simple-crypto

Linking Automatically (for react-native <0.60)

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)
......
import com.pedrouid.crypto.RCTCryptoPackage;

......

@Override
protected List<ReactPackage> getPackages() {
   ......
   new RCTCryptoPackage(),
   ......
}

API

- AES
  - encrypt(text: ArrayBuffer, key: ArrayBuffer, iv: ArrayBuffer): Promise<ArrayBuffer>
  - decrypt(cipherText: ArrayBuffer, key: ArrayBuffer, iv: ArrayBuffer): Promise<ArrayBuffer>
- SHA
  - sha1(text: string): Promise<string>
  - sha256(text: string): Promise<string>
  - sha512(text: string): Promise<string>
- HMAC
  - hmac256(text: ArrayBuffer, key: ArrayBuffer): Promise<ArrayBuffer>
- PBKDF2
  - hash(password: string, salt: ArrayBuffer, iterations: number, keyLength: number, hash: string): Promise<ArrayBuffer>
- RSA
  - generateKeys(keySize: number): Promise<KeyPair>
  - encrypt(data: string, key: string): Promise<string>
  - sign(data: string, key: string, hash: string): Promise<string>
  - verify(secretToVerify: string, data: string, key: string, hash: string): Promise<boolean>
- utils
  - randomBytes(bytes: number): Promise<ArrayBuffer>
  - convertArrayBufferToUtf8(input: ArrayBuffer): string
  - convertUtf8ToArrayBuffer(input: string): ArrayBuffer
  - convertArrayBufferToBase64(input: ArrayBuffer): string
  - convertBase64ToArrayBuffer(input: string): ArrayBuffer
  - convertArrayBufferToHex(input: ArrayBuffer): string
  - convertHexToArrayBuffer(input: string): ArrayBuffer

NOTE: Supported hashing algorithms for RSA and PBKDF2 are:

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

Example

import {
  utils,
  AES,
  HMAC,
  SHA,
  PBKDF2,
  RSA
} from "@egendata/react-native-simple-crypto";

// -- AES ------------------------------------------------------------- //

const message = "data to encrypt";
const messageArrayBuffer = utils.convertUtf8ToArrayBuffer(
  message
);

const keyArrayBuffer = await utils.randomBytes(32);
console.log("randomBytes key", keyArrayBuffer);

const ivArrayBuffer = await utils.randomBytes(16);
console.log("randomBytes iv", ivArrayBuffer);

const cipherTextArrayBuffer = await AES.encrypt(
  msgArrayBuffer,
  keyArrayBuffer,
  ivArrayBuffer
);
console.log("AES encrypt", cipherTextArrayBuffer);

const messageArrayBuffer = await AES.decrypt(
  cipherTextArrayBuffer,
  keyArrayBuffer,
  ivArrayBuffer
);
const message = utils.convertArrayBufferToUtf8(
  messageArrayBuffer
);
console.log("AES decrypt", message);

// -- HMAC ------------------------------------------------------------ //

const signatureArrayBuffer = await HMAC.hmac256(message, key);

const signatureHex = utils.convertArrayBuffertoHex(
  signatureArrayBuffer
);
console.log("HMAC signature", signatureHex);

// -- SHA ------------------------------------------------------------- //

const sha1Hash = await SHA.sha1("test");
console.log("SHA1 hash", hash);

const sha256Hash = await SHA.sha1("test");
console.log("SHA256 hash", sha256Hash);

const sha512Hash = await SHA.sha1("test");
console.log("SHA512 hash", sha512Hash);

// -- PBKDF2 ---------------------------------------------------------- //

const password = "secret password";
const salt = utils.randomBytes(8);
const iterations = 4096;
const keyInBytes = 32;
const hash = "SHA1";
const passwordKey = await Pbkdf2.hash(
  password,
  salt,
  iterations,
  keyInBytes,
  hash
);
console.log("PBKDF2 passwordKey", passwordKey);

// -- RSA ------------------------------------------------------------ //

const rsaKeys = await RSA.generateKeys(1024);
console.log("RSA1024 private key", rsaKeys.private);
console.log("RSA1024 public key", rsaKeys.public);

const rsaEncryptedMessage = await RSA.encrypt(
  message,
  rsaKeys.public
);
console.log("rsa Encrypt:", rsaEncryptedMessage);

const rsaSignature = await RSA.sign(
  rsaEncryptedMessage,
  rsaKeys.private,
  "SHA256"
);
console.log("rsa Signature:", rsaSignature);

const validSignature = await RSA.verify(
  rsaSignature,
  rsaEncryptedMessage,
  rsaKeys.public,
  "SHA256"
);
console.log("rsa signature verified:", validSignature);

const rsaDecryptedMessage = await RSA.decrypt(
  rsaEncryptedMessage,
  rsaKeys.private
);
console.log("rsa Decrypt:", rsaDecryptedMessage);

Forked Libraries