1.0.0 • Published 7 years ago

text-crypto v1.0.0

Weekly downloads
-
License
-
Repository
github
Last release
7 years ago

Text Crypto Build Status

A Node.js module to encrypt and decrypt text.

Features

  • Simple API - there are only two functions (encrypt and decrypt).
  • Multiple calls to encrypt with the same key and plaintext will produce a different ciphertext.
  • The string produced by encrypt will always contain a HMAC and a initialization vector.
  • Cipher algorithm: AES256
  • HMAC message digest algorithm: SHA256

Install

$ npm install text-crypto

Example

const TextCrypto = require('text-crypto')

const key = 'a long random key'
const plaintext = 'a plain text message'

const ciphertext = TextCrypto.encrypt(key, plaintext)
const decryptedText = TextCrypto.decrypt(key, ciphertext)

console.log(ciphertext)
console.log(decryptedText) // 'a plain text message'
console.log(plaintext === decryptedText) // true

API

TextCrypto.encrypt(key, plaintext)

Returns a string - the ciphertext of the plaintext.

Invoking encrypt with the same key and plaintext will produce a different ciphertext each time.

key must contain sixteen or more characters, otherwise an error is thrown.

An error is thrown if either argument key or plaintext is not a string.

TextCrypto.decrypt(key, ciphertext)

Returns the decrypted plaintext of the ciphertext.

key must contain sixteen or more characters, otherwise an error is thrown.

An error is thrown if either argument key or plaintext is not a string.

Wrapping decrypt in a try catch block is advised. If the key is incorrect or the ciphertext has been modified in any way then decrypt will throw an ERR_INVALID_HMAC error.

const key = 'a long random key'
const plaintext = 'a plain text message'

const ciphertext = TextCrypto.encrypt(key, plaintext)

let decryptedText
try {
  decryptedText = TextCrypto.decrypt(key, ciphertext + 'a modification')
} catch (err) {
  console.log(err) // { code: 'ERR_INVALID_HMAC', message: 'invalid HMAC' }
}

Tests

Run the unit tests.

$ npm test

License

MIT. Copyright (c) Shane Bloomer.

1.0.0

7 years ago