vADC Docs

libHMAC.rts: Calculating HMAC hashes in TrafficScript

by on ‎03-15-2013 04:01 AM (592 Views)

The need to calculate HMAC hashes cropped up here: TrafficScript can Tweet Too.  Currently (as of Stingray version 9.1), Stingray's TrafficScript language does not contain HMAC-MD5 or HMAC-SHA1 hash functions, so it was necessary to create a library to implement these operations.

libHMAC.rts

Copy and paste the following library from GitHub, saving it as libHMAC.rts in your rules catalog:

libHMAC.rts

Import the library into your TrafficScript rule as follows:


import libHMAC.rts as hmac;




Invoke the HMAC hash functions like this:


$digestMD5 = hmac.MD5( $key, $data );


$digestSHA1 = hmac.SHA1( $key, $data );




To test the performance and correct operation of the library, you can use the following TrafficScript rule.  Assign it to an HTTP virtual server (as a request rule) and then request a page (any page) from the virtual server using your web browser.  The rule will take approximately 15 seconds to run before giving you the results.

testcase.rts

On my test system, I could achieve about 1,500 operations (slightly more with MD5 than SHA1).  Allowing CPU resource for other operations, that suggests a few 100 HMAC operations per core in a production system.  The rule runs on a single core, so if you attach it to real traffic, the effective capability will increase with the number of cores.