Questions tagged [hmac]

In cryptography, HMAC (Hash-based Message Authentication Code) is a specific construction for calculating a message authentication code (MAC) involving a cryptographic hash function in combination with a secret key.

In cryptography, HMAC (Hash-based Message Authentication Code) is a specific construction for calculating a message authentication code (MAC) involving a cryptographic hash function in combination with a secret key. As with any MAC, it may be used to simultaneously verify both the data integrity and the authenticity of a message. Any cryptographic hash function, such as MD5 or SHA-1, may be used in the calculation of an HMAC; the resulting MAC algorithm is termed HMAC-MD5 or HMAC-SHA1 accordingly. The cryptographic strength of the HMAC depends upon the cryptographic strength of the underlying hash function, the size of its hash output length in bits and on the size and quality of the cryptographic key.

An iterative hash function breaks up a message into blocks of a fixed size and iterates over them with a compression function. For example, MD5 and SHA-1 operate on 512-bit blocks. The size of the output of HMAC is the same as that of the underlying hash function (128 or 160 bits in the case of MD5 or SHA-1, respectively), although it can be truncated if desired.

The definition and analysis of the HMAC construction was first published in 1996 by Mihir Bellare, Ran Canetti, and Hugo Krawczyk, who also wrote RFC 2104. This paper also defined a variant called NMAC that is rarely if ever used. FIPS PUB 198 generalizes and standardizes the use of HMACs. HMAC-SHA-1 and HMAC-MD5 are used within the IPsec and TLS protocols.

Source: Wikipedia


An example of calculating a HMAC-SHA256 in Java:

byte[] expectedResult = { /* Expected HMAC result from a prior run */
        96, 21, 116, 11, 4, -51, -115, -20, 104, 18, 117, -75, 3, -100, 126,
        -89, -22, 120, -120, 30, 102, 104, -125, -120, -62, 111, -75,
        24, 14, 62, 48, -65 };

byte[] secret = "your eyes only".getBytes();
String algorithm = "HmacSha256";

SecretKeySpec signingKey = new SecretKeySpec(secret, algorithm);

// Init HMAC usign secret
Mac hmac = Mac.getInstance(algorithm);
hmac.init(signingKey);

// Run message through HMAC and calculate result
byte[] message = "Don't tamper with me".getBytes();
byte[] macOutput = hmac.doFinal(message);

// Compare HMAC output to expected result
// A message that has been altered will not be equal
assertTrue(Arrays.equals(macOutput, expectedResult));
1439 questions
11
votes
2 answers

Generate a strong HMACSHA256 key in C#

I'm looking to implement HMACSHA256 request signing in an API I'm building. From what I understood from https://www.rfc-editor.org/rfc/rfc4868, it's best that the secret key be the same number of bits as the hashing algorithm (i.e. SHA256 secret…
Omar
  • 39,496
  • 45
  • 145
  • 213
11
votes
1 answer

Java vs Python HMAC-SHA256 Mismatch

Based on recent feedback and findings on this problem, I've rewritten the question to get rid of noise. I have 2 separate code paths, one in Java (Android), one and Python which accomplish the following for the purposes of negotiating a pairing…
DanH
  • 5,498
  • 4
  • 49
  • 72
10
votes
1 answer

Attempt to get Twitter request_token using Oauth 1.0 keeps giving '215 Bad Authentication' error

I'm trying to get an Oauth request token from Twitter - I'm following their guide (links below) and I've gone over every single step about 10 times but I cannot figure out why I'm getting this error: {\"errors\":[{\"code\":32,\"message\":\"Could…
Joe Morano
  • 1,715
  • 10
  • 50
  • 114
10
votes
2 answers

HMAC-SHA1 in Rust

I'm trying to apply HMAC-SHA1 in order to check some content but I'm unable to make it work. These are the tests I have: #[cfg(test)] mod tests { use crypto::hmac::Hmac; use crypto::mac::Mac; use crypto::sha1::Sha1; use…
robertohuertasm
  • 846
  • 9
  • 17
10
votes
1 answer

When to use RS256 for JWT?

So, right now I'm building an API for third parties uses and I was reading about RS256 and HS256. What I understood was that diff between is that in the first one you use a public key to verify and a private key to sign, and the other one, use just…
10
votes
1 answer

Testing API routes with HMAC-SHA256 authentication in OpenAPI (Swagger)

I am trying to generate OpenAPI (Swagger) documentation for my API routes which require HMAC-SHA256 authentication. This means that I have to include Authorization header for every request which consists of API key and generated HMAC signature…
errata
  • 5,695
  • 10
  • 54
  • 99
10
votes
2 answers

How to get digest representation of CryptoJS.HmacSHA256 in JS

I have to generate string representation of CryptoJS.HmacSHA256 in digest (bytes representation). I need it because i have to duplicate python code which generate such digest in javascript: print hmac.new("secret", "test",…
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
10
votes
1 answer

What are the benefits of HMAC over symmetric cryptography?

Somehow I don't get HMACs. I once asked Why do I need HMACs when we do have public key signatures?, and I think I got this one. Easier to compute, and so on ... But, what I do not get is why we need HMACs at all, respectively what kind of problem…
Golo Roden
  • 140,679
  • 96
  • 298
  • 425
9
votes
1 answer

NodeJS hmac digest issue with accents

I'm doing a side by side comparison with Ruby, PHP and NodeJS for the following code, getting an incorrect response in NodeJS using the crypto module. PHP hash_hmac('sha256', 'text', 'á'); Ruby OpenSSL::HMAC.hexdigest('sha256', 'á',…
Roberto
  • 1,944
  • 1
  • 30
  • 42
9
votes
3 answers

URL Signing with HMAC or OpenSSL

I'm interested in url signing (e.g. http://.../?somearg=value&anotherarg=anothervalue&sig=aSKS9F3KL5xc), but I have a few requirements which have left me without a solution yet. I'll be using either PHP or Python for pages, so I'll need to be able…
Josh
  • 145
  • 1
  • 2
  • 5
9
votes
1 answer

SHA256 hash the body and base64 encode in Python Vs TypeScript

My goal is to hash the body in SHA256 and then encode it with base64. I am converting python code to TypeScript. Based on google search, what I understood is, crypto can be used against hashlib and base64. Here challenge is, when I use .createHmac…
GThree
  • 2,708
  • 7
  • 34
  • 67
9
votes
1 answer

Python TypeError - Expected bytes but got 'str' when trying to created signature

I'm trying to create a signature for an API call - for which the documentation provides these instructions: timestamp = str(int(time.time())) message = timestamp + request.method + request.path_url + (request.body or '') signature =…
drandom3
  • 199
  • 1
  • 3
  • 13
9
votes
1 answer

What is the preferred way of comparing hmac signatures in Node?

I've read that doing a string comparison is not the preferred way to determine if hmac signatures match. (Go to Step 5) So, in Node, given something like this const hmac = crypto.createHmac("sha256", signingSecret).update(buf, encoding); const…
Newtang
  • 6,414
  • 10
  • 49
  • 70
9
votes
1 answer

How and where to store secret key(s) of your REST API with HMAC based signing

I'm building REST API that should be acceseble for other developers. To authenticate clients I decided to use HMAC-SHA256 request signing with API key and secret key. So the proccess of authentication looks like this: Along with the request body,…
dvnev
  • 121
  • 8
9
votes
3 answers

Securing a javascript client with hmac

I am researching ways to secure a javascript application I am working on. The application is a chat client which uses APE (Ajax Push Engine) as the backend. Currently, anyone can access the page and make a GET/POST request to the APE server. I only…
Walderman
  • 132
  • 3
  • 5