Objective
Crypto is a core module in Nodejs, although there is now a Crypto global object () in the Advanced browser, and its use in Nodejs is different from the web side.
Web-side Use Cryptojs
- Foreign download slower, recommended domestic mirror
Link: Https://pan.baidu.com/s/1jKgTAZW Password: 0HPI
- How to use:
- Using encryption methods in Nodejs
javascript var crypto = require(‘crypto‘); var PRIVATE_KEY = ‘123‘; var hmac = crypto.createHmac(‘sha1‘, private_key); hmac.write(url); hmac.end(); var key = hmac.read().toString(‘base64‘); key = encodeURIComponent(key);
- Switch to web-side use
html <!-- html中代码 --> <!-- 根据上面代码使用引入相应的js --> <script src="cryptojs/rollups/hmac-sha1.js"></script> <script src="cryptojs/components/enc-base64-min.js"></script>
javascript // js中代码 var PRIVATE_KEY = ‘123‘; var hash = CryptoJS.HmacSHA1(url, private_key); var base64 = hash.toString(CryptoJS.enc.Base64); var key = encodeURIComponent(base64);
- Let's try it yourself!
The use of crypto in the Web