1. 为什么抉择应用JavaScript来复现算法

JS实现的算法,能够很不便地被任何语言调用

2. CryptoJS中音讯摘要算法的应用

CryptoJS.MD5(message);CryptoJS.HmacMD5(message, key);CryptoJS.SHA1(message);CryptoJS.HmacSHA1(message, key);CryptoJS.SHA256(message);CryptoJS.HmacSHA256(message, key);CryptoJS.SHA512(message);CryptoJS.HmacSHA512(message, key);CryptoJS.SHA3('xiaojianbang', {outputLength: 256})

3. 音讯摘要算法的其余调用模式

SHA256var hasher = CryptoJS.algo.SHA256.create();hasher.reset();hasher.update('message');hasher.update(wordArray);var hash = hasher.finalize();var hash = hasher.finalize('message');var hash = hasher.finalize(wordArray);HmacSHA256var hmacHasher = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);hmacHasher.reset();hmacHasher.update('message');hmacHasher.update(wordArray);var hmac = hmacHasher.finalize();var hmac = hmacHasher.finalize('message');var hmac = hmacHasher.finalize(wordArray);

4. CryptoJS(字符串解析)

// -------------------------------------- string转wordArray console.log("-----------------------------------------------");var md5_str= "hengdin.com";var utf8_str= md5_str;var hex_str = "68656e6764696e2e636f6d";var base64_str = "aGVuZ2Rpbi5jb20==";var utf8_wordarray = CryptoJS.enc.Utf8.parse(utf8_str);var hex_wordarray= CryptoJS.enc.Hex.parse(hex_str);var base64_wordarray = CryptoJS.enc.Base64.parse(base64_str);console.log(utf8_wordarray);console.log(hex_wordarray);console.log(base64_wordarray);// { words: [ 1751477863, 1684631086, 1668246784 ], sigBytes: 11 }// 解析-------------------------------------- wordArray转stringconsole.log("-----------------------1111------------------------");// 形式一:// 默认应用hex 编码 console.log(utf8_wordarray+'');console.log(hex_wordarray+'');console.log(base64_wordarray+'');// 形式二:console.log(utf8_wordarray.toString(CryptoJS.enc.Utf8));console.log(hex_wordarray.toString(CryptoJS.enc.Hex));console.log(base64_wordarray.toString(CryptoJS.enc.Base64));// 形式三:console.log(CryptoJS.enc.Utf8.stringify(utf8_wordarray));console.log(CryptoJS.enc.Hex.stringify(hex_wordarray));console.log(CryptoJS.enc.Base64.stringify(base64_wordarray));// Hex编码转Base64编码 :先转成 wordarray  再转成 base64 strvar hex_str = "68656e6764696e2e636f6d"; var wordArray__ = CryptoJS.enc.Hex.parse(hex_str);var base_str__= CryptoJS.enc.Base64.stringify(wordArray__);console.log(base_str__);// 加密的参数  能够是String 类型 ,也能够是wordarray 类型 string类型的数据,将应用默认的Utf8.parse来解析var str = "hendi";var str_wordarray  = CryptoJS.enc.Utf8.parse(str);console.log(CryptoJS.MD5(str).toString());console.log(CryptoJS.MD5(str_wordarray).toString());