ecb.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. -- require("lockbox.init").insecure();
  2. local Array = require("lockbox.util.array");
  3. local Stream = require("lockbox.util.stream");
  4. local Queue = require("lockbox.util.queue");
  5. local ECB = {};
  6. ECB.Cipher = function()
  7. local public = {};
  8. local key;
  9. local blockCipher;
  10. local padding;
  11. local inputQueue;
  12. local outputQueue;
  13. public.setKey = function(keyBytes)
  14. key = keyBytes;
  15. return public;
  16. end
  17. public.setBlockCipher = function(cipher)
  18. blockCipher = cipher;
  19. return public;
  20. end
  21. public.setPadding = function(paddingMode)
  22. padding = paddingMode;
  23. return public;
  24. end
  25. public.init = function()
  26. inputQueue = Queue();
  27. outputQueue = Queue();
  28. return public;
  29. end
  30. public.update = function(messageStream)
  31. local byte = messageStream();
  32. while (byte ~= nil) do
  33. inputQueue.push(byte);
  34. if(inputQueue.size() >= blockCipher.blockSize) then
  35. local block = Array.readFromQueue(inputQueue, blockCipher.blockSize);
  36. block = blockCipher.encrypt(key, block);
  37. Array.writeToQueue(outputQueue, block);
  38. end
  39. byte = messageStream();
  40. end
  41. return public;
  42. end
  43. public.finish = function()
  44. local paddingStream = padding(blockCipher.blockSize, inputQueue.getHead());
  45. public.update(paddingStream);
  46. return public;
  47. end
  48. public.getOutputQueue = function()
  49. return outputQueue;
  50. end
  51. public.asHex = function()
  52. return Stream.toHex(outputQueue.pop);
  53. end
  54. public.asBytes = function()
  55. return Stream.toArray(outputQueue.pop);
  56. end
  57. public.asString = function()
  58. return Stream.toString(outputQueue.pop);
  59. end
  60. return public;
  61. end
  62. ECB.Decipher = function()
  63. local public = {};
  64. local key;
  65. local blockCipher;
  66. local padding;
  67. local inputQueue;
  68. local outputQueue;
  69. public.setKey = function(keyBytes)
  70. key = keyBytes;
  71. return public;
  72. end
  73. public.setBlockCipher = function(cipher)
  74. blockCipher = cipher;
  75. return public;
  76. end
  77. public.setPadding = function(paddingMode)
  78. padding = paddingMode;
  79. return public;
  80. end
  81. public.init = function()
  82. inputQueue = Queue();
  83. outputQueue = Queue();
  84. return public;
  85. end
  86. public.update = function(messageStream)
  87. local byte = messageStream();
  88. while (byte ~= nil) do
  89. inputQueue.push(byte);
  90. if(inputQueue.size() >= blockCipher.blockSize) then
  91. local block = Array.readFromQueue(inputQueue, blockCipher.blockSize);
  92. block = blockCipher.decrypt(key, block);
  93. Array.writeToQueue(outputQueue, block);
  94. end
  95. byte = messageStream();
  96. end
  97. return public;
  98. end
  99. public.finish = function()
  100. local paddingStream = padding(blockCipher.blockSize, inputQueue.getHead());
  101. public.update(paddingStream);
  102. return public;
  103. end
  104. public.getOutputQueue = function()
  105. return outputQueue;
  106. end
  107. public.asHex = function()
  108. return Stream.toHex(outputQueue.pop);
  109. end
  110. public.asBytes = function()
  111. return Stream.toArray(outputQueue.pop);
  112. end
  113. public.asString = function()
  114. return Stream.toString(outputQueue.pop);
  115. end
  116. return public;
  117. end
  118. return ECB;