123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- "use strict";
- const { randomFillSync } = require("crypto");
- const crctable = new Uint32Array(256).map((t, crc) => {
- for (let j = 0; j < 8; j++) {
- if (0 !== (crc & 1)) {
- crc = (crc >>> 1) ^ 0xedb88320;
- } else {
- crc >>>= 1;
- }
- }
- return crc >>> 0;
- });
- const uMul = (a, b) => Math.imul(a, b) >>> 0;
- const crc32update = (pCrc32, bval) => {
- return crctable[(pCrc32 ^ bval) & 0xff] ^ (pCrc32 >>> 8);
- };
- const genSalt = () => {
- if ("function" === typeof randomFillSync) {
- return randomFillSync(Buffer.alloc(12));
- } else {
-
- return genSalt.node();
- }
- };
- genSalt.node = () => {
- const salt = Buffer.alloc(12);
- const len = salt.length;
- for (let i = 0; i < len; i++) salt[i] = (Math.random() * 256) & 0xff;
- return salt;
- };
- const config = {
- genSalt
- };
- function Initkeys(pw) {
- const pass = Buffer.isBuffer(pw) ? pw : Buffer.from(pw);
- this.keys = new Uint32Array([0x12345678, 0x23456789, 0x34567890]);
- for (let i = 0; i < pass.length; i++) {
- this.updateKeys(pass[i]);
- }
- }
- Initkeys.prototype.updateKeys = function (byteValue) {
- const keys = this.keys;
- keys[0] = crc32update(keys[0], byteValue);
- keys[1] += keys[0] & 0xff;
- keys[1] = uMul(keys[1], 134775813) + 1;
- keys[2] = crc32update(keys[2], keys[1] >>> 24);
- return byteValue;
- };
- Initkeys.prototype.next = function () {
- const k = (this.keys[2] | 2) >>> 0;
- return (uMul(k, k ^ 1) >> 8) & 0xff;
- };
- function make_decrypter( pwd) {
-
- const keys = new Initkeys(pwd);
-
- return function ( data) {
-
- const result = Buffer.alloc(data.length);
- let pos = 0;
-
- for (let c of data) {
-
-
- result[pos++] = keys.updateKeys(c ^ keys.next());
- }
- return result;
- };
- }
- function make_encrypter( pwd) {
-
- const keys = new Initkeys(pwd);
-
- return function ( data, result, pos = 0) {
-
- if (!result) result = Buffer.alloc(data.length);
-
- for (let c of data) {
- const k = keys.next();
- result[pos++] = c ^ k;
- keys.updateKeys(c);
- }
- return result;
- };
- }
- function decrypt( data, header, pwd) {
- if (!data || !Buffer.isBuffer(data) || data.length < 12) {
- return Buffer.alloc(0);
- }
-
- const decrypter = make_decrypter(pwd);
-
- const salt = decrypter(data.slice(0, 12));
-
- if (salt[11] !== header.crc >>> 24) {
- throw "ADM-ZIP: Wrong Password";
- }
-
- return decrypter(data.slice(12));
- }
- function _salter(data) {
- if (Buffer.isBuffer(data) && data.length >= 12) {
-
- config.genSalt = function () {
- return data.slice(0, 12);
- };
- } else if (data === "node") {
-
- config.genSalt = genSalt.node;
- } else {
-
- config.genSalt = genSalt;
- }
- }
- function encrypt( data, header, pwd, oldlike = false) {
-
- if (data == null) data = Buffer.alloc(0);
-
- if (!Buffer.isBuffer(data)) data = Buffer.from(data.toString());
-
- const encrypter = make_encrypter(pwd);
-
- const salt = config.genSalt();
- salt[11] = (header.crc >>> 24) & 0xff;
-
- if (oldlike) salt[10] = (header.crc >>> 16) & 0xff;
-
- const result = Buffer.alloc(data.length + 12);
- encrypter(salt, result);
-
- return encrypter(data, result, 12);
- }
- module.exports = { decrypt, encrypt, _salter };
|