#StackBounty: #cryptography #elm #hmac How do I build a HMAC code in Elm?

Bounty: 500

I’m trying to implement HMAC in Elm,
but I can’t seem to figure out what I’m doing wrong here.
Some help would be greatly appreciated ?

encrypt : Int -> HashFunction -> String -> String -> String
encrypt blockSize hasher message key =
    let
        keySize =
            String.length key

        keyWithCorrectSize =
            if keySize > blockSize then
                hexStringToUtf8String (hasher key)
            else if keySize < blockSize then
                String.padRight blockSize (Char.fromCode 0) key
            else
                key

        keyCodePoints =
            keyWithCorrectSize
                |> String.toList
                |> List.map Char.toCode

        partA =
            keyCodePoints
                |> List.map (Bitwise.xor 54 >> Char.fromCode)
                |> String.fromList

        partB =
            keyCodePoints
                |> List.map (Bitwise.xor 92 >> Char.fromCode)
                |> String.fromList
    in
        message
            |> String.append partA
            |> hasher
            |> hexStringToUtf8String
            |> String.append partB
            |> hasher


hexStringToUtf8String : String -> String
hexStringToUtf8String =
    String.map hexCharToNormalChar


hexCharToNormalChar : Char -> Char
hexCharToNormalChar =
    String.fromChar >> Hex.fromString >> Result.withDefault 0 >> Char.fromCode

You can find the related code here: https://github.com/icidasset/ongaku-ryoho/blob/master/src/App/Sources/Crypto/Hmac.elm (which includes doc tests)

edit: To be more clear… my current code here doesn’t output a valid HMAC,
and I would like to know why.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.