Skip to content

Latest commit

 

History

History
53 lines (38 loc) · 1.7 KB

File metadata and controls

53 lines (38 loc) · 1.7 KB

cipher-base

NPM Package Build Status

js-standard-style

Abstract base class to inherit from if you want to create streams implementing the same API as node crypto Cipher or Decipher (for Hash check crypto-browserify/hash-base).

Example

const CipherBase = require('cipher-base')
const inherits = require('inherits')

// our cipher will apply XOR 0x42 to every byte
function MyCipher () {
  CipherBase.call(this, true) // for Deciper you need pass `false`
}

inherits(MyCipher, CipherBase)

MyCipher.prototype._isAuthenticatedMode = function () {
  return false
}

MyCipher.prototype._setAutoPadding = function (ap) {}
MyCipher.prototype._setAAD = function (aadbuf) {}

MyCipher.prototype._update = function (data) {
  const result = Buffer.allocUnsafe(data.length)
  for (let i = 0; i < data.length; ++i) result[i] = data[i] ^ 0x42
  return result
}

MyCipher.prototype._final = function () {
  return Buffer.allocUnsafe(0)
}

const data = Buffer.from([ 0x00, 0x42 ])
const cipher = new MyCipher()
console.log(Buffer.concat([cipher.update(data), cipher.final()]))
// => <Buffer 42 00>

LICENSE

MIT