而我之前玩的就是ASP代码。有一天我需要用到对象存储,但是要用对象存储就需要对请求URL进行加密。大家都知道ASP已经不更新了,要达到这样的加密根本就不行。因此我就找到了前端加密库Crypto-JS。加密效果杠杠的,顺利解决加密任务。
使用方法:
使用前先引用加密库Crypto-JS
/** * message :需要加密的字符串或数组 * miwen :已经加密的密文 * key :自己设置的密钥 */ var hash = CryptoJS.MD5(message); var hash = CryptoJS.SHA1(message); var hash = CryptoJS.SHA256('message'); var hmac = CryptoJS.HmacMD5(message, key); var hmac = CryptoJS.HmacSHA1(message, key); var hmac = CryptoJS.HmacSHA256(message, key); //base64加密 var base64String = CryptoJS.enc.Base64.stringify(message); //base64解密 var wordArray = CryptoJS.enc.Base64.parse(miwen);
对象存储使用示例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="crypto-js.js" ></script> </head> <body> <p id="demo"></p> <script> function getSignature(accessId, secretKey, verb, ctMd5, ctType, date, resource) { var message = verb + '\n' + ctMd5 + '\n' + ctType + '\n' + date + '\n' + '' + resource; var secretMessage = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(message, secretKey)); //secretMessage = encodeURIComponent(secretMessage); //secretMessage = secretMessage.replace('\+', '%20'); return secretMessage; } var accessId = 'fbfa832dee2b4347869f726c6cacfcf7', secretKey= '495567f8becd48765719371335f9d472', verb = 'GET', ctMd5 = '', ctType = '', date = 'Wed, 9 Aug 2017 12:42:00 GMT', resource = '/'; var signature = getSignature(accessId, secretKey, verb, ctMd5, ctType, date, resource); document.getElementById("demo").innerHTML = signature; </script> </body> </html>