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
12
votes
2 answers

Why base64 a sha1/sha256 hash?

can anybody tell me why amazon want a base64 of the hmac-sha1/sha256 hash? http://docs.amazonwebservices.com/AmazonSimpleDB/latest/DeveloperGuide/HMACAuth.html I know that base64 is to represent binary date in ascii but sha1/sha256 is already ascii…
tuna
  • 931
  • 2
  • 10
  • 28
12
votes
5 answers

HMAC SHA256 hex digest of a string in Erlang, how?

I am trying to interact with third party real time Web messaging System created and maintained by Pusher.com. Now, i cannot send anything through the API unless i produce an HMAC SHA256 hex digest of my data. A sample source code written in ruby…
Muzaaya Joshua
  • 7,736
  • 3
  • 47
  • 86
12
votes
2 answers

How to implement HMAC Authentication in a RESTful WCF API

We are building a RESTful API using WCF (currently .Net 3.5, but will be moving to .Net 4 soon). We have a functional framework in place, but it is currently unsecured. It will need to be accessible from .Net applications as well as iOS, Android,…
Steven King
  • 562
  • 1
  • 8
  • 13
12
votes
1 answer

Why the hashlib and hmac are generating different hash values?

In Python 2.7, my = "my" key = "key" print(hashlib.sha256(my + key).hexdigest()) print(hmac.new(my, key,…
Er Dj
  • 121
  • 1
  • 1
  • 3
12
votes
1 answer

JWT Token Invalid Signature

I am using JWT in my application for login authentication process. To generate the token I am using: Jwts.builder().setSubject(username).signWith(SignatureAlgorithm.HS512, MacProvider.generateKey()).compact(); Generated…
Mohit224
  • 443
  • 1
  • 10
  • 24
12
votes
2 answers

How to create a SECRET_HASH for AWS Cognito using boto3?

I want to create/calculate a SECRET_HASH for AWS Cognito using boto3 and python. This will be incorporated in to my fork of warrant. I configured my cognito app client to use an app client secret. However, this broke the following code. def…
nu everest
  • 9,589
  • 12
  • 71
  • 90
12
votes
2 answers

C# help required to Create Facebook AppSecret_Proof HMACSHA256

Facebook requires that I create a appsecret_proof: https://developers.facebook.com/docs/graph-api/securing-requests And I have done this using the following code: public string FaceBookSecret(string content, string key) { var encoding =…
davethecoder
  • 3,856
  • 4
  • 35
  • 66
12
votes
2 answers

What is the C# equivalent of the Java SecretKeySpec

I have following code written in Java Mac mac = Mac.getInstance("HmacSHA1"); String secretKey ="sKey"; String content ="Hello"; byte[] secretKeyBArr = secretKey.getBytes(); byte[] contentBArr = content.getBytes(); SecretKeySpec secret_key =…
user1810618
  • 153
  • 1
  • 2
  • 6
12
votes
2 answers

Ruby and PHP HMACs not agreeing

I'm trying to create an HMAC in Ruby and then verify it in PHP. Ruby: require 'openssl' message = "A522EBF2-5083-484D-99D9-AA97CE49FC6C,1234567890,/api/comic/aWh62,GET" key = "3D2143BD-6F86-449F-992C-65ADC97B968B" hash =…
Jim Keener
  • 9,255
  • 4
  • 24
  • 24
12
votes
1 answer

Node JS crypto, cannot create hmac on chars with accents

I am having an issue generating the correct signature in NodeJS (using crypto.js) when the text I am trying to encrypt has accented characters (such as ä,ï,ë) generateSignature = function (str, secKey) { var hmac = crypto.createHmac('sha1',…
Tommy
  • 176
  • 1
  • 2
  • 17
11
votes
1 answer

How to get Ruby generated HMAC for SHA256 that is url safe to match Java?

I have a tomcat server running some Java code that lets users authenticate using an API key. The request uses an HMAC created with SHA256. I have a Ruby client that I am using to make the request and since I'm new to encryption I am having a…
mvalley
  • 127
  • 1
  • 6
11
votes
4 answers

Library for generating HMAC-SHA1 OAuth signature on Android?

Using the specifications below I need to create an oauth_signature on Android. I'm looking for a library that handles the boiler plate code in creating a signature for accessing resources via OAuth. Construct a signature "base string", which…
Will Curran
  • 6,959
  • 15
  • 59
  • 92
11
votes
1 answer

How to get PHP to create HMAC-SHA1 strings like Objective-C?

I am trying to implement an authentication solution with PHP and Objective-C. Both languages create their own HMAC-SHA1 encoded strings with the same key and the same secret. Apparently they seem to differ in their way how they do it. On Objective-C…
Paul
  • 1,295
  • 2
  • 11
  • 26
11
votes
3 answers

Erlang calculating HMAC-SHA1 example?

Any examples or libraries to caculate HMAC-SHA1 in Erlang? I tried Crypto Module, but apparently doesn't match exactly. Any examples?
barata7
  • 338
  • 1
  • 5
  • 13
11
votes
3 answers

dnx451 RC1 What happened to InMemorySymmetricSecurityKey?

I've been trying to create and sign a JwtSecurityToken using a simple key. And after a lot of research it seems that all the examples I find use the InMemorySymmetricSecurityKey class but unfortunately this class doesn't seem to exist in the newest…
Rui Taborda
  • 170
  • 1
  • 3
  • 17