javascript程式碼混淆之google closure
最近專案用到了程式碼混淆,就研究了下混淆技術
通過蒐羅資訊得知的混淆工具有: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");
高階壓縮選項會把無用的變數,函式刪除掉,函式體量過可能產生不是原意的程式碼,大家使用後請多多測試。