I am looking for an implementation of an application that receives Twitter authorization through Oauth in Java. The first step is to get the request token . Here is a Python example for the application engine.
To test my code, I run Python and test the output using Java. Here is a Python example generating a Hash-based message authentication code (HMAC):
#!/usr/bin/python from hashlib import sha1 from hmac import new as hmac key = "qnscAdgRlkIhAUPY44oiexBKtQbGY0orf7OV1I50" message = "foo" print "%s" % hmac(key, message, sha1).digest().encode('base64')[:-1]
Output:
$ ./foo.py +3h2gpjf4xcynjCGU5lbdMBwGOc=
How to replicate this example in Java?
I saw an HMAC example 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()); mac.init(key); 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 is well. However, SecretKey constructors accept bytes and an algorithm.
What is the example of Python? How to create Java secret key without algorithm?
java oauth hmac sha1 cryptoapi
dfrankow Jul 08 '10 at 21:31 2010-07-08 21:31
source share