JavaScript各种加密算法库crypto-js使用演示项目地址:https://code.google.com/p/crypto-js/
crypto-js库使用范例:MD5加密:
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js"></script> <script> var hash = CryptoJS.MD5("Message"); </script>
SHA1加密演示
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha1.js"></script> <script> var hash = CryptoJS.SHA1("Message"); </script>
SHA2加密演示
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js"></script> <script> var hash = CryptoJS.SHA256("Message"); </script>
Progressive Hashing
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js"></script> <script> var sha256 = CryptoJS.algo.SHA256.create(); sha256.update("Message Part 1"); sha256.update("Message Part 2"); sha256.update("Message Part 3"); var hash = sha256.finalize(); </script>
HMACKeyed-hash message authentication codes (HMAC) is a mechanism for message authentication using cryptographic hash functions.HMAC can be used in combination with any iterated cryptographic hash function.
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-md5.js"></script> <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha1.js"></script> <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha256.js"></script> <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha512.js"></script> <script> var hash = CryptoJS.HmacMD5("Message", "Secret Passphrase"); var hash = CryptoJS.HmacSHA1("Message", "Secret Passphrase"); var hash = CryptoJS.HmacSHA256("Message", "Secret Passphrase"); var hash = CryptoJS.HmacSHA512("Message", "Secret Passphrase"); </script>
Progressive HMAC Hashing
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/hmac-sha256.js"></script> <script> var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, "Secret Passphrase"); hmac.update("Message Part 1"); hmac.update("Message Part 2"); hmac.update("Message Part 3"); var hash = hmac.finalize(); </script>