项目作者: hugorodrigues

项目描述 :
NodeJS library to encrypt and decrypt data using RSA keys
高级语言: JavaScript
项目地址: git://github.com/hugorodrigues/octosecret-crypto.git
创建时间: 2018-02-17T22:45:33Z
项目社区:https://github.com/hugorodrigues/octosecret-crypto

开源协议:MIT License

下载


octosecret-crypto

CircleCI
Known Vulnerabilities
NSP Status

Encrypt and decrypt data using RSA keys

What

Nodejs library that wraps openSSL to provide encryption and decryption of files and buffers.
This was originally created to provide the crypto layer of octosecret, a CLI that allows easy data encryption and decryption between github users.

Features

  • Encrypt and decrypt files
  • Encrypt and decrypt streams (eg: text, buffer..)
  • Encryption support multiple keys

Install

  1. npm install octosecret-crypto --save

API

File operations:

  • file.encrypt(origin, destination, keys)
  • file.decrypt(origin, destination, [key])

Stream operations:

  • stream.encrypt(data, keys)
  • stream.decrypt(data, [key])

Utils:

  • util.randomSymmetricKey(bytes)
  • util.createEncryptionKey(keys)
  • util.rsa2pkcs8(origin, destination)
  • util.rsaEncrypt(data, pkcs8_file)
  • util.rsaDecrypt(data, privateKey)
  • util.base64Encode(data)
  • util.base64Decode(data)
  • util.fileEncrypt(origin, destination, key)
  • util.fileDecrypt(origin, destination, key)
  • util.bufferEncrypt(buffer, key)
  • util.bufferDecrypt(buffer, key)

Examples

Encrypt and decrypt files

  1. const octosecret = require('octosecret-crypto')
  2. // Encrypt "octocat.gif" to "octocat.gif.octosecret" using "public.key"
  3. await octosecret.file.encrypt(`octocat.gif`, `octocat.gif.octosecret`, ['public.key'])
  4. // Decrypt "octocat.gif.octosecret" to "octocat.gif" using "id_rsa" key
  5. await octosecret.file.decrypt(`octocat.gif.octosecret`, `octocat.gif`, `~/.ssh/id_rsa`)

Encrypt and decrypt streams

  1. const octosecret = require('octosecret-crypto')
  2. // Example data
  3. const dataToEncrypt = "Super secret message"
  4. // Encrypt
  5. const encrypted = await octosecret.stream.encrypt(dataToEncrypt, ['public.key'])
  6. // Decrypt
  7. const decrypted = await octosecret.stream.decrypt(encrypted, `~/.ssh/id_rsa`)
  8. // Convert Buffer to String
  9. const decryptedText = decrypted.toString()
  10. // "Super secret message"

Check __test__ and src/example.js for more examples

License

MIT © Hugo Rodrigues