最近项目用到了代码混淆,就研究了下混淆技术
通过搜罗信息得知的混淆工具有:google closure ,uglify2,yuicompressor
今天先拿google closure来做混淆
使用google closure做混淆需要 Java Runtime Environment version 7
1.下载工具google closure
创建文件夹 closure-test
下载 compiler.jar 文件并且保存到closure-test文件夹下
2.创建javascript文件
在文件夹closure-test下创建文件test.js,内容如下:
// 这是注释
let a = 6
function hello(userName) {
alert('Hello, ' + userName);
}
hello('fawa');
function test(){
alert("hello fawa");
}
然后保存文件。
3. 在文件夹closure-test下执行命令:
java -jar closure-compiler.jar --js test.js --js_output_file test-compiled.js
此命令会在文件夹下创建文件test-compiled.js, 内容如下:
var a=6;function hello(b){alert("Hello, "+b)}hello("fawa");function test(){alert("hello fawa")};
注意,编译器去除了注释、空白和不必要的分号。编译器还将参数名userName替换为较短的名称b。结果是一个更小的javascript文件。
另外:使用高级选项可生成更精简的代码
运行如下命令:
java -jar closure-compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --js test.js --js_output_file test-compiled.js
生成文件test-compiled.js 内容如下:
alert("Hello, fawa");
高级压缩选项会把无用的变量,函数删除掉,函数体量过可能产生不是原意的代码,大家使用后请多多测试。
网友评论