java hmac/sha512生成
我的php代码生成了一个HMAC(而不是一个简单的消息摘要): 我需要用java克隆它 下面是我当前的java克隆:java hmac/sha512生成,java,php,sha256,hmac,Java,Php,Sha256,Hmac,我的php代码生成了一个HMAC(而不是一个简单的消息摘要): 我需要用java克隆它 下面是我当前的java克隆: private String generateHMAC( String datas ) { // final Charset asciiCs = Charset.forName( "utf-8" ); Mac mac; String result = ""; try
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版本的完全克隆
我做错了什么?我用它来表示Java中的SHA 512。这可能有助于:
public static String sha512 ( String str )
{
try
{
return sha512 ( str.getBytes ( "UTF-8" ) );
}
catch ( UnsupportedEncodingException e )
{
e.printStackTrace ( );
return "";
}
}
public static String sha512 ( byte[] array )
{
try
{
MessageDigest m = MessageDigest.getInstance ( "SHA-512" );
m.update ( array );
String hash = new BigInteger ( 1, m.digest ( ) ).toString ( 16 );
while ( hash.length ( ) < 32 )
{
hash = "0" + hash;
}
return hash;
}
catch ( NoSuchAlgorithmException e )
{
e.printStackTrace ( );
return "";
}
}
公共静态字符串sha512(字符串str)
{
尝试
{
返回sha512(str.getBytes(“UTF-8”);
}
捕获(不支持的编码异常e)
{
e、 printStackTrace();
返回“”;
}
}
公共静态字符串sha512(字节[]数组)
{
尝试
{
MessageDigest m=MessageDigest.getInstance(“SHA-512”);
m、 更新(数组);
stringhash=newbiginger(1,m.digest()).toString(16);
while(hash.length()<32)
{
hash=“0”+散列;
}
返回散列;
}
捕获(无算法异常)
{
e、 printStackTrace();
返回“”;
}
}
我还记得stackoverflow的帖子中有关于MD-5的详细答案(只是算法不同)试试:
private String generateHMAC( String datas )
{
MessageDigest md = MessageDigest.getInstance("SHA-512");
md.update(datas.getBytes("UTF-8")); // Change this to "UTF-16" if needed
byte[] digest = md.digest();
StringBuffer hexString = new StringBuffer();
for (int i=0;i<digest.length;i++) {
hexString.append(Integer.toHexString(0xFF & digest[i]));
}
return hexString.toString();
}
专用字符串生成器MAC(字符串数据)
{
MessageDigest md=MessageDigest.getInstance(“SHA-512”);
md.update(datas.getBytes(“UTF-8”);//如果需要,将其更改为“UTF-16”
字节[]摘要=md.digest();
StringBuffer hexString=新的StringBuffer();
对于(inti=0;i您只是忘记在Java代码中模拟pack()
的行为(无论您需要它做什么)
使用
在Java代码中
其中DatatypeConverter.parseHexBinary()
来自
或者,如果您不想仅仅为了将十六进制字符串转换为字节而包含JAXB,您可能需要使用发布的代码。事实上消息摘要不同于hmac:事实上消息摘要不同于hmac:非常感谢,这正是我要找的。
public static String sha512 ( String str )
{
try
{
return sha512 ( str.getBytes ( "UTF-8" ) );
}
catch ( UnsupportedEncodingException e )
{
e.printStackTrace ( );
return "";
}
}
public static String sha512 ( byte[] array )
{
try
{
MessageDigest m = MessageDigest.getInstance ( "SHA-512" );
m.update ( array );
String hash = new BigInteger ( 1, m.digest ( ) ).toString ( 16 );
while ( hash.length ( ) < 32 )
{
hash = "0" + hash;
}
return hash;
}
catch ( NoSuchAlgorithmException e )
{
e.printStackTrace ( );
return "";
}
}
private String generateHMAC( String datas )
{
MessageDigest md = MessageDigest.getInstance("SHA-512");
md.update(datas.getBytes("UTF-8")); // Change this to "UTF-16" if needed
byte[] digest = md.digest();
StringBuffer hexString = new StringBuffer();
for (int i=0;i<digest.length;i++) {
hexString.append(Integer.toHexString(0xFF & digest[i]));
}
return hexString.toString();
}
final SecretKeySpec secretKey = new SecretKeySpec( DatatypeConverter.parseHexBinary(PayboxConstants.KEY), "HmacSHA512" );