-
-
Notifications
You must be signed in to change notification settings - Fork 644
Tutorial for MessageDigest class
TOP | Wiki | DOWNLOADS | TUTORIALS | API REFERENCE | Online Tool | DEMO | NODE TOOL
The KJUR.crypto.MessageDigest class is a very similar to Java JCE [java.security.MessageDigest] (http://docs.oracle.com/javase/7/docs/api/index.html?java/security/MessageDigest.html) class for cryptographic hash algorithm calculation. So it's easy to learn.
Here is a basic example for 'SHA1' hash calculation.
// initialize
var md = new KJUR.crypto.MessageDigest({"alg": "sha1", "prov": "cryptojs"});
// update data
md.updateString('aaa')
// SHA1 hash result of string aaa which will be 7e240de74fb1ed08fa08d38063f6a6a91462a815
var hashValueHex = md.digest()
You can also update a hexadecimal string as hash input.
var md = new KJUR.crypto.MessageDigest({"alg": "sha1", "prov": "cryptojs"});
md.updateHex('5f6de0');
var hashValueHex = md.digest();
The 'updateHex' and 'updateString' method can be called one or more times.
var md = new KJUR.crypto.MessageDigest({"alg": "sha1", "prov": "cryptojs"});
md.updateHex('9a3bcd345793173');
md.updateHex('5f6de0');
md.updateString('abcdefg');
md.updateHex('01341571fg56ab');
md.updateString('apple');
var hashValueHex = md.digest();
To update and digest in a one method you can use 'digestHex' or 'digestString' method.
var md = new KJUR.crypto.MessageDigest({"alg": "sha1", "prov": "cryptojs"});
var hashValueHex = md.digestHex('1afcdd');
var md = new KJUR.crypto.MessageDigest({"alg": "sha1", "prov": "cryptojs"});
var hashValueHex = md.digestString('orange');
MessageDigest class supports combinations of following providers and hash algorithms.
- cryptojs - md5
- cryptojs - sha1
- cryptojs - sha224
- cryptojs - sha256
- cryptojs - sha384
- cryptojs - sha512
- cryptojs - ripemd160
- sjcl - sha256 (NEW since crypto 1.0.4, sjcl only supports sha256)
To use MessageDigest class following JavaScript codes are required with this order:
// core-min.js required in case cryptojs provider is used
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/core-min.js"></script>
// x64-core.js required in case sha384 and sha512 algorithm of cryptojs is used
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/x64-core.js"></script>
// sha256.js is required if sha224 of cryptojs is used
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/sha256.js"></script>
// sha512.js is required if sha384 of cryptojs is used
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/sha512.js"></script>
// these depends on algorithm to be used
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/md5.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/sha1.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/sha224.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/sha384.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/ripemd160.js"></script>
// for sha256 of sjcl provider
<script src="http://bitwiseshiftleft.github.io/sjcl/sjcl.js"></script>
// for crypto-1.0.js
<script src="http://yui.yahooapis.com/2.9.0/build/yahoo/yahoo-min.js"></script>
<script src="http://kjur.github.io/jsrsasign/crypto-1.0.js"></script>
- CryptoJS Library - progressive hashing supported crypto library
- SJCL - Stanford JavaScript Crypto Library which also supports progressive hashing