All files / src/encryption cipherAes256Cbc.ts

44.44% Statements 4/9
100% Branches 0/0
66.67% Functions 2/3
44.44% Lines 4/9

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  1x               1x                         1x 1x    
// eslint-disable-next-line import/no-nodejs-modules
import { createCipheriv, createDecipheriv } from 'crypto'
import { Cipher } from './cryptoInterfaces'
 
// TODO: Create a WebCrypto implementation for browser usage
 
class NodeCryptoAes256CbcCipher implements Cipher {
  async encrypt(key: NodeJS.TypedArray, iv: NodeJS.TypedArray, data: NodeJS.TypedArray): 
    Promise<Buffer> {
    const cipher = createCipheriv('aes-256-cbc', key, iv)
    const result = Buffer.concat([cipher.update(data), cipher.final()])
    return Promise.resolve(result)
  }
 
  async decrypt(key: NodeJS.TypedArray, iv: NodeJS.TypedArray, data: NodeJS.TypedArray): 
    Promise<Buffer> {
    const cipher = createDecipheriv('aes-256-cbc', key, iv)
    const result = Buffer.concat([cipher.update(data), cipher.final()])
    return Promise.resolve(result)
  }
}
 
export function createCipherAes256Cbc(): Cipher {
  return new NodeCryptoAes256CbcCipher()
}