简体   繁体   English

来自 Java 的 Ruby 中的 HMAC

[英]HMAC in Ruby from Java

I am trying to write have the following java function in ruby:我正在尝试在 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;
}

I added the System.out.println, here is the output:我添加了 System.out.println,这是输出:

Algorithm [RAW] key [3132333435363738393031323334353637383930]
Final text: 0000000000000000
Hash: cc93cf18508d94934c64b65d8ba7667fb7cde4b0

Now in ruby I try现在在红宝石我尝试

require 'openssl' 
#    
#   text: 0000000000000000
#   Key bytes: 3132333435363738393031323334353637383930 
#   Wanted hash = cc93cf18508d94934c64b65d8ba7667fb7cde4b0

digest  = OpenSSL::Digest::Digest.new('sha1')
secret = "12345678901234567890"
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"

None of the hashes match, I know it's something to do with encodings etc. How can I match the java HMAC ?没有一个哈希匹配,我知道这与编码等有关。我如何匹配 java HMAC ?

Actually, I ended up using an utility class from from commons-codec-1.5.jar , as follows:实际上,我最终使用了来自commons-codec-1.5.jar的实用程序类,如下所示:

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

Just a compiling version of the accepted answer if anybody else needs it.如果其他人需要,只是已接受答案的编译版本。

public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException {
    String secret = "12345678901234567890";
    byte[] text = "0".getBytes();

    Mac hmacSha1 = Mac.getInstance("HmacSHA1");
    SecretKeySpec macKey = new SecretKeySpec(secret.getBytes(), "HmacSHA1");
    hmacSha1.init(macKey);

    byte[] hash = hmacSha1.doFinal(text);
    String hexString = toHexString(hash);
    System.out.println(hexString);
}

private static String toHexString(final byte[] hash) {
    Formatter formatter = new Formatter();

    for (byte b : hash) {
        formatter.format("%02x", b);
    }

    return formatter.toString();
}

Java code:爪哇代码:


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 = "12345678901234567890";
        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 code:红宝石代码:


require 'openssl'

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

output: Java:输出:Java:

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

Ruby:红宝石:

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

Done, the problem was that the java version was not translated into hexstrings properly.完成了,问题是 java 版本没有正确转换为 hexstrings。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM