npm package: @fntools/crypto
Description: A lightweight encryption library that outputs hashes as JSON for using in the server and in any browser based storage
Author: Lluie Cain Andaya
Latest version: 1.0.0



A lightweight encryption library that outputs hashes as JSON for using in the server and in any browser based storage
## Instalation

```bash
# NPM
$ npm i @fntools/crypto --save

# YARN
$ yarn add @fntools/crypto
```


## Importing package

```javascript
// ESM
import crypto from "@fntools/crypto";

// CJS
const crypto = require("@fntools/crypto");
```

## Get Started

### Primary Usage

```javascript
import crypto from "@fntools/crypto";
// encrypt
const encrypted = crypto.encrypt("some data", "some-password", "some-salt");
// decrypt
crypto.decrypt(encrypted, "some-password", "some-salt"); 
```


### Using Callback 
```javascript 
// encrypting 
crypto.encrypt("some-data", "some-password", "salt", (data) => {
  // ...do something cool 
}, (err) => {
  // ...handle error  
})

// decrypting 
crypto.decrypt("some-hash", "some-password", "salt", (data) => {
  // ...do something cool 
}, (err) => {
  // ...handle error 
})

```


### Promise Approach
```javascript  
  crypto.async.encrypt("some-data", "some-password", "salt").then((data) => {
  // ...do something cool 
}).catch((err) => {
  // ...handle error 
})

// decrypting 
crypto.async.decrypt("some-hash", "some-password", "salt").then((data) => {
  // ...do something cool 
}).catch((err) => {
  // ...handle error 
})

```

## API Documentation 

### Crypto#Encrypt
```javascript 


 crypto.encrypt(data:any , password:string, salt?:string, callback?:function, errorCallback?:function);


``` 
**Parameters**

  **data** Any type as long as it can be conveted to string or JSON

  **password**  Required password for incrypting the given **data**
  
  **salt** _Optional_ secret key that differ each data even if given the same **data** or **password**
  
  **callback:Function(data:string)** _Optional_ callback function called when encryption is a success  
  
  **errorCallback:?Function(err:Error)** _Optional_ callback that will handle encryption error 


### Crypto#Decrypt
```javascript 


crypto.decrypt(hash:string|JSON, password:string, salt?:string, callback:Function, errorCallback:function)


```
**Parameters**
  **hash**  Hash string or JSON object that contains all the needed data for decryption
  
  **password**  Required password for decrypting **hash**
  
  **salt** _Optional_ given only when there is salt when encryping  
  
  **callback:Function(data:string)** _Optional_ callback function called when decryption is a success
 
  **errorCallback:?Function(err:Error)** _Optional_ callback that will handle decryption error

  **NOTE:**  _if it is incrypted with salt it can't be decrypted without salt_ 



## Promise and Async Environment

#### Promise

```javascript  
// just replace the callbacks with .then and .catch
  crypto.async.encrypt("some-data", "some-password", "salt").then((data) => {
  // ...do something cool 
}).catch((err) => {
  // ...handle error 
})

// decrypting 
crypto.async.decrypt("some-hash", "some-password", "salt").then((data) => {
  // ...do something cool 
}).catch((err) => {
  // ...handle error 
})

```
#### Async, Await 
```javascript  
// encryption

const encrypted = await crypto.async.encrypt("some-data", "some-password", "salt")

// decrypting 

const decryptedData = await crypto.async.decrypt(encrypted, "some-password", "salt");

```