zipcrypto.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. "use strict";
  2. // node crypt, we use it for generate salt
  3. // eslint-disable-next-line node/no-unsupported-features/node-builtins
  4. const { randomFillSync } = require("crypto");
  5. // generate CRC32 lookup table
  6. const crctable = new Uint32Array(256).map((t, crc) => {
  7. for (let j = 0; j < 8; j++) {
  8. if (0 !== (crc & 1)) {
  9. crc = (crc >>> 1) ^ 0xedb88320;
  10. } else {
  11. crc >>>= 1;
  12. }
  13. }
  14. return crc >>> 0;
  15. });
  16. // C-style uInt32 Multiply (discards higher bits, when JS multiply discards lower bits)
  17. const uMul = (a, b) => Math.imul(a, b) >>> 0;
  18. // crc32 byte single update (actually same function is part of utils.crc32 function :) )
  19. const crc32update = (pCrc32, bval) => {
  20. return crctable[(pCrc32 ^ bval) & 0xff] ^ (pCrc32 >>> 8);
  21. };
  22. // function for generating salt for encrytion header
  23. const genSalt = () => {
  24. if ("function" === typeof randomFillSync) {
  25. return randomFillSync(Buffer.alloc(12));
  26. } else {
  27. // fallback if function is not defined
  28. return genSalt.node();
  29. }
  30. };
  31. // salt generation with node random function (mainly as fallback)
  32. genSalt.node = () => {
  33. const salt = Buffer.alloc(12);
  34. const len = salt.length;
  35. for (let i = 0; i < len; i++) salt[i] = (Math.random() * 256) & 0xff;
  36. return salt;
  37. };
  38. // general config
  39. const config = {
  40. genSalt
  41. };
  42. // Class Initkeys handles same basic ops with keys
  43. function Initkeys(pw) {
  44. const pass = Buffer.isBuffer(pw) ? pw : Buffer.from(pw);
  45. this.keys = new Uint32Array([0x12345678, 0x23456789, 0x34567890]);
  46. for (let i = 0; i < pass.length; i++) {
  47. this.updateKeys(pass[i]);
  48. }
  49. }
  50. Initkeys.prototype.updateKeys = function (byteValue) {
  51. const keys = this.keys;
  52. keys[0] = crc32update(keys[0], byteValue);
  53. keys[1] += keys[0] & 0xff;
  54. keys[1] = uMul(keys[1], 134775813) + 1;
  55. keys[2] = crc32update(keys[2], keys[1] >>> 24);
  56. return byteValue;
  57. };
  58. Initkeys.prototype.next = function () {
  59. const k = (this.keys[2] | 2) >>> 0; // key
  60. return (uMul(k, k ^ 1) >> 8) & 0xff; // decode
  61. };
  62. function make_decrypter(/*Buffer*/ pwd) {
  63. // 1. Stage initialize key
  64. const keys = new Initkeys(pwd);
  65. // return decrypter function
  66. return function (/*Buffer*/ data) {
  67. // result - we create new Buffer for results
  68. const result = Buffer.alloc(data.length);
  69. let pos = 0;
  70. // process input data
  71. for (let c of data) {
  72. //c ^= keys.next();
  73. //result[pos++] = c; // decode & Save Value
  74. result[pos++] = keys.updateKeys(c ^ keys.next()); // update keys with decoded byte
  75. }
  76. return result;
  77. };
  78. }
  79. function make_encrypter(/*Buffer*/ pwd) {
  80. // 1. Stage initialize key
  81. const keys = new Initkeys(pwd);
  82. // return encrypting function, result and pos is here so we dont have to merge buffers later
  83. return function (/*Buffer*/ data, /*Buffer*/ result, /* Number */ pos = 0) {
  84. // result - we create new Buffer for results
  85. if (!result) result = Buffer.alloc(data.length);
  86. // process input data
  87. for (let c of data) {
  88. const k = keys.next(); // save key byte
  89. result[pos++] = c ^ k; // save val
  90. keys.updateKeys(c); // update keys with decoded byte
  91. }
  92. return result;
  93. };
  94. }
  95. function decrypt(/*Buffer*/ data, /*Object*/ header, /*String, Buffer*/ pwd) {
  96. if (!data || !Buffer.isBuffer(data) || data.length < 12) {
  97. return Buffer.alloc(0);
  98. }
  99. // 1. We Initialize and generate decrypting function
  100. const decrypter = make_decrypter(pwd);
  101. // 2. decrypt salt what is always 12 bytes and is a part of file content
  102. const salt = decrypter(data.slice(0, 12));
  103. // 3. does password meet expectations
  104. if (salt[11] !== header.crc >>> 24) {
  105. throw "ADM-ZIP: Wrong Password";
  106. }
  107. // 4. decode content
  108. return decrypter(data.slice(12));
  109. }
  110. // lets add way to populate salt, NOT RECOMMENDED for production but maybe useful for testing general functionality
  111. function _salter(data) {
  112. if (Buffer.isBuffer(data) && data.length >= 12) {
  113. // be aware - currently salting buffer data is modified
  114. config.genSalt = function () {
  115. return data.slice(0, 12);
  116. };
  117. } else if (data === "node") {
  118. // test salt generation with node random function
  119. config.genSalt = genSalt.node;
  120. } else {
  121. // if value is not acceptable config gets reset.
  122. config.genSalt = genSalt;
  123. }
  124. }
  125. function encrypt(/*Buffer*/ data, /*Object*/ header, /*String, Buffer*/ pwd, /*Boolean*/ oldlike = false) {
  126. // 1. test data if data is not Buffer we make buffer from it
  127. if (data == null) data = Buffer.alloc(0);
  128. // if data is not buffer be make buffer from it
  129. if (!Buffer.isBuffer(data)) data = Buffer.from(data.toString());
  130. // 2. We Initialize and generate encrypting function
  131. const encrypter = make_encrypter(pwd);
  132. // 3. generate salt (12-bytes of random data)
  133. const salt = config.genSalt();
  134. salt[11] = (header.crc >>> 24) & 0xff;
  135. // old implementations (before PKZip 2.04g) used two byte check
  136. if (oldlike) salt[10] = (header.crc >>> 16) & 0xff;
  137. // 4. create output
  138. const result = Buffer.alloc(data.length + 12);
  139. encrypter(salt, result);
  140. // finally encode content
  141. return encrypter(data, result, 12);
  142. }
  143. module.exports = { decrypt, encrypt, _salter };