Wiki: Tutorial for MessageDigest class

TOP | static wiki | wiki | DOWNLOADS | TUTORIALS | API REFERENCE | DEMOS |

TOP | DOWNLOADS-|-TUTORIALS-|-API-REFERENCE-|-Online-Tool-|-DEMO-|-[[NODE-TOOL|Sample-Node-Tool-List.html">Wiki


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.

Getting Started

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()

for hexadecimal input

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();

progressive hashing

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();

in short

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');

supported hash algorithms and cryptographic providers

MessageDigest class supports combinations of following providers and hash algorithms.

required JavaScript sources

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>

Reference