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


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.


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)


String A string containing the hash value of message

2. hashBytes()

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


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


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="" layout="absolute">
			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;

			private var encryptedString:String = "";

			private var encryptedByteArray:String = "";

			private var algorithms:Array =

			private var selectedAlgorithm:String = "MD5";

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

			private function get secret():String
				// ***********************************
				// 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();
				byteArray = secretBytes;

			private var _byteArray:ByteArray;

			private function get byteArray():ByteArray
					_byteArray = new ByteArray();

				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.
					case "MD5":
						algorithm = MD5;
					case "SHA1":
						algorithm = SHA1;
					case "SHA224":
						algorithm = SHA224;
					case "SHA256":
						algorithm = SHA256;
				if(message.text.length > 0)
					encryptedString = HMAC.hash(secret,message.text,algorithm);

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


Share and Enjoy:
  • Digg
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to

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