How to create SHA256 HMAC using Ironclad in Common Lisp?

There is a python function that I'm trying to port to Common Lisp:

HEX(HMAC_SHA256(apiSecret, 'stupidstupid')) 

How do I do this with Ironclad?

Nearest I came:

 (ironclad:make-hmac apiSecret :sha256) 

But it does not work; he says apiSecret

 The value "V0mn1LLQIc6GNSiBpDfDmRo3Ji8leBZWqMIolNBsnaklScgI" is not of type (SIMPLE-ARRAY (UNSIGNED-BYTE 8) (*)). 
+5
source share
1 answer

Ironclad internally works with byte arrays.

But it provides tools for converting from ascii strings to such arrays, and from bytes to โ€œhexadecimalโ€ strings. Here is an interactive session (note that I know little about cryptographic algorithms):

 CL-USER> (in-package :ironclad) #<PACKAGE "IRONCLAD"> 

Privacy Transformation:

 CRYPTO> (ascii-string-to-byte-array "V0mn1LLQIc6GNSiBpDfDmRo3Ji8leBZWqMIolNBsnaklScgI") #(86 48 109 110 49 76 76 81 73 99 54 71 78 83 105 66 112 68 102 68 109 82 111 51 74 105 56 108 101 66 90 87 113 77 73 111 108 78 66 115 110 97 107 108 83 99 103 73) 

Building HMAC from the previous value:

 CRYPTO> (make-hmac * :sha256) #<HMAC(SHA256) {1006214D93}> 

Now I'm not sure if this is what you want, but according to the documentation , you should update hmac with one or more sequences:

 CRYPTO> (update-hmac * (ascii-string-to-byte-array "stupidstupid")) #<HMAC(SHA256) {1006214D93}> 

... and then calculate the digest:

 CRYPTO> (hmac-digest *) #(178 90 228 244 244 45 109 163 51 222 77 235 244 173 249 208 144 43 116 130 210 188 62 247 145 153 100 198 119 86 207 163) 

The resulting array can be converted to the sixth line:

 CRYPTO> (byte-array-to-hex-string *) "b25ae4f4f42d6da333de4debf4adf9d0902b7482d2bc3ef7919964c67756cfa3" 

For completeness, here's how you could wrap these functions to replicate the source code, assuming you are in a package that imports the correct characters:

 (defun hex (bytes) (byte-array-to-hex-string bytes)) (defun hmac_sha256 (secret text) (let ((hmac (make-hmac (ascii-string-to-byte-array secret) :sha256))) (update-hmac hmac (ascii-string-to-byte-array text)) (hmac-digest hmac))) 

Finally:

 (HEX (HMAC_SHA256 "V0mn1LLQIc6GNSiBpDfDmRo3Ji8leBZWqMIolNBsnaklScgI" "stupidstupid")) => "b25ae4f4f42d6da333de4debf4adf9d0902b7482d2bc3ef7919964c67756cfa3" 
+9
source

Source: https://habr.com/ru/post/1264698/


All Articles