我試圖寫在紅寶石下面的Java功能:HMAC在Ruby中從Java
public static byte[] hmac_sha1(byte[] keyBytes, byte[] text)
throws NoSuchAlgorithmException, InvalidKeyException
{
// try {
Mac hmacSha1;
try {
hmacSha1 = Mac.getInstance("HmacSHA1");
} catch (NoSuchAlgorithmException nsae) {
hmacSha1 = Mac.getInstance("HMAC-SHA-1");
}
SecretKeySpec macKey = new SecretKeySpec(keyBytes, "RAW");
hmacSha1.init(macKey);
System.out.println("Algorithm [" + macKey.getAlgorithm() + "] key [" + Helper.bytesToString(macKey.getEncoded()) + "]");
System.out.println("Final text: " + Helper.bytesToString(text));
byte[] hash = hmacSha1.doFinal(text);
System.out.println("Hash: " + Helper.bytesToString(hash));
return hash;
}
我加入的System.out.println,這裏是輸出:
Algorithm [RAW] key [3132333435363738393031323334353637383930]
Final text: 0000000000000000
Hash: cc93cf18508d94934c64b65d8ba7667fb7cde4b0
現在在紅寶石我嘗試哈希比賽,我知道這件事情與編碼等做我怎樣才能在Java HMAC匹配
require 'openssl'
#
# text: 0000000000000000
# Key bytes: 3132333435363738393031323334353637383930
# Wanted hash = cc93cf18508d94934c64b65d8ba7667fb7cde4b0
digest = OpenSSL::Digest::Digest.new('sha1')
secret = "123456789"
secret2 = "3132333435363738393031323334353637383930"
text = "0000000000000000"
puts OpenSSL::HMAC.hexdigest(digest, secret, text)
puts OpenSSL::HMAC.hexdigest(digest, secret, "0")
puts OpenSSL::HMAC.hexdigest(digest, secret2, "0")
puts OpenSSL::HMAC.hexdigest(digest, secret2, text)
puts "Wanted hash: cc93cf18508d94934c64b65d8ba7667fb7cde4b0"
無?
你可以發佈你如何獲得在Java中鍵/文本? – emboss
謝謝你引導我正確的方向,這是我編碼生成的散列的方式 – daniel
@daniel:我明白,現在解決了這個問題嗎?如果是這樣,你可以請你的解決方案作爲答案(並在以後接受它)嗎? –