Monday, April 28, 2014

Message Digest in Java Using Secret Key

I have made a post about message digest and secret key before. Now we will see how to generate a message digest using a secret key in Java.


First, we created the message and convert it to array of byte.


Then, we created a KeyGenerator object, the object that will generate our secret key, by supplying  an algorithm name. In this case we use AES to generate the secret key. Note that we can also create s secret key using a secret key string as we did in this post.


Next, we created a Mac object. Message Authentication Code (MAC) is a Message Digest that is generated using a secret key. There are several algorithms available for Mac. For now we choose HmacMD5. Note that the secret key and mac algorithms are completely separated, for example we use AES for secret key generation and HmacMD5 for Mac.

Then we initialize Mac with our secret key object and then update it with our original message.


We then call doFinal() method of Mac object against our original message to generate the message digest byte. Converting the byte to String using UTF8 encoding enables us to display the generated message digest to console as shown below.


0 comments:

 

©2009 Stay the Same | by TNB