我正在尝试编写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
现在我在红宝石中尝试
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"
没有哈希匹配,我知道它与编码等有关。我如何匹配java HMAC?
答案 0 :(得分:2)
实际上,我最终使用了来自 commons-codec-1.5.jar 的实用程序类,如下所示:
import org.apache.commons.codec.binary.Hex;
// (...)
Hex.encodeHexString(rawBytes);
答案 1 :(得分: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 = "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代码:
require 'openssl'
digest = OpenSSL::Digest::Digest.new('sha1')
secret = "12345678901234567890"
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。
答案 2 :(得分:0)
只要其他人需要,accepted answer的编译版本。
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();
}