Take the 2-minute tour ×

I'm looking at implementing an app getting Twitter authorization via Oauth in Java. The first step is getting a request token. Here is a Python example for app engine. To test my code, I am running Python and checking output with Java. Here is an example of Python generating a Hash-Based Message Authentication Code (HMAC):


from hashlib import sha1
from hmac import new as hmac

key = "qnscAdgRlkIhAUPY44oiexBKtQbGY0orf7OV1I50"
message = "foo"

print "%s" % hmac(key, message, sha1).digest().encode('base64')[:-1]


$ ./foo.py

How does one replicate this example in Java?

I've seen an example of HMAC in Java:

try {
    // Generate a key for the HMAC-MD5 keyed-hashing algorithm; see RFC 2104
    // In practice, you would save this key.
    KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
    SecretKey key = keyGen.generateKey();

    // Create a MAC object using HMAC-MD5 and initialize with key
    Mac mac = Mac.getInstance(key.getAlgorithm());

    String str = "This message will be digested";

    // Encode the string into bytes using utf-8 and digest it
    byte[] utf8 = str.getBytes("UTF8");
    byte[] digest = mac.doFinal(utf8);

    // If desired, convert the digest into a string
    String digestB64 = new sun.misc.BASE64Encoder().encode(digest);
} catch (InvalidKeyException e) {
} catch (NoSuchAlgorithmException e) {
} catch (UnsupportedEncodingException e) {

It uses javax.crypto.Mac, all good. However, the SecretKey constructors take bytes and an algorithm. What's the algorithm in the Python example? How can one create a Java secret key without an algorithm?

share|improve this question

2 Answers 2

up vote 46 down vote accepted

HmacSHA1 seems to be the algorithm name you need:

SecretKeySpec keySpec = new SecretKeySpec(

Mac mac = Mac.getInstance("HmacSHA1");
byte[] result = mac.doFinal("foo".getBytes());

BASE64Encoder encoder = new BASE64Encoder();



Note that I've used sun.misc.BASE64Encoder for a quick implementation here, but you should probably use something that doesn't depend on the Sun JRE. The base64-encoder in Commons Codec would be a better choice, for example.

share|improve this answer
good one! thanks. –  dfrankow Jul 9 '10 at 14:22
@Bruno hi, could you explain how can I "zero pad" the secret key here if it is smaller than the recommended block size, which is 160 bits for SHA1 tnx –  Spring Dec 27 '12 at 0:39
Thank you very much, great example. For the record though, I think you definitely change your code sample with the Apache Commons Codec or the Guava encoder ;) –  Brice May 2 '13 at 17:41
"string".getBytes() looks rather strange. It will return platform dependend bytes (specify an encoding!) and it will not cover the whole range of byte values. I would expect either using an encoding (like bae64, base32 or hexdigest) for the string representation of the key or to use a password strengthening (PBKDF) to return pure well distributed bytes. (But of course this is a problem of the spec/python sample as well). –  eckes Jul 13 at 1:18
@eckes Very good point, I almost always use getBytes("UTF-8") myself. As you say, the string in the Python example doesn't start with u"....", and we don't know whether it's Python 3. –  Bruno Jul 13 at 13:54

A minor thing but if you are looking for an equivalent to hmac(key,message) then by default the python library will use the MD5 algorithm, so you need to use the HmacMD5 algorithm in Java.

I mention this because I had this exact problem and found this answer which was helpful, but I missed the part where a digest method was passed in to hmac() and thus went down a rabbit hole. Hopefully this answer will prevent others doing the same in the future.

e.g. in Python REPL

>>> import hmac
>>> hmac.new("keyValueGoesHere", "secretMessageToHash").hexdigest()

This is equivalent to the Java method:

import org.apache.commons.codec.binary.Hex;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class HashingUtility {
    public static String HMAC_MD5_encode(String key, String message) throws Exception {

        SecretKeySpec keySpec = new SecretKeySpec(

        Mac mac = Mac.getInstance("HmacMD5");
        byte[] rawHmac = mac.doFinal(message.getBytes());

        return Hex.encodeHexString(rawHmac);

Note that in my example I'm doing the equivalent of .hexdigest()

share|improve this answer
encodeHexString did not work on Android so I had to use this instead: return new String(Hex.encodeHex(rawHmac)); –  JustinMorris Aug 21 '13 at 14:52

Your Answer


