webpack打包使用eval加密混淆深度压缩

webpack打包使用eval加密混淆深度压缩

1.编写webpack自定义插件

const fs = require("fs");
const path = require("path");
const zlib = require("zlib");

/**
 * 自定义代码压缩加密webpack插件
 */
class EvalCompressPlugin {
   
    constructor({
     file, packMinify = false, base62Encode = false, shrinkVariables = false,gzip = false }) {
   
        // 判断是否为空
        if (!file) {
   
            throw new Error("file 参数不能为空");
        }
        this.file = file;
        // 是否使用packer的minify压缩
        this.packMinify = packMinify;
        // 是否使用base62压缩
        this.base62Encode = base62Encode;
        // 是否压缩变量名
        this.shrinkVariables = shrinkVariables;
        // 是否进行gzip压缩
        this.gzip = gzip;
    }

    apply(compiler) {
   
        compiler.hooks.done.tap("EvalCompressPlugin", async stats => {
   
            this.eval();
        });
    }

    eval() {
   
        let Packer = require("./JSPacker");
        var packer = new Packer();
        let fileContent = fs.readFileSync(this.file, "utf8");
        // eval加密混淆
        var newFileContent = packer.pack(fileContent, this.packMinify,this.base62Encode, this.shrinkVariables);
        fs.writeFileSync(this.file, newFileContent, "utf8");
        // 进行gzip压缩
        if(this.gzip) this._gzip();
    }

    _gzip() {
   
        // 创建转化流
        let gzip = zlib.createGzip();
        // 创建可读流
        let rs = fs.createReadStream(this.file);
        // 创建可写流
        let ws = fs.createWriteStream(`${
     this.file}.gz`);
        // 实现转化
        rs.pipe(gzip).pipe(ws);
    }
}

module.exports = EvalCompressPlugin;

2.webpack添加plugins

new EvalCompressPlugin({
   
     file:path.resolve(__dirname, "build/BuildOutput.js"),
     packMinify:true,
     base62Encode:true,
     shrinkVariables:true,
     gzip:true
})

3.JSPacker加密混淆工具(packer在线地址)

// JSPacker.js文件
var base2 = {
   
    name: "base2",
    version: "1.0.1(pre)",
    exports: "Base, Package, Abstract, Module, Enumerable, Map, Collection, RegGrp, " + "assert, assertArity, assertType, " + "assignID, copy, counter, detect, extend, forEach, format, instanceOf, match, rescape, slice, trim, " + "I, K, Undefined, Null, True, False, bind, delegate, flip, not, partial, unbind",
    global: this,
    namespace: "var global=base2.global;function base(o,a){return o.base.apply(o,a)};",
    detect: new (function (_) {
   
        var global = _;
        var jscript /*@cc_on=@_jscript_version@*/;
        if (_.navigator) {
   
            var element = document.createElement("span");
            var userAgent = navigator.platform + " " + navigator.userAgent;
            if (!jscript) userAgent = userAgent.replace(/MSIE\s[\d.]+/, "");
            userAgent = userAgent.replace(/([a-z])[\s\/](\d)/gi, "$1$2");
        }
        return function (a) {
   
            var r = false;
            var b = a.charAt(0) == "!";
            if (b) a = a.slice(1);
            if (a.charAt(0) == "(") {
   
                try {
   
                    eval("r=!!" + a);
                } catch (error) {
   }
            } else {
   
                r = new RegExp("(" + a + ")", "i").test(userAgent);
            }
            return !!(b ^ r);
        };
    })(this)
};
new (function (_) {
   
    var detect = base2.detect;
    var slice =
        Array.slice ||
        (function (b) {
   
            return function (a) {
   
                return b.apply(a, b.call(arguments, 1));
            };
        })(Array.prototype.slice);
    var Undefined = K(),
        Null = K(null),
        True = K(true),
        False = K(false);
    var _0 = /%([1-9])/g;
    var _1 = /^\s\s*/;
    var _2 = /\s\s*$/;
    var _3 = /([\/()[\]{}|*+-.,^$?\\])/g;
    var _4 = /eval/.test(detect) ? /\bbase\s*\(/ : /.*/;
    var _5 = ["constructor", "toString", "valueOf"];
    var _6 = String(new RegExp());
    var _7 = 1;
    _8();
    eval(base2.namespace);
    var _9 = function (a, b) {
   
        base2.__prototyping = true;
        var c = new this();
        extend(c, a);
        delete base2.__prototyping;
        var d = c.constructor;
        function _10() {
   
            if (!base2.__prototyping) {
   
                if (this.constructor == arguments.callee || this.__constructing) {
   
                    this.__constructing = true;
                    d.apply(this, arguments);
                    delete this.__constructing;
                } else {
   
                    return extend(arguments[0], c);
                }
            }
            return this;
        }
        c.constructor = _10;
        for (var i in Base) _10[i] = this[i];
        _10.toString = K(String(d));
        _10.ancestor = this;
        _10.base = Undefined;
        _10.init = Undefined;
        extend(_10, b);
        _10.prototype = c;
        _10.init();
        return _10;
    };
    var Base = _9.call(
        Object,
        {
   
            constructor: function () {
   
                if (arguments.length > 0) {
   
                    this.extend(arguments[0]);
                }
            },
            base: function () {
   },
            extend: delegate(extend)
        },
        (Base = {
   
            ancestorOf: delegate(_11),
            extend: _9,
            forEach: delegate(_8),
            implement: function (a) {
   
                if (typeof a == "function") {
   
                    if (_11(Base, a)) {
   
                        a(this.prototype);
                    }
                } else {
   
                    extend(this.prototype, a);
                }
                return this;
            }
        })
    );
    var Package = Base.extend({
   
        constructor: function (d, e) {
   
            this.extend(e);
            if (this.init) this.init();
            if (this.name != "base2") {
   
                if (!this.parent) this.parent = base2;
                this.parent.addName(this.name, this);
                this.namespace = format("var %1=%2;", this.name, String(this).slice(1, -1));
            }
            var f = /[^\s,]+/g;
            if (d) {
   
                d.imports = Array2.reduce(
                    this.imports.match(f),
                    function (a, b) {
   
                        eval("var ns=base2." + b);
                        assert(ns, format("Package not found: '%1'.", b));
                        return (a += ns.namespace);
                    },
                    base2.namespace + JavaScript.namespace
                );
                d.exports = Array2.reduce(
                    this.exports.match(f),
                    function (a, b) {
   
                        var c = this.name + "." + b;
                        this.namespace += "var " + b + "=" + c + ";";
                        return (a += "if(!" + c + ")" + c + "=" + b + ";");
                    },
                    "",
                    this
                );
            }
        },
        exports: "",
        imports: "",
        name: "",
        namespace: "",
        parent: null,
        addName: function (a, b) {
   
            if (!this[a]) {
   
                this[a] = b;
                this.exports += ", " + a;
                this.namespace += format("var %1=%2.%1;", a, this.name);
            }
        },
        addPackage: function (a) {
   
            this.addName(a, new Package(null, {
    name: a, parent: this }));
        },
        toString: function () {
   
            return format("[%1]", this.parent ? String(this.parent).slice(1, -1) + "." + this.name : this.name);
        }
    });
    var Abstract = Base.extend({
   
        constructor: function () {
   
            throw new TypeError("Class cannot be instantiated.");
        }
    });
    var Module = Abstract.extend(null, {
   
        extend: function (a, b) {
   
            var c = this.base();
            _12(c, this);
            c.implement(a);
            extend(c, b);
            c.init();
            return c;
        },
        implement: function (c) {
   
            var d = this;
            if (typeof c == "function") {
   
                d.base(c);
                if (_11(Module, c)) {
   
                    _12(d, c);
                }
            } else {
   
                var e = {
   };
                _8(Object, c, function (a, b) {
   
                    if (b.charAt(0) == "@") {
   
                        if (detect(b.slice(1))) {
   
                            forEach(a, arguments.callee);
                        }
                    } else if (!Module[b] && typeof a == "function" && a.call) {
   
                        function _13() {
   
                            return d[b].apply(d, [this].concat(slice(arguments)));
                        }
                        _13.__base = _4.test(a);
                        e[b] = _13;
                    }
                });
                extend(d.prototype, e);
                extend(d, c);
            }
            return d;
        }
    });
    function _12(a, b) {
   
        for (var c in b) {
   
            var method = b[c];
            if (!Module[c] && typeof method == "function" && method.call) {
   
                a[c] = method;
            }
        }
    }
    var Enumerable = Module.extend(
        {
   
            every: function (c, d, e) {
   
                var f = true;
                try {
   
                    this.forEach(c, function (a, b) {
   
                        f = d.call(e, a, b, c);
                        if (!f) throw 
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值