Skip to content

Commit

Permalink
refactor javascript api
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrouid committed Jan 10, 2019
1 parent 0d86f62 commit 53797a4
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 119 deletions.
163 changes: 119 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,15 @@

A simpler React-Native crypto library

## API
## Features

- AES
- encrypt(text, key, iv)
- decrypt(cipherText, key, iv)
- SHA
- sha1(text)
- sha256(text)
- sha512(text)
- HMAC
- hmac256(text, key)
- AES-256-CBC
- HMAC-SHA256
- SHA1
- SHA256
- SHA512
- PBKDF2
- hash(password, saltBase64, iterations, keyLen, hash)
- RSA
- generateKeys(keySize)
- encrypt(data, key)
- sign(data, key, hash)
- verify(data, secretToVerify, hash)
- randomBytes(bytes)

## Installation

Expand Down Expand Up @@ -83,45 +73,128 @@ protected List<ReactPackage> getPackages() {
}
```

## Usage
## API

### Example
- AES
- encrypt(text, key, iv)
- decrypt(cipherText, key, iv)
- SHA
- sha1(text)
- sha256(text)
- sha512(text)
- HMAC
- hmac256(text, key)
- PBKDF2
- hash(password, salt, iterations, keyLength, hash)
- RSA
- generateKeys(keySize)
- encrypt(data, key)
- sign(data, key, hash)
- verify(data, secretToVerify, hash)
- utils
- randomBytes(bytes)
- convert
- ArrayBuffer
- to
- Utf8(arrayBuffer)
- Hex(arrayBuffer)
- Base64(arrayBuffer)
- from
- Utf8(string)
- Hex(string)
- Base64(string)
- Utf8
- to
- ArrayBuffer(string)
- from
- ArrayBuffer(arrayBuffer)
- Hex
- to
- ArrayBuffer(string)
- from
- ArrayBuffer(arrayBuffer)
- Base64
- to
- ArrayBuffer(string)
- from
- ArrayBuffer(arrayBuffer)

## Example

```javascript
import RNSimpleCrypto from "react-native-simple-crypto";

const iterations = 4096;
const keyInBytes = 32;
// -- AES ------------------------------------------------------------- //

const message = "data to encrypt";
const key = await Pbkdf2.hash("a0", "a1b4efst", iterations, keyInBytes, "SHA1");
console.log(`pbkdf2 key: ${key}`);
const messageArrayBuffer = RNSimpleCrypto.utils.convert.Utf8.to.ArrayBuffer(
message
);

const ivBuffer = Buffer.from("random16bytesstr");
const ivBase64 = ivBuffer.toString("base64");
console.log("ivBase64:", ivBase64);
const aesEncryptedMessage = await RNSimpleCrypto.AES.encrypt(
message,
key,
ivBase64
const keyArrayBuffer = await RNSimpleCrypto.randomBytes(32);
console.log("randomBytes key", keyArrayBuffer);

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

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

const messageArrayBuffer = await RNSimpleCrypto.AES.decrypt(
cipherTextArrayBuffer,
keyArrayBuffer,
ivArrayBuffer
);
const message = RNSimpleCrypto.utils.convert.ArrayBuffer.to.Utf8(
messageArrayBuffer
);
console.log(`aes Encrypt: ${aesEncryptedMessage}`);
console.log("AES decrypt", message);

const aesDecryptedMessage = await RNSimpleCrypto.AES.decrypt(
aesEncryptedMessage,
key,
ivBase64
// -- HMAC ------------------------------------------------------------ //

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

const signatureHex = RNSimpleCrypto.utils.convert.ArrayBuffer.to.Hex(
signatureArrayBuffer
);
console.log(`aes Decrypt: ${aesDecryptedMessage}`);
console.log("HMAC signature", signatureHex);

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

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

const hmac256Hash = await RNSimpleCrypto.HMAC.hmac256(message, key);
console.log(`hmac256: ${hmac256Hash}`);
const sha256Hash = await RNSimpleCrypto.SHA.sha1("test");
console.log("SHA256 hash", sha256Hash);

const sha1hash = await RNSimpleCrypto.SHA.sha1("test");
console.log(`sha1: ${sha1hash}`);
const sha512Hash = await RNSimpleCrypto.SHA.sha1("test");
console.log("SHA512 hash", sha512Hash);

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

const password = "secret password";
const salt = RNSimpleCrypto.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 RNSimpleCrypto.RSA.generateKeys(1024);
console.log("1024 private:", rsaKeys.private);
console.log("1024 public:", rsaKeys.public);
console.log("RSA1024 private key", rsaKeys.private);
console.log("RSA1024 public key", rsaKeys.public);

const rsaEncryptedMessage = await RNSimpleCrypto.RSA.encrypt(
message,
Expand Down Expand Up @@ -149,7 +222,9 @@ const rsaDecryptedMessage = await RNSimpleCrypto.RSA.decrypt(
rsaKeys.private
);
console.log("rsa Decrypt:", rsaDecryptedMessage);

const bytes = await RNSimpleCrypto.randomBytes(32);
console.log("randomBytes:", bytes);
```

## Forked Libraries

- [@trackforce/react-native-crypto](https://github.com/trackforce/react-native-crypto)
- [react-native-randombytes](https://github.com/mvayngrib/react-native-randombytes)
42 changes: 41 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,45 @@ declare module "react-native-simple-crypto" {
): Promise<boolean>;
}

export function randomBytes(bytes: number): Promise<ArrayBuffer>;
export namespace utils {
export function randomBytes(bytes: number): Promise<ArrayBuffer>;
export interface convert {
ArrayBuffer: {
to: {
Utf8: (arrayBuffer: ArrayBuffer) => string;
Hex: (arrayBuffer: ArrayBuffer) => string;
Base64: (arrayBuffer: ArrayBuffer) => string;
};
to: {
Utf8: (string: string) => ArrayBuffer;
Hex: (string: string) => ArrayBuffer;
Base64: (string: string) => ArrayBuffer;
};
};
Utf8: {
to: {
ArrayBuffer: (string: string) => ArrayBuffer;
};
from: {
ArrayBuffer: (arrayBuffer: ArrayBuffer) => string;
};
};
Hex: {
to: {
ArrayBuffer: (string: string) => ArrayBuffer;
};
from: {
ArrayBuffer: (arrayBuffer: ArrayBuffer) => string;
};
};
Base64: {
to: {
ArrayBuffer: (string: string) => ArrayBuffer;
};
from: {
ArrayBuffer: (arrayBuffer: ArrayBuffer) => string;
};
};
}
}
}
Loading

0 comments on commit 53797a4

Please sign in to comment.