最近項(xiàng)目用到了代碼混淆仓手,就研究了下混淆技術(shù)
通過搜羅信息得知的混淆工具有:google closure? 玻淑,uglify2,yuicompressor
今天先拿google closure來做混淆
使用google closure做混淆需要?Java Runtime Environment version 7
1.下載工具google closure?
? ? 創(chuàng)建文件夾 closure-test
? ? 下載?compiler.jar?文件并且保存到closure-test文件夾下
2.創(chuàng)建javascript文件?
? ? 在文件夾closure-test下創(chuàng)建文件test.js添坊,內(nèi)容如下:
// 這是注釋
let a = 6
function hello(userName) {
? alert('Hello, ' + userName);
}
hello('fawa');
function test(){
alert("hello fawa");
}
? ? 然后保存文件箫锤。
3.? ?在文件夾closure-test下執(zhí)行命令:
java -jar? closure-compiler.jar? --js test.js --js_output_file test-compiled.js
? ? 此命令會(huì)在文件夾下創(chuàng)建文件test-compiled.js, 內(nèi)容如下:
var a=6;function hello(b){alert("Hello, "+b)}hello("fawa");function test(){alert("hello fawa")};
注意,編譯器去除了注釋谚攒、空白和不必要的分號(hào)。編譯器還將參數(shù)名userName替換為較短的名稱b。結(jié)果是一個(gè)更小的javascript文件岔擂。
另外:使用高級(jí)選項(xiàng)可生成更精簡(jiǎn)的代碼
運(yùn)行如下命令:
java -jar? closure-compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --js test.js --js_output_file test-compiled.js
生成文件test-compiled.js 內(nèi)容如下:
alert("Hello, fawa");
高級(jí)壓縮選項(xiàng)會(huì)把無用的變量乱灵,函數(shù)刪除掉,函數(shù)體量過可能產(chǎn)生不是原意的代碼痛倚,大家使用后請(qǐng)多多測(cè)試。