# json-deepcrypt

> encrypt and MAC complex json stuff using a field schema

Latest version **0.1.0** (published 2020-09-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-deepcrypt
pnpm add json-deepcrypt
yarn add json-deepcrypt
bun add json-deepcrypt
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2020-09-05 |
| First published | 2020-09-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 17 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Silur |
| Maintainers | silur |
| Keywords | encrypt, json, rsa, ecc, aes, hash |

## Links

- npm: https://www.npmjs.com/package/json-deepcrypt
- Repository: https://github.com/Silur/json-deepcrypt
- Homepage: https://github.com/Silur/json-deepcrypt#readme
- Issues: https://github.com/Silur/json-deepcrypt/issues
- npm.io page: https://npm.io/package/json-deepcrypt

## Dependencies (6)

- [joi](https://npm.io/package/joi.md) ^17.2.1
- [chai](https://npm.io/package/chai.md) ^4.2.0
- [mocha](https://npm.io/package/mocha.md) ^8.1.3
- [openpgp](https://npm.io/package/openpgp.md) ^4.10.8
- [jsonwalk](https://npm.io/package/jsonwalk.md) ^0.1.0
- [js-sha256](https://npm.io/package/js-sha256.md) ^0.9.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.1.0 (latest) — 2020-09-05

## README

# json-deepcrypt ![](https://img.shields.io/npm/v/json-deepcrypt)

A tool to recursively encrypt and optionally MAC specified fields of JSON data based on `jq`-like schemas. The codebase was originally intended to ease pinning of JSON-based stuff on blockchain but found other use cases. It uses [OpenPGP.js](https://openpgpjs.org/) under the hood

## Install

```bash
npm install -S json-deepcrypt
```

## Usage

### With AES256
```javascript
  const cipherText = await jsonDeepcrypt.encrypt({
    data: JSON.stringify(plainText),
    privateFields: ['Account.Order.$.OrderID'], // $ to wildcard array elements
    password: `thats my kung fu`,
    salt: `somesalt`
  })
  console.log(JSON.stringify(cipherText)) // you get an object as result
  const decrypted = await jsonDeepcrypt.decrypt({
    data: cipherText, // you can pass object or string too here
    privateFields: ['Account.Order.$.OrderID'],
    password: `thats my kung fu`,
    salt: `somesalt`
  })
  console.log(decrypted)
```

### With PKI

```javascript
const publicKeyArmored = `-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----`;
    const privateKeyArmored = `-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----`; // encrypted private key

const cipherText = await jsonDeepcrypt.encrypt({
    data: plainText, // ... or object
    privateFields: ['Account.Order.$.OrderID'],
    pubKeys: [publicKeyArmored] // you can encrypt to multiple keys!
  })
  console.log(JSON.stringify(cipherText))

  const decrypted = await jsonDeepcrypt.decrypt({
    data: cipherText,
    password: password,
    privateFields: ['Account.Order.$.OrderID'],
    privKey: privateKeyArmored
  })
  console.log(decrypted)
```

### With MAC verification
```javascript
  const cipherText = await jsonDeepcrypt.encrypt({
    data: JSON.stringify(plainText),
    privateFields: ['Account.Order.$.OrderID'],
    password: password,
    salt: 'somesalt',
    hmacKey: 'another key for the hmac'
  })
  console.log(JSON.stringify(cipherText))

  const decrypted = await jsonDeepcrypt.decrypt({
    data: JSON.stringify(cipherText),
    privateFields: ['Account.Order.$.OrderID'],
    password: password,
    salt: 'somesalt',
    hmacKey: 'another key for the hmac'
  })
  console.log(decrypted)
  
  const shouldFail = await jsonDeepcrypt.decrypt({
    data: JSON.stringify(cipherText),
    privateFields: ['Account.Order.$.OrderID'],
    password: password,
    salt: 'somesalt',
    hmacKey: 'wrong hmac breaks hashes'
  })
  // should throw an error
```

### Trivia

The data format for encrypted fields is:

- `_data:gq9kule12j6wMA...ayl5CeTPw5E=` with no HMAC
- `_data:gq9kule12j6wMA...ayl5CeTPw5E=;_hmac:2d17f4...85420c` with HMAC

---
_Source: https://npm.io/package/json-deepcrypt · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
