HMAC()
Creates a hash-based message authentication code (HMAC) for the given message combined with a given secret key using an optional algorithm and encoding.
HMACs are used to verify the data integrity and authenticity of a transmitted message.
The hash function used by the HMAC function depends on the algorithm specified.
HMAC( message=object, key=object, algorithm=string, encoding=string );
Returns: String
Examples
message = 'this is a test';
key = 'ABC123';
<span class="c">// Using the required fields only</span>
<span class="nv">result</span> <span class="o">=</span> <span class="nf">HMAC</span><span class="p">(</span> <span class="nv">message</span><span class="p">,</span> <span class="nv">key</span> <span class="p">);</span>
<span class="nf">writeDump</span><span class="p">(</span> <span class="nv">result</span> <span class="p">);</span> <span class="c">// 776770430C93778AD6F91B43A4A30B69</span>
<span class="c">// Using the optional algorithm parameter (HmacSHA1)</span>
<span class="nv">algorithm</span> <span class="o">=</span> <span class="s1">'HmacSHA1'</span><span class="p">;</span>
<span class="nv">result</span> <span class="o">=</span> <span class="nf">HMAC</span><span class="p">(</span> <span class="nv">message</span><span class="p">,</span> <span class="nv">key</span><span class="p">,</span> <span class="nv">algorithm</span> <span class="p">);</span>
<span class="nf">writeDump</span><span class="p">(</span> <span class="nv">result</span> <span class="p">);</span> <span class="c">// 049E53BAE339C4A05587D7BBBA2857548E8FC327</span>
<span class="c">// Using the optional algorithm parameter (HmacSHA256)</span>
<span class="nv">algorithm</span> <span class="o">=</span> <span class="s1">'HmacSHA256'</span><span class="p">;</span>
<span class="nv">result</span> <span class="o">=</span> <span class="nf">HMAC</span><span class="p">(</span> <span class="nv">message</span><span class="p">,</span> <span class="nv">key</span><span class="p">,</span> <span class="nv">algorithm</span> <span class="p">);</span>
<span class="nf">writeDump</span><span class="p">(</span> <span class="nv">result</span> <span class="p">);</span> <span class="c">// 0503949602EDE3FF61C84F4CE51C99EEA2961CAA144AEE552F7D120AD6A60D7D</span>
<span class="c">// Using the optional encoding parameter (UTF-8)</span>
<span class="nv">encoding</span> <span class="o">=</span> <span class="s1">'UTF-8'</span><span class="p">;</span>
<span class="nv">result</span> <span class="o">=</span> <span class="nf">HMAC</span><span class="p">(</span> <span class="nv">message</span><span class="p">,</span> <span class="nv">key</span><span class="p">,</span> <span class="nv">algorithm</span><span class="p">,</span> <span class="nv">encoding</span> <span class="p">);</span>
<span class="nf">writeDump</span><span class="p">(</span> <span class="nv">result</span> <span class="p">);</span> <span class="c">// 0503949602EDE3FF61C84F4CE51C99EEA2961CAA144AEE552F7D120AD6A60D7D</span>
See also
- Cryptography
- Search Issue Tracker
- Search Lucee Test Cases (good for further, detailed examples)