2011-08-20 61 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);