Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HMAC support? #44

Closed
ylxdzsw opened this issue Nov 7, 2017 · 3 comments
Closed

HMAC support? #44

ylxdzsw opened this issue Nov 7, 2017 · 3 comments

Comments

@ylxdzsw
Copy link
Contributor

ylxdzsw commented Nov 7, 2017

I use this package for the TOTP algorithm and some exchanges protocols. Both requires an HMAC functionality, so I hope this can be built into this package like Nettle.jl did.

The implementation (from wiki) can be quiet simple:

function hmac(key::Vector{UInt8}, msg::Vector{UInt8}, hash, blocksize::Int=64)
    if length(key) > blocksize
        key = hash(key)
    end

    pad = blocksize - length(key)

    if pad > 0
        resize!(key, blocksize)
        key[end-pad+1:end] = 0
    end

    o_key_pad = key .⊻ 0x5c
    i_key_pad = key .⊻ 0x36

    hash([o_key_pad; hash([i_key_pad; msg])])
end

and here is a test suite (also from wiki):

@test hmac(b"", b"", sha1, 64)   == hex2bytes("fbdb1d1b18aa6c08324b7d64b71fb76370690e1d")
@test hmac(b"", b"", sha256, 64) == hex2bytes("b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad")
@test hmac(b"key", b"The quick brown fox jumps over the lazy dog", sha1, 64)   == hex2bytes("de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9")
@test hmac(b"key", b"The quick brown fox jumps over the lazy dog", sha256, 64) == hex2bytes("f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8")

When opening an issue, please ping @staticfloat since he does not receive emails automatically when new issues are created.

@staticfloat
Copy link
Member

Hey, this is great @ylxdzsw, if you care to open a PR with this code and the tests, I will happily merge it. I wouldn't want to strip you of your authorship rights. :)

@ylxdzsw
Copy link
Contributor Author

ylxdzsw commented Nov 8, 2017

Hi, I just opened #45 with better interface than above demo, hope it helps.

@staticfloat
Copy link
Member

Merged in #45

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants