# HMAC request validation

**URL:** <https://discuss.google.dev/t/hmac-request-validation/6031>\
**Category:** Apigee\
**Tags:** api-runtime\
**Created:** [October 6, 2014, 9:37am UTC](https://discuss.google.dev/t/hmac-request-validation/6031 "2014-10-06T09:37:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [October 6, 2014, 9:37am UTC](https://discuss.google.dev/t/hmac-request-validation/6031/1 "2014-10-06T09:37:06Z")

</div>

Hi,

Is it possible to use HMAC (or alternative hash) to validate the request has not been tampered with?

Some of our requests are based on the post payload so I would like to be able to hash the payload with a provided secret and confirm that within apigee when the request is received. Is this possible?

Thanks

Carl

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [October 6, 2014, 4:01pm UTC](https://discuss.google.dev/t/hmac-request-validation/6031/2 "2014-10-06T16:01:44Z")

</div>

There is no built-in policy for HMAC validation, however if there is a Javascript/Node library that you could use, you may be able to do it that way. If you are using Apigee Edge, both Python and Java callouts are available as well.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [October 6, 2014, 8:13pm UTC](https://discuss.google.dev/t/hmac-request-validation/6031/3 "2014-10-06T20:13:21Z")

</div>

Hi Michael,

Thanks for your reply. You mention a javascript library, is this something that exists is out in the community or are you referring to the fact it is possible to achieve this with a custom library?

Cheers

Carl

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [October 6, 2014, 8:24pm UTC](https://discuss.google.dev/t/hmac-request-validation/6031/4 "2014-10-06T20:24:53Z")

</div>

I meant to say that it is possible if a suitable javascript library exists for your purposes. A Google search of “HMAC javascript” gives some links to possible candidates but I’m not sure which or any would be of use to you.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [June 1, 2015, 8:48pm UTC](https://discuss.google.dev/t/hmac-request-validation/6031/5 "2015-06-01T20:48:02Z")

</div>

Can you please elaborate on this more. If I were to use crypto-js or some other javascript library to do HMAC computation how would I import that library into my proxy such that it is callable from a javascript policy. In javaScript policy I can only specific one js file which is my file. How do I link other files / libraries there?

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [June 1, 2015, 11:46pm UTC](https://discuss.google.dev/t/hmac-request-validation/6031/6 "2015-06-01T23:46:17Z")

</div>

[@Gagan Arora](https://community.apigee.com/users/1661/gaganarora.html) , Refer this [http://apigee.com/docs/api-services/reference/javascript-policy#elements-includeurl](http://apigee.com/docs/api-services/reference/javascript-policy#elements-includeurl) to understand how you can add other Js files as dependent files .

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [June 2, 2015, 5:24am UTC](https://discuss.google.dev/t/hmac-request-validation/6031/7 "2015-06-02T05:24:37Z")

</div>

[@Carl Lambert](https://community.apigee.com/users/164/thetownfoolgmailcom.html), it will be great if you can update the thread on the library you ended up picking. I am curious, and am sure few others will be.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [June 2, 2015, 5:54am UTC](https://discuss.google.dev/t/hmac-request-validation/6031/8 "2015-06-02T05:54:02Z")

</div>

CryptoJS is a very usable library for this function ([code.google.com/p/crypto-js](http://code.google.com/p/crypto-js)).

The follow JSC snippet demonstrates who to validate a request payload:

```auto
var request = decodeURIComponent(context.getVariable("request.content"));
if(request) request = request.trim();

var signStartIndex = request.indexOf("&signature=");
var contentPayload = request.substring(5,signStartIndex);

contentPayload = contentPayload.replace(/\+/g,' ');

var givenSign = request.substring( (signStartIndex + 11),request.length);
givenSign = givenSign.trim();

var hmacKey = context.getVariable("hmac.key");
if(hmacKey) hmacKey = hmacKey.trim();
var hashSign = CryptoJS.HmacSHA256(contentPayload,hmacKey );
var hashBase64Sign = CryptoJS.enc.Base64.stringify(hashSign);

var hmacValidation = false;
if(hashBase64Sign === givenSign){
	hmacValidation = true;
}

context.setVariable("hmac.validation", hmacValidation);

```

Note that this policy presumes that the decryption key was inserted into context as a variable “hmac.key”. The result of this is a variable hmac.validation being set into context. You can then raise a fault in a later step if that is the desired result.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [June 4, 2015, 2:13pm UTC](https://discuss.google.dev/t/hmac-request-validation/6031/9 "2015-06-04T14:13:51Z")

</div>

Signing just the payload still leaves the server vulnerable to replay attacks. As a result most implementations use current-time in the string-to-sign. By checking for current-time you can make a signature invalid after XX minutes. Also you should always HMAC using the secret, not the public key.

I have posted my implementation that uses the above and sjcl [https://crypto.stanford.edu/sjcl/](https://crypto.stanford.edu/sjcl/)

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [June 4, 2015, 2:17pm UTC](https://discuss.google.dev/t/hmac-request-validation/6031/10 "2015-06-04T14:17:39Z")

</div>

Implementation that uses - [https://crypto.stanford.edu/sjcl/](https://crypto.stanford.edu/sjcl/)

string to Sign is defined at the top and can be changed based upon agreement between client and server. Variable extraction and key verification needs to be done prior to this script

```auto
function validDate(){
    context.setVariable("Auth.stringToSign", "Auth.dateSigned,Auth.apikey,Auth.nonce,Auth.salt,request.content");

    var validMilliseconds = 300000;
    var dtstr = context.getVariable("Auth.dateSigned"); // format yyyyMMddHHmmss UTC
    var yy = parseInt(dtstr.substr(0,4));
    var MM = parseInt(dtstr.substr(4,2))-1;
    var dd = parseInt(dtstr.substr(6,2));
    var HH = parseInt(dtstr.substr(8,2));
    var mm = parseInt(dtstr.substr(10,2));
    var ss = parseInt(dtstr.substr(12,2));

    var dt = new Date(Date.UTC(yy,MM,dd,HH,mm,ss));
    var dtnow = new Date();
    return ((Math.abs(dtnow - dt)) < validMilliseconds);
}

(function validate(validDate) {
    var stringToSign = "";
    var computedSig = "";
    var signatureMatch = false;

    if (validDate)
    {
        var contextVars = context.getVariable("Auth.stringToSign").split(",");

        var i = 0;
        for (i = 0; i < contextVars.length; i++) {
            var str = context.getVariable(contextVars[i]);
            if ( str !== null ) {
                stringToSign += str;
            }
        }

        var key = context.getVariable("verifyapikey.verifyApiKey.client_secret");
        var keyBits = sjcl.codec.utf8String.toBits(key);
        var out = (new sjcl.misc.hmac(keyBits, sjcl.hash.sha256)).mac(stringToSign);
        computedSig = sjcl.codec.hex.fromBits(out);
    }

    if (computedSig !== null && computedSig.length > 0)
    {
        var requestSig = context.getVariable("Auth.signature");
        // modified base64 i.e. + -> - , / -> _, remove trailing pad
        computedSig = computedSig.replace("+","-");
        computedSig = computedSig.replace("/","_");
        computedSig = computedSig.replace(/(=+$)/g, "");

        signatureMatch = (computedSig.valueOf() === requestSig.valueOf());
    }

    context.setVariable("hmacjs.signatureMatch", signatureMatch);

})(validDate());

```

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [June 5, 2015, 3:13pm UTC](https://discuss.google.dev/t/hmac-request-validation/6031/11 "2015-06-05T15:13:33Z")

</div>

Sweet! And [@Gagan Arora](https://community.apigee.com/users/1661/gaganarora.html), you would be including that sjcl library like this, correct?:

```auto
<Javascript name='Javascript-verifyHmac' timeLimit='200' >
  <IncludeURL>jsc://sjcl.js</IncludeURL> 
  <ResourceURL>jsc://yourcodehere.js</ResourceURL>
</Javascript>

```

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [June 5, 2015, 4:07pm UTC](https://discuss.google.dev/t/hmac-request-validation/6031/12 "2015-06-05T16:07:59Z")

</div>

Yes. I used my policy looks like this:

```auto
<Javascript timeLimit="200" name="hmac"> 
  <DisplayName>validateHmac</DisplayName> 
  <ResourceURL>jsc://hmac.js</ResourceURL> 
  <IncludeURL>jsc://sjcl.js</IncludeURL> 
</Javascript>

```

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [September 13, 2016, 5:26pm UTC](https://discuss.google.dev/t/hmac-request-validation/6031/13 "2016-09-13T17:26:01Z")

</div>

An update to an old question. There is no built-in policy for HMAC calculation.

However, there is a Java callout that does this. It will probably perform better than the Javascript-based option described in the other answers here. If you don’t run at high concurrency, you probably don’t care.

Find it here: [https://github.com/apigee/iloveapis2015-hmac-httpsignature](https://github.com/apigee/iloveapis2015-hmac-httpsignature)

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [November 29, 2019, 1:50am UTC](https://discuss.google.dev/t/hmac-request-validation/6031/14 "2019-11-29T01:50:21Z")

</div>

besides the “sjcl.js”, is there any other recommending library can be used for encrypting?

---

<div class="post-metadata">

**Author:** ![dchiesa1](https://d3byx8b92xw3lq.cloudfront.net/user_avatar/discuss.google.dev/dchiesa1/32/15641_2.png) [@dchiesa1](https://discuss.google.dev/u/dchiesa1)\
**Post date:** [December 2, 2019, 7:51pm UTC](https://discuss.google.dev/t/hmac-request-validation/6031/15 "2019-12-02T19:51:23Z")

</div>

If you are talking about HMAC, then yes, there is a Java callout available here:

[https://github.com/apigee/iloveapis2015-hmac-httpsignature](https://github.com/apigee/iloveapis2015-hmac-httpsignature)

If that doesn’t satisfy, then it sounds like you have a new question. If so, please POST a new question.

If you think it’s appropriate, you can reference the URL to THIS question in your new question.

 ![9473-ask-a-question.png](https://d2yds90mtvelsl.cloudfront.net/original/2X/8/892eba97da31e3e95f5f1d4a5e4da74056ce2d99.png)

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [December 4, 2019, 9:30am UTC](https://discuss.google.dev/t/hmac-request-validation/6031/16 "2019-12-04T09:30:45Z")

</div>

The variable “contextVars” is not a flow variable, but the code behind it try to get it from flow variable.It is really runnable?

```auto
var str = context.getVariable(contextVars[i]);

```

---

<div class="post-metadata">

**Author:** ![dchiesa1](https://d3byx8b92xw3lq.cloudfront.net/user_avatar/discuss.google.dev/dchiesa1/32/15641_2.png) [@dchiesa1](https://discuss.google.dev/u/dchiesa1)\
**Post date:** [December 4, 2019, 4:20pm UTC](https://discuss.google.dev/t/hmac-request-validation/6031/17 "2019-12-04T16:20:23Z")

</div>

contextVars is set previously, by reading “Auth.stringToSign” .

(Yes it worked when I wrote it)

---

<div class="post-metadata">

**Author:** ![dchiesa1](https://d3byx8b92xw3lq.cloudfront.net/user_avatar/discuss.google.dev/dchiesa1/32/15641_2.png) [@dchiesa1](https://discuss.google.dev/u/dchiesa1)\
**Post date:** [December 4, 2019, 4:20pm UTC](https://discuss.google.dev/t/hmac-request-validation/6031/18 "2019-12-04T16:20:43Z")

</div>

We’re working on adding an HMAC policy to Apigee. . . . .

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [February 24, 2020, 10:14am UTC](https://discuss.google.dev/t/hmac-request-validation/6031/19 "2020-02-24T10:14:42Z")

</div>

Hi [@Dino](https://community.apigee.com/users/803/dchiesa.html) is there any ETA on that? And is it safe to use the JAVA callout you mentioned in the previous comment for now?

---

<div class="post-metadata">

**Author:** ![dchiesa1](https://d3byx8b92xw3lq.cloudfront.net/user_avatar/discuss.google.dev/dchiesa1/32/15641_2.png) [@dchiesa1](https://discuss.google.dev/u/dchiesa1)\
**Post date:** [February 24, 2020, 6:56pm UTC](https://discuss.google.dev/t/hmac-request-validation/6031/20 "2020-02-24T18:56:48Z")

</div>

Yes it's still safe, for now. That release is rolling out now, as we speak. I don't have an ETA on availability for your particular organization. Possibly this week. . . .

[Next page](https://discuss.google.dev/t/hmac-request-validation/6031.md?page=2)
