All files / src crypt.js

100% Statements 24/24
100% Branches 16/16
100% Functions 2/2
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 301x         1450x 1449x 1448x 1447x   1446x 1446x 1446x           804x 803x 802x 801x   800x 800x 800x     1x  
const crypto = require('crypto')
 
/* Encrypts a 32-byte buffer using AES-256-ECB with the given 32-byte key */
// Internal use only
function encrypt (data, key) {
  if (!Buffer.isBuffer(data)) throw new TypeError('data must be a buffer')
  if (data.length !== 32) throw new RangeError('data must be 32 bytes')
  if (!Buffer.isBuffer(key)) throw new TypeError('key must be a buffer')
  if (key.length !== 32) throw new RangeError('key must be 32 bytes')
 
  const cipher = crypto.createCipheriv('AES-256-ECB', key, null)
  cipher.setAutoPadding(false)
  return Buffer.concat([cipher.update(data), cipher.final()])
}
 
/* Decrypts a 32-byte buffer using AES-256-ECB with the given 32-byte key */
// Internal use only
function decrypt (data, key) {
  if (!Buffer.isBuffer(data)) throw new TypeError('data must be a buffer')
  if (data.length !== 32) throw new RangeError('data must be 32 bytes')
  if (!Buffer.isBuffer(key)) throw new TypeError('key must be a buffer')
  if (key.length !== 32) throw new RangeError('key must be 32 bytes')
 
  const decipher = crypto.createDecipheriv('AES-256-ECB', key, null)
  decipher.setAutoPadding(false)
  return Buffer.concat([decipher.update(data), decipher.final()])
}
 
module.exports = { encrypt, decrypt }