Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Cryptographic hash-based message authentication code used to verify data integrity and authenticity
| ResourceFunction["HMAC"][expr, key] calculates message authentication code for expr using secret key key and the SHA256 hash function. | |
| ResourceFunction["HMAC"][expr, key, hash] calculates message authentication code for expr using secret key key and hash function hash. | 
| expr | byte representation obtained by BinarySerialize | 
| "string" | bytes in the UTF-8 representation of string | 
| ByteArray[…] | literal bytes in the byte array | 
| "MD2" | 128-bit MD2 code | 
| "MD4" | 128-bit MD4 code | 
| "MD5" | 128-bit MD5 code | 
| "SHA" | 160-bit SHA-1 code | 
| "SHA224" | 224-bit SHA code | 
| "SHA256" | 256-bit SHA code (default) | 
| "SHA384" | 384-bit SHA code | 
| "SHA512" | 512-bit SHA code | 
| "RIPEMD160" | 160-bit RIPEMD code | 
| "RIPEMD160SHA256" | RIPEMD-160 following SHA-256 (as used in Bitcoin) | 
| "SHA256SHA256" | double SHA-256 code (as used in Bitcoin) | 
| "SHA3-224" | 224-bit SHA3 code | 
| "SHA3-256" | 256-bit SHA3 code | 
| "SHA3-384" | 384-bit SHA3 code | 
| "SHA3-512" | 512-bit SHA3 code | 
| "Keccak224" | 224-bit Keccak code | 
| "Keccak256" | 256-bit Keccak code | 
| "Keccak384" | 384-bit Keccak code | 
| "Keccak512" | 512-bit Keccak code | 
Compute message authentication code using the default SHA256 hash and a secret key:
| In[1]:= | ![key = GenerateSymmetricKey[]](https://www.wolframcloud.com/obj/resourcesystem/images/06c/06c0011f-6e0c-481e-9952-805d382528fe/7029f80264efc57d.png) | 
| Out[1]= |  | 
| In[2]:= | ![ResourceFunction["HMAC"][Graphics[Disk[]], key]](https://www.wolframcloud.com/obj/resourcesystem/images/06c/06c0011f-6e0c-481e-9952-805d382528fe/72dd7aeb4d9ae54f.png) | 
| Out[2]= |  | 
Compute a SHA3-based message authentication code:
| In[3]:= | ![key = GenerateSymmetricKey[];](https://www.wolframcloud.com/obj/resourcesystem/images/06c/06c0011f-6e0c-481e-9952-805d382528fe/40d935a17f84ec5c.png) | 
| In[4]:= | ![ResourceFunction[
 "HMAC"]["The quick brown fox jumps over the lazy dog", key, "SHA3-256"]](https://www.wolframcloud.com/obj/resourcesystem/images/06c/06c0011f-6e0c-481e-9952-805d382528fe/5cba43d78b145e4a.png) | 
| Out[12]= |  | 
Compute HMAC using a password as a key:
| In[13]:= | ![ResourceFunction[
 "HMAC"]["The quick brown fox jumps over the lazy dog", "password"]](https://www.wolframcloud.com/obj/resourcesystem/images/06c/06c0011f-6e0c-481e-9952-805d382528fe/4db484791f1801e3.png) | 
| Out[13]= |  | 
Use a ByteArray key directly:
| In[14]:= | ![key = ByteArray[{11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}];](https://www.wolframcloud.com/obj/resourcesystem/images/06c/06c0011f-6e0c-481e-9952-805d382528fe/172c6e8d83136715.png) | 
| In[15]:= | ![ResourceFunction[
 "HMAC"]["The quick brown fox jumps over the lazy dog", key]](https://www.wolframcloud.com/obj/resourcesystem/images/06c/06c0011f-6e0c-481e-9952-805d382528fe/2464f0c48d216a76.png) | 
| Out[15]= |  | 
For ByteArray or string message, literal bytes are used in HMAC computation:
| In[16]:= | ![key = ByteArray[{11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}];](https://www.wolframcloud.com/obj/resourcesystem/images/06c/06c0011f-6e0c-481e-9952-805d382528fe/0c67b5d06bfb5a23.png) | 
| In[17]:= | ![ResourceFunction["HMAC"]["abc", key] == ResourceFunction["HMAC"][StringToByteArray["abc"], key]](https://www.wolframcloud.com/obj/resourcesystem/images/06c/06c0011f-6e0c-481e-9952-805d382528fe/68f6786ccd07ba0c.png) | 
| Out[17]= |  | 
Use BaseEncode to convert byte array output to string encodings:
| In[18]:= | ![hmac = ResourceFunction["HMAC"]["message", ByteArray[{11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}]];](https://www.wolframcloud.com/obj/resourcesystem/images/06c/06c0011f-6e0c-481e-9952-805d382528fe/3ed216d6d5422554.png) | 
| In[19]:= | ![BaseEncode[hmac]](https://www.wolframcloud.com/obj/resourcesystem/images/06c/06c0011f-6e0c-481e-9952-805d382528fe/3ff8b065bd290fbf.png) | 
| Out[19]= |  | 
| In[20]:= | ![BaseEncode[hmac, "Base16"]](https://www.wolframcloud.com/obj/resourcesystem/images/06c/06c0011f-6e0c-481e-9952-805d382528fe/68b6f3629636ec85.png) | 
| Out[20]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License