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

An example of how to use HMAC #39

Open
Ruffio opened this issue Sep 26, 2017 · 3 comments
Open

An example of how to use HMAC #39

Ruffio opened this issue Sep 26, 2017 · 3 comments

Comments

@Ruffio
Copy link

Ruffio commented Sep 26, 2017

I would be nice with an example of how to use HMAC. Are there any restrictions on salt? Length/format ?

@Lingonmirakel
Copy link

I'll second that. It's the only one not mentioned in the docs, and it seems to be implemented in a non-straightforward way? I'm on version 1.0.7 and have this problem:

const sha256 = message => new Hashes.SHA256().hex(message) // Works!
const hmac = message => new Hashes.HMAC().hex(message) // Doesn't work

@robocoder
Copy link

@rlueder
Copy link

rlueder commented May 18, 2020

I landed here after following an example in the Twitter API docs and not getting the expected result, hopefully the following will help someone else:

  1. The README is a bit misleading as it lists HMAC under "Supported hash algorithms" but "Usage" does not include an example of it an instantiable Object. Turns out HMAC is NOT available as an instance, you'll need one of the public methods instead: hex_hmac(), b64_hmac() or any_hmac()

  2. The example pasted above by @robocoder is incorrect as b64_mac() and other functions take key first and then the string; a small detail that completely modifies the result of the encoding.

In my case I was able to sign an OAuth 1.0 request correctly with the following:

const OAUTH_SIGNATURE = new Hashes.SHA1().b64_hmac(KEY, STRING);

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

4 participants