Javaで Oauth を介して Twitter 承認を取得するアプリの実装を検討しています。最初のステップは、リクエスト トークンの取得です。App EngineのPython の例を次に示します。
コードをテストするために、Python を実行し、Java で出力をチェックしています。ハッシュベースのメッセージ認証コード (HMAC) を生成する Python の例を次に示します。
#!/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]
出力:
$ ./foo.py
+3h2gpjf4xcynjCGU5lbdMBwGOc=
この例をJavaでどのように複製しますか?
Javaでの HMACの例を見てきました。
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) {
}
javax.crypto.Macを使用していますが、すべて問題ありません。ただし、SecretKeyコンストラクターはバイトとアルゴリズムを使用します。
Python の例のアルゴリズムは何ですか? アルゴリズムなしでJava秘密鍵を作成するにはどうすればよいですか?