# @guanghechen/cipher-helper

> Cipher helper

Latest version **1.9.8** (published 2022-07-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @guanghechen/cipher-helper
pnpm add @guanghechen/cipher-helper
yarn add @guanghechen/cipher-helper
bun add @guanghechen/cipher-helper
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.9.8 |
| Published | 2022-07-27 |
| First published | 2021-07-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 14.15.0 |
| Dependencies | 3 |
| Unpacked size | 43.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | guanghechen |
| Maintainers | lemonclown |
| Keywords | cipher, cipher helper |

## Links

- npm: https://www.npmjs.com/package/@guanghechen/cipher-helper
- Repository: https://github.com/guanghechen/node-scaffolds
- Homepage: https://github.com/guanghechen/node-scaffolds/tree/main/packages/cipher-helper#readme
- Issues: https://github.com/guanghechen/node-scaffolds/issues
- npm.io page: https://npm.io/package/@guanghechen/cipher-helper

## Dependencies (3)

- [fs-extra](https://npm.io/package/fs-extra.md) ^10.1.0
- [@guanghechen/invariant](https://npm.io/package/@guanghechen/invariant.md) ^1.9.8
- [@guanghechen/file-helper](https://npm.io/package/@guanghechen/file-helper.md) ^1.9.8

## Alternatives

- [@gemini-wallet/core](https://npm.io/package/@gemini-wallet/core.md) — 515.6K weekly downloads
- [utility](https://npm.io/package/utility.md) — 416.6K weekly downloads
- [@primno/dpapi](https://npm.io/package/@primno/dpapi.md) — 7.2K weekly downloads
- [pi-readseek](https://npm.io/package/pi-readseek.md) — 3.7K weekly downloads
- [@emilia-protocol/verify](https://npm.io/package/@emilia-protocol/verify.md) — 1.1K weekly downloads

## Recent versions

- 1.9.8 (latest) — 2022-07-27
- 1.9.7 — 2022-07-17
- 1.9.6 — 2022-07-10
- 1.9.5 — 2022-06-03
- 1.9.4 — 2022-04-27
- 1.9.3 — 2022-04-10
- 1.9.2 — 2022-03-12
- 1.9.1 — 2022-02-23
- 1.9.0 — 2022-02-17
- 1.9.0-alpha.0 — 2022-02-16
- 1.8.6 — 2022-01-15
- 1.8.5 — 2021-12-05
- 1.8.4 — 2021-10-28
- 1.8.3 — 2021-08-18
- 1.8.2 — 2021-08-14
- … 8 more at https://npm.io/package/@guanghechen/cipher-helper/versions

## README

<header>
  <h1 align="center">
    <a href="https://github.com/guanghechen/node-scaffolds/tree/main/packages/cipher-helper#readme">@guanghechen/cipher-helper</a>
  </h1>
  <div align="center">
    <a href="https://www.npmjs.com/package/@guanghechen/cipher-helper">
      <img
        alt="Npm Version"
        src="https://img.shields.io/npm/v/@guanghechen/cipher-helper.svg"
      />
    </a>
    <a href="https://www.npmjs.com/package/@guanghechen/cipher-helper">
      <img
        alt="Npm Download"
        src="https://img.shields.io/npm/dm/@guanghechen/cipher-helper.svg"
      />
    </a>
    <a href="https://www.npmjs.com/package/@guanghechen/cipher-helper">
      <img
        alt="Npm License"
        src="https://img.shields.io/npm/l/@guanghechen/cipher-helper.svg"
      />
    </a>
    <a href="#install">
      <img
        alt="Module Formats: cjs"
        src="https://img.shields.io/badge/module_formats-cjs-green.svg"
      />
    </a>
    <a href="https://github.com/nodejs/node">
      <img
        alt="Node.js Version"
        src="https://img.shields.io/node/v/@guanghechen/cipher-helper"
      />
    </a>
    <a href="https://github.com/facebook/jest">
      <img
        alt="Tested with Jest"
        src="https://img.shields.io/badge/tested_with-jest-9c465e.svg"
      />
    </a>
    <a href="https://github.com/prettier/prettier">
      <img
        alt="Code Style: prettier"
        src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square"
      />
    </a>
  </div>
</header>
<br/>

Utility functions for cipher contents or files.


## Install

* npm

  ```bash
  npm install --save @guanghechen/cipher-helper
  ```

* yarn

  ```bash
  yarn add @guanghechen/cipher-helper
  ```

## Usage

* `AESCipherHelper`

  ```typescript
  import { AESCipherHelper } from '@guanghechen/cipher-helper'

  const cipher = new AESCipherHelper()  // Some options
  cipher.initFromSecret(cipher.createSecret())  // Or use password 

  // encrypt / decrypt
  cipher.encrypt(Buffer.from('plain data'))     // => Buffer
  cipher.decrypt(Buffer.from('cypher data'))    // => Buffer

  // encrypt / decrypt file
  cipher.encryptFile('plain.txt', 'cipher.txt')
  cipher.decryptFile('cipher.txt', 'plain.txt')

  // encrypt / decrypt files
  cipher.encryptFile(['plain1.txt', 'plain2.txt'], 'cipher.txt')
  cipher.decryptFile(['cipher1.txt', 'cipher2.txt'], 'plain.txt')
  ```

* `CipherCatalog`

  ```typescript
  import { AESCipherHelper, CipherCatalog } from '@guanghechen/cipher-helper'
  import path from 'path'

  const password = Buffer.from('password, such as `@guanghechen/cipher-helper`') 
  const cipher = new AESCipherHelper()
  cipher.initFromPassword(password)

  const catalog = new CipherCatalog({
    cipher,
    sourceRootDir: path.join(__dirname, 'workspace/source'),
    targetRootDir: path.join(__dirname, 'workspace/target'),
  })

  catalog.calcAbsoluteSourceFilepath('a.md')              // Resolve the absolute path of a source file.
  catalog.calcAbsoluteTargetFilepath('a.md')              // Resolve the absolute path of a target file.
  catalog.calcRelativeSourceFilepath('<WORKSPACE>/a.md')  // Resolve the relative path of the source file.
  catalog.calcRelativeTargetFilepath('<WORKSPACE>/a.md')  // Resolve the relative path of the target file.
  catalog.checkIntegrity()                                // Check if the index file is damaged.
  catalog.cleanup()                                       // Perform cleanup operations.
  catalog.decryptAll(bakSourceRootDir)                    // Decrypt all target files and output into the given directory.
  catalog.dump()                                          // Dump catalog states.
  catalog.isModified('a.md')                              // Test whether the given file has changed. (provide a simple filtering for continued operations)
  catalog.load(cipheredCatalogIndex)                      // Load states from ciphered data string
  catalog.loadFroMFile('catalog.txt')                     // Load states from index files.
  catalog.reset()                                         // Reset inner states.
  catalog.save('catalog.index.txt')                       // Dump catalog data and save into the index file.
  catalog.register(sourceFilepath)                        // Register a item into the catalog and perform some cleanup operations.
  catalog.touch()                                         // Synchronize the lastCheckTime.
  ```


### Overview

Name                                | Description
:----------------------------------:|:----------------------------:
`calcFingerprint`                   | Calc fingerprint of Buffer contents
`calcMac`                           | Calc mac (Message Authentication Code)
`calcMacFromFile`                   | Calc mac (Message Authentication Code) from file
`createRandomIv`                    | Create random initial vector
`createRandomKey`                   | Create random key of aes
`destroyBuffer`                     | Fill buffer with a random number
`destroyBuffers`                    | Fill buffers with random numbers
`streams2buffer`                    | Merge multiple read streams into Buffer serially
`AESCipherHelper`                   | A CipherHelper implementation with AES algorithm.
`CipherCatalog`                     | Catalog for managing source / target files and relationship maps



[homepage]: https://github.com/guanghechen/node-scaffolds/tree/main/packages/cipher-helper#readme

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