Information Security Stack Exchange is a question and answer site for information security professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking for, but unable to find, some definitive information on how long to make the secret and what characters are useable in creating a HMAC signature in PHP using SHA256.

I found some information on stackoverflow but I'm still not sure how long the key should be. In this context is 1 character 1 bit? Should my key be a-f0-9 and 256 characters?

Just trying to get message signing done right. Thanks.

share|improve this question

HMAC uses as key a sequence of arbitrary bytes and of arbitrary length. Some implementations of HMAC/SHA-256 may (unduly) reject keys longer than 32 bytes.

Your key should be such that it is too hard to find it by exhaustive search with non-negligible success probability. If your key comes from a strong source of random bytes (e.g. /dev/urandom), then 16 bytes are enough. If the key comes from a biological computing device (i.e. the brain of a human), then you are back to the whole problematic of passwords (which is best avoided if possible).

share|improve this answer

The hash_hmac function takes binary input, i.e. strings made of raw bytes. Character encoding should not come into play at all.

For example, if you were to use SHA256 to compute the HMAC key, from a password, you'd do something like this:

// second parameter selects raw bytes or ASCII hex for output (false = hex, true = raw)
$hmac_key = hash("sha256", $password, true);
// last parameter is, again, whether or not to output raw bytes
echo hash_hmac("sha256", $data, $hmac_key, false);

Assuming the password is "polynomial", the $hmac_key variable should be:


Note that this is a hex representation of the contents of the variable - in reality it'll contain raw bytes. Now let's assume that our message is "Hello there!". The result is as follows:


(the above was generated by QuickHash)

HMAC schemes require that the key length is equal to the block size of the hash. Some implementations use a hash algorithm to produce the correct size if the input it larger or smaller than the block size. Usually the hash algorithm used is the non-HMAC version of whatever you're using. I'm unsure as to whether PHP does this, and (if they do) what method they use.

share|improve this answer
I found that PHP pads the string with 0s but I'm still feeling a bit stupid ... how many characters would I ideally want for a sha256? – Erik Sep 20 '12 at 14:59
@Erik You don't use characters at all. Characters are letters and symbols, which are stored in an encoding format. One character might be stored in one byte, two bytes, or an arbitrary number of bytes. If you're randomly generating a key, look into some of the other questions here about generating cryptographic keys in PHP. – Polynomial Sep 20 '12 at 15:03

It depends on your desired level of security. It is recommended to use SHA256 algo with and an at least 256 bits long key to achieve a 128bit security level (against eg. birthday collision attacks). You were mentioning characters but it is important to note that a key is binary and you should not restrict yourself to human readable characters.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.