我有这个生成HMAC的 php 代码(而不是简单的消息摘要):
<?php
$key = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
$binkey = pack("H*", $key);
echo strtoupper(hash_hmac('sha512', "ABC", $binkey));
?>
输入它的ABC
输出是:
100A6A016A4B21AE120851D51C93B293D95B7D8A44B16ACBEFC2D1C9DF02B6F54FA3C2D6802E52FED5DF8652DDD244788A204682D2D1CE861FDA4E67F2792643
我需要在java中克隆它。
所以这是我当前的 java 克隆:
private String generateHMAC( String datas )
{
// final Charset asciiCs = Charset.forName( "utf-8" );
Mac mac;
String result = "";
try
{
byte[] bytesKey = PayboxConstants.KEY.getBytes( );
final SecretKeySpec secretKey = new SecretKeySpec( bytesKey, "HmacSHA512" );
mac = Mac.getInstance( "HmacSHA512" );
mac.init( secretKey );
final byte[] macData = mac.doFinal( datas.getBytes( ) );
byte[] hex = new Hex( ).encode( macData );
result = new String( hex, "ISO-8859-1" );
}
catch ( final NoSuchAlgorithmException e )
{
AppLogService.error( e );
}
catch ( final InvalidKeyException e )
{
AppLogService.error( e );
}
catch ( UnsupportedEncodingException e )
{
AppLogService.error( e );
}
return result.toUpperCase( );
}
但它不能完成这项工作,因为对于相同的输入(ABC),它的输出是:
AA6492987D7A7AC81109E877315414806F1973CC47B897ECE713171A25A11B279329B1BFF39EA72A5EFB7EDCD71D1F34D5AAC49999A780BD13F019ED99685B80
我尝试了很多其他 java 代码,但没有一个是 php 版本的精确克隆。
我做错什么了?