as3corelib Tutorial:How to Use HMAC Class in Flex

farm-fence-256x256 HMAC is the ab. For Hashed Message Authentication codes (I am not sure, may be Hash Message Authentication codes?). The usage of this mechanism is described in the RFC2104 document (http://www.faqs.org/rfcs/rfc2104.html) as below:

Providing a way to check the integrity of information transmitted over or stored in an unreliable medium is a prime necessity in the world of open computing and communications. Mechanisms that provide such integrity check based on a secret key are usually called “message authentication codes” (MAC). Typically, message authentication codes are used between two parties that share a secret key in order to validate information transmitted between these parties.

All the hash functions MD5, SHA1, SHA224, SHA256 provided by corelib are supported by this class.

Search-256x256 Demo | DownloadDownload Full Project

HMAC

1. Select your desired crypto algorithm listed in the first combo box.

2. Input the secret key used to encrypt the message into the first input box.

3. The ByteArray in the second input box is translated from the prior secret key.

4. Input your message to be encrypted into the third input box.

5. Click “calculate” button at the bottom.

6. The results will be displayed in the fourth and fifth input boxes.

Methods:

1. hash()

public static function hash(secret:String, message:String, algorithm:Object = null):String

Performs the HMAC hash algorithm using byte arrays.

secret:String The secret key
message:String The message to hash
algorithm:Object Hash object to use(default = null)

Returns:

String A string containing the hash value of message

2. hashBytes()

public static function hashBytes(secret:ByteArray, message:ByteArray, algorithm:Object = null):String

Parameters:

secret:String The secret key
message:String The message to hash
algorithm:Object Hash object to use(default = null)

Returns:

String A string containing the hash value of message

The following is full source code of HMACDemo.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Script>
		<![CDATA[
			import com.adobe.crypto.SHA256;
			import com.adobe.crypto.SHA224;
			import com.adobe.crypto.SHA1;
			import com.adobe.crypto.MD5;
			import com.adobe.crypto.HMAC;
			import mx.utils.UIDUtil;

			[Bindable]
			private var encryptedString:String = "";

			[Bindable]
			private var encryptedByteArray:String = "";

			[Bindable]
			private var algorithms:Array =
			[
				{name:"MD5"},
				{name:"SHA1"},
				{name:"SHA224"},
				{name:"SHA256"}
			];

			[Bindable]
			private var selectedAlgorithm:String = "MD5";

			private var _secret:String = "this is the key";

			[Bindable]
			private function get secret():String
			{
				// http://www.faqs.org/rfcs/rfc2104.html
				// ***********************************
				// The key for HMAC can be of any length (keys longer than B bytes are
   				// first hashed using H).  However, less than L bytes is strongly
   				// discouraged as it would decrease the security strength of the
   				// function.  Keys longer than L bytes are acceptable but the extra
   				// length would not significantly increase the function strength. (A
   				// longer key may be advisable if the randomness of the key is
   				// considered weak.)
   				// ***********************************
				return _secret;
			}

			private function set secret(value:String):void
			{
				_secret = value;

				var secretBytes:ByteArray = new ByteArray();
				secretBytes.writeMultiByte(_secret,"utf-8");
				byteArray = secretBytes;
			}

			private var _byteArray:ByteArray;

			[Bindable]
			private function get byteArray():ByteArray
			{
				if(!_byteArray)
				{
					_byteArray = new ByteArray();
					_byteArray.writeMultiByte(secret,"utf-8");
				}

				return _byteArray;
			}	

			private function set byteArray(value:ByteArray):void
			{
				_byteArray = value;
			}		

			private function byteArrayToString(byteArray:ByteArray):String
			{
				byteArray.position = 0;
				var str:String = "";
				while(byteArray.position != byteArray.length - 1)
				{
					str += byteArray.readByte().toString(16).toUpperCase()+" ";
				}
				return str;
			}				

			private function calculate():void
			{
				var algorithm:Object;

				// HMAC also support RIPEMD algorithm which is not provided
				// by corelib.
				switch(selectedAlgorithm)
				{
					case "MD5":
						algorithm = MD5;
						break;
					case "SHA1":
						algorithm = SHA1;
						break;
					case "SHA224":
						algorithm = SHA224;
						break;
					case "SHA256":
						algorithm = SHA256;
						break;
				}
				if(message.text.length > 0)
				{
					encryptedString = HMAC.hash(secret,message.text,algorithm);

					var messageBytes:ByteArray = new ByteArray();
					messageBytes.writeMultiByte(message.text,"utf-8");
					encryptedByteArray = HMAC.hashBytes(byteArray,messageBytes,algorithm);
				}
			}
		]]>
	</mx:Script>
	<mx:Panel
		title="HMAC Cryto Demo"
		layout="vertical"
		width="100%" height="100%">
		<mx:Form>
			<mx:FormItem label="algorithms">
				<mx:ComboBox change="selectedAlgorithm = event.currentTarget.selectedItem.name" dataProvider="{algorithms}" labelField="name"/>
			</mx:FormItem>
			<mx:FormItem label="secret key(String)">
				<mx:TextInput text="{secret}" change="if(event.currentTarget.text != '') secret = event.currentTarget.text;"/>
			</mx:FormItem>
			<mx:FormItem label="secret key(ByteArray)">
				<mx:TextInput text="{byteArrayToString(byteArray)}" backgroundColor="#cccccc"  editable="false"/>
			</mx:FormItem>
			<mx:FormItem label="message">
				<mx:TextInput id="message" text="this is a message to be encrypted"/>
			</mx:FormItem>
			<mx:FormItem label="encryptedString">
				<mx:TextInput text="{encryptedString}" backgroundColor="#cccccc"  editable="false"/>
			</mx:FormItem>
			<mx:FormItem label="encryptedByteArray">
				<mx:TextInput text="{encryptedByteArray}" backgroundColor="#cccccc"  editable="false"/>
			</mx:FormItem>
		</mx:Form>
		<mx:ControlBar>
			<mx:Button label="calculate" click="calculate()"/>
		</mx:ControlBar>
	</mx:Panel>
</mx:Application>

Enjoy!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

3 Responses to “as3corelib Tutorial:How to Use HMAC Class in Flex”

  1. jimy says:

    Why I get the result use this libary not equal use java hmax library ?

  2. lingxiayidu says:

    the JPEGEncoder Class is very good which I need right now in my project.Thank you verty much.

Leave a Reply