cfb1.js 917 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var Buffer = require('safe-buffer').Buffer
  2. function encryptByte (self, byteParam, decrypt) {
  3. var pad
  4. var i = -1
  5. var len = 8
  6. var out = 0
  7. var bit, value
  8. while (++i < len) {
  9. pad = self._cipher.encryptBlock(self._prev)
  10. bit = (byteParam & (1 << (7 - i))) ? 0x80 : 0
  11. value = pad[0] ^ bit
  12. out += ((value & 0x80) >> (i % 8))
  13. self._prev = shiftIn(self._prev, decrypt ? bit : value)
  14. }
  15. return out
  16. }
  17. function shiftIn (buffer, value) {
  18. var len = buffer.length
  19. var i = -1
  20. var out = Buffer.allocUnsafe(buffer.length)
  21. buffer = Buffer.concat([buffer, Buffer.from([value])])
  22. while (++i < len) {
  23. out[i] = buffer[i] << 1 | buffer[i + 1] >> (7)
  24. }
  25. return out
  26. }
  27. exports.encrypt = function (self, chunk, decrypt) {
  28. var len = chunk.length
  29. var out = Buffer.allocUnsafe(len)
  30. var i = -1
  31. while (++i < len) {
  32. out[i] = encryptByte(self, chunk[i], decrypt)
  33. }
  34. return out
  35. }