2019-03-30 20:50:04 +01:00
|
|
|
# occulto 🔒
|
|
|
|
|
2019-07-07 21:20:42 +02:00
|
|
|
High level wrapper around [forge](https://github.com/digitalbazaar/forge).
|
|
|
|
|
|
|
|
Supports Hashes, Symmetric AES & ChaCha20 ciphers and Asymmetric RSA.
|
2019-03-30 20:50:04 +01:00
|
|
|
|
|
|
|
**Typescript typings included**
|
|
|
|
|
|
|
|
## Quickstart 🚀
|
|
|
|
|
|
|
|
###### Install
|
|
|
|
|
|
|
|
```
|
2019-07-07 21:20:42 +02:00
|
|
|
npm i node-forge occulto
|
2019-03-30 20:50:04 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
// Whatever import you prefer
|
|
|
|
// const { RSA } = require('occulto')
|
|
|
|
import { RSA } from 'occulto'
|
|
|
|
|
|
|
|
const pair = await RSA.gen()
|
|
|
|
|
|
|
|
const encrypted = RSA.encrypt('some string', 'myPass')
|
|
|
|
const decrypted = RSA.decrypt(encrypted, 'myPass')
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
### Reference 📒
|
|
|
|
|
|
|
|
## RSA
|
|
|
|
|
2019-03-30 21:01:12 +01:00
|
|
|
#### `RSA.gen(size: number = 2 ** 12)`
|
2019-03-30 20:50:04 +01:00
|
|
|
|
2019-03-30 21:01:12 +01:00
|
|
|
- size: [optional, default=4096] Size of the RSA key
|
2019-03-30 20:50:04 +01:00
|
|
|
|
|
|
|
###### Examples
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
const pair = await RSA.gen() // 4096-Bit
|
|
|
|
const smallPair = await RSA.gen(2**10) // 1024-Bit
|
|
|
|
```
|
|
|
|
|
2019-03-30 21:01:12 +01:00
|
|
|
#### `RSA.encrypt(data: string, key: PublicKey)`
|
2019-03-30 20:50:04 +01:00
|
|
|
|
2019-03-30 21:01:12 +01:00
|
|
|
Encrypt message with public key
|
2019-03-30 20:50:04 +01:00
|
|
|
|
|
|
|
###### Example
|
|
|
|
|
|
|
|
```javascript
|
2019-03-30 21:01:12 +01:00
|
|
|
const pair = await RSA.gen()
|
|
|
|
const encrypted = RSA.encrypt('some text', pair.pub)
|
2019-03-30 20:50:04 +01:00
|
|
|
```
|
|
|
|
|
2019-03-30 21:01:12 +01:00
|
|
|
#### `RSA.decrypt(data: string, key: PrivateKey)`
|
2019-03-30 20:50:04 +01:00
|
|
|
|
2019-03-30 21:01:12 +01:00
|
|
|
Decrypts a message encrypted with `RSA.encrypt()` with the private key
|
2019-03-30 20:50:04 +01:00
|
|
|
|
|
|
|
###### Example
|
|
|
|
|
|
|
|
```javascript
|
2019-03-30 21:01:12 +01:00
|
|
|
const pair = await RSA.gen()
|
|
|
|
const encrypted = RSA.encrypt('some text', pair.pub)
|
|
|
|
const decrypted = RSA.decrypt(encrypted, pair.prv)
|
2019-03-30 20:50:04 +01:00
|
|
|
```
|
|
|
|
|
2019-07-07 21:20:42 +02:00
|
|
|
## Symmetric
|
2019-03-30 20:50:04 +01:00
|
|
|
|
2019-07-07 21:20:42 +02:00
|
|
|
### `Symmetric.Ciphers`
|
2019-03-30 20:50:04 +01:00
|
|
|
|
2019-03-30 21:01:12 +01:00
|
|
|
Available ciphers
|
2019-03-30 20:50:04 +01:00
|
|
|
|
2019-07-07 21:20:42 +02:00
|
|
|
- `Ciphers.ChaCha20`
|
2019-03-30 21:01:12 +01:00
|
|
|
- `Ciphers.AES_256_GCM`
|
|
|
|
- `Ciphers.AES_192_GCM`
|
|
|
|
- `Ciphers.AES_128_GCM`
|
2019-07-07 21:20:42 +02:00
|
|
|
- `Ciphers.AES_256_CTR`
|
|
|
|
- `Ciphers.AES_192_CTR`
|
|
|
|
- `Ciphers.AES_128_CTR`
|
2019-03-30 20:50:04 +01:00
|
|
|
|
2019-07-07 21:20:42 +02:00
|
|
|
#### `Symmetric.encrypt(data: string, key: string, type: Ciphers = Ciphers.AES_256_GCM)`
|
2019-03-30 20:50:04 +01:00
|
|
|
|
2019-03-30 21:01:12 +01:00
|
|
|
Encrypts a string.
|
2019-07-07 21:20:42 +02:00
|
|
|
Defaults to `Ciphers.AES_256_CTR`
|
2019-03-30 20:50:04 +01:00
|
|
|
|
2019-03-30 21:01:12 +01:00
|
|
|
###### Examples
|
2019-03-30 20:50:04 +01:00
|
|
|
|
|
|
|
```javascript
|
2019-07-07 21:20:42 +02:00
|
|
|
const encrypted = Symmetric.encrypt('some string' , 'myPass')
|
2019-03-30 20:50:04 +01:00
|
|
|
|
2019-07-07 21:20:42 +02:00
|
|
|
const e = Symmetric.encrypt('some string' , 'myPass', Ciphers.AES_128_GCM)
|
2019-03-30 20:50:04 +01:00
|
|
|
```
|
|
|
|
|
2019-07-07 21:20:42 +02:00
|
|
|
## Hash
|
2019-03-30 20:50:04 +01:00
|
|
|
|
2019-07-07 21:20:42 +02:00
|
|
|
### `Hash.Hashes`
|
2019-03-30 20:50:04 +01:00
|
|
|
|
2019-07-07 21:20:42 +02:00
|
|
|
Available hashes
|
|
|
|
|
|
|
|
- `Hashes.MD5`
|
|
|
|
- `Hashes.SHA1_1`
|
|
|
|
- `Hashes.SHA1_256`
|
|
|
|
- `Hashes.SHA1_512`
|
|
|
|
- `Hashes.SHA3_256`
|
|
|
|
- `Hashes.SHA3_384`
|
|
|
|
- `Hashes.SHA3_512`
|
|
|
|
|
|
|
|
#### `Hash.digest(s: string, type: Hashes = Hashes.SHA3_256)`
|
|
|
|
|
|
|
|
Calculates the hash of a string.
|
|
|
|
Defaults to `Hashes.SHA3_256`
|
|
|
|
|
|
|
|
###### Examples
|
2019-03-30 20:50:04 +01:00
|
|
|
|
|
|
|
```javascript
|
2019-07-07 21:20:42 +02:00
|
|
|
const hash = Hash.digest('something')
|
|
|
|
|
|
|
|
const h = Hash.digest('something', Hashes.MD5)
|
|
|
|
|
2019-03-30 20:50:04 +01:00
|
|
|
```
|