Skip to content

Instantly share code, notes, and snippets.

@etrepum
Created May 16, 2012 16:57
Show Gist options
  • Save etrepum/2712195 to your computer and use it in GitHub Desktop.
Save etrepum/2712195 to your computer and use it in GitHub Desktop.
hmac
-module(erlhmac).
-export([hmac/4]).
hmac(Key, Message, Hash, BlockSize) when byte_size(Key) > BlockSize ->
hmac(Hash(Key), Message, Hash, BlockSize);
hmac(Key, Message, Hash, BlockSize) when byte_size(Key) < BlockSize ->
hmac(<<Key/binary,
(binary:copy(<<0>>, BlockSize - byte_size(Key)))/binary>>,
Message, Hash, BlockSize);
hmac(Key, Message, Hash, _BlockSize) ->
OKeyPad = <<<<(X bxor 16#5c)>> || <<X>> <= Key>>,
IKeyPad = <<<<(X bxor 16#36)>> || <<X>> <= Key>>,
Hash([OKeyPad, Hash([IKeyPad, Message])]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment