[erlang-questions] How to use crypto:hmac_init() and friends?

Frans Schneider <>
Thu Nov 19 14:37:32 CET 2015


Dear list,

I have to create the HMAC of a message by including data at several 
points in my code as shown below:

...
Mac_ctxt_init = crypto:hmac_init(sha256, Mac_key),
Mac_ctxt = case Version of
                3 ->
                    Mac_ctxt1 = crypto:hmac_update(Mac_ctxt_init, Dhi_pub),
                    crypto:hmac_update(Mac_ctxt1, Dhir_pub);
                2 ->
                    Mac_ctxt_init
            end,
...
...
...
LMac_ctxt = crypto:hmac_update(Mac_ctxt, <<Version/binary, 
Serialized/binary>>),
LMac = crypto:hmac_final_n(LMac_ctxt, 8),
...

This will always return <<>>! It already starts with the hmac_init() 
which returns <<>>.

Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:2:2] [async-threads:10] 
[hipe] [kernel-poll:false]

Eshell V7.1  (abort with ^G)
1> K = 
<<9,138,157,154,221,37,75,104,28,207,127,232,193,207,68,39,22,88,62,219,15,114,22,236,68,226,141,39,69,212,143,123>>.
<<9,138,157,154,221,37,75,104,28,207,127,232,193,207,68,
   39,22,88,62,219,15,114,22,236,68,226,141,39,69,...>>
2> crypto:hmac_init(sha256, K).
<<>>
3>

Using crypto:hmac(sha256, K, Data, 8) works fine, but is not usable.

Any idea what goes wrong or what I am doing wrong?

TIA,

Frans


More information about the erlang-questions mailing list