2011-08-20 23 views
0

我試圖寫在紅寶石下面的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" 

無?

+0

你可以發佈你如何獲得在Java中鍵/文本? – emboss

+0

謝謝你引導我正確的方向,這是我編碼生成的散列的方式 – daniel

+1

@daniel:我明白,現在解決了這個問題嗎?如果是這樣,你可以請你的解決方案作爲答案(並在以後接受它)嗎? –

回答

0

Java代碼:


import java.io.IOException; 
import java.io.File; 
import java.io.DataInputStream; 
import java.io.FileInputStream ; 
import java.lang.reflect.UndeclaredThrowableException; 

import java.security.GeneralSecurityException; 
import java.security.NoSuchAlgorithmException; 
import java.security.InvalidKeyException; 

import javax.crypto.Mac; 
import javax.crypto.spec.SecretKeySpec; 

public class Stack 
{ 
    public static String hashToHexString(byte[] hash) 
    { 
    StringBuffer hexString = new StringBuffer(); 
    for (int i = 0; i 0) { 
      hexString.append('0'); 
     } 
     hexString.append(hexByte); 
    } 
     return hexString.toString(); 
    } 

    public static void main(String[] args) 
     throws NoSuchAlgorithmException, InvalidKeyException 

    { 

     String secret = "123456789"; 
     byte[] keyBytes = secret.getBytes(); 
     String movingFact = "0"; 
     byte[] text = movingFact.getBytes(); 

     Mac hmacSha1; 
     try { 
      hmacSha1 = Mac.getInstance("HmacSHA1"); 
     } catch (Exception nsae) { 
      hmacSha1 = Mac.getInstance("HMAC-SHA-1");   
     } 
     SecretKeySpec macKey = new SecretKeySpec(keyBytes, "RAW"); 
     hmacSha1.init(macKey); 
     byte[] hash = hmacSha1.doFinal(text); 
     String hexString = hashToHexString(hash); 
     System.out.println(hexString); 
    } 
} 

Ruby代碼:


require 'openssl' 

digest = OpenSSL::Digest::Digest.new('sha1') 
secret = "123456789" 
movingFactor = "0" 
hash = OpenSSL::HMAC.hexdigest(digest, secret, movingFactor) 
puts "Hash: #{hash}" 

輸出: 爪哇:

DANIELs-MacBook-Air:del dani$ javac Stack.java 
DANIELs-MacBook-Air:del dani$ java Stack 
32a67f374525d32d0ce13e3db42b5b4a3f370cce 

紅寶石:

DANIELs-MacBook-Air:del dani$ ruby Stack.rb 
Hash: 32a67f374525d32d0ce13e3db42b5b4a3f370cce 

完成後,問題是java版本沒有正確轉換爲hexstrings。

+0

我有同樣的問題,但你的java代碼有錯誤。我會盡力弄清楚。 –

+0

如果這個Java示例會真正運行,那將會很有幫助。有幾個語法錯誤和一個缺失的變量。 – andrhamm

2

其實,我結束了使用從工具類從公地編解碼器1.5.jar如下:

import org.apache.commons.codec.binary.Hex; 
// (...) 
Hex.encodeHexString(rawBytes);