java equivalente a hmac-SHA1 di php
-
05-07-2019 - |
Domanda
Sto cercando un java equivalente a questa chiamata php:
hash_hmac('sha1', "test", "secret")
Ho provato questo, usando java.crypto.Mac , ma i due non sono d'accordo:
String mykey = "secret";
String test = "test";
try {
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec secret = new SecretKeySpec(mykey.getBytes(),"HmacSHA1");
mac.init(secret);
byte[] digest = mac.doFinal(test.getBytes());
String enc = new String(digest);
System.out.println(enc);
} catch (Exception e) {
System.out.println(e.getMessage());
}
Le uscite con chiave = " secret " e test = " test " non sembrano corrispondere.
Soluzione
In effetti sono d'accordo.
Come già notato da Hans Doggen, PHP genera il digest del messaggio utilizzando la notazione esadecimale a meno che non si imposti il ??parametro di output non elaborato su true.
Se vuoi usare la stessa notazione in Java puoi usare qualcosa come
for (byte b : digest) {
System.out.format("%02x", b);
}
System.out.println();
per formattare l'output di conseguenza.
Altri suggerimenti
Puoi provare questo in Java:
private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException {
SecretKey secretKey = null;
byte[] keyBytes = keyString.getBytes();
secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(secretKey);
byte[] text = baseString.getBytes();
return new String(Base64.encodeBase64(mac.doFinal(text))).trim();
}
Questa è la mia implementazione:
String hmac = "";
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec secret = new SecretKeySpec(llave.getBytes(), "HmacSHA1");
mac.init(secret);
byte[] digest = mac.doFinal(cadena.getBytes());
BigInteger hash = new BigInteger(1, digest);
hmac = hash.toString(16);
if (hmac.length() % 2 != 0) {
hmac = "0" + hmac;
}
return hmac;
Mi sembra che PHP usi la notazione HEX per i byte che Java produce (1a = 26) - ma non ho controllato l'intera espressione.
Cosa succede se si esegue l'array di byte tramite il metodo su this pagina?
La mia implementazione per HmacMD5 - basta cambiare l'algoritmo in HmacSHA1:
SecretKeySpec keySpec = new SecretKeySpec("secretkey".getBytes(), "HmacMD5");
Mac mac = Mac.getInstance("HmacMD5");
mac.init(keySpec);
byte[] hashBytes = mac.doFinal("text2crypt".getBytes());
return Hex.encodeHexString(hashBytes);
Non l'ho provato, ma prova questo:
BigInteger hash = new BigInteger(1, digest);
String enc = hash.toString(16);
if ((enc.length() % 2) != 0) {
enc = "0" + enc;
}
Questa è un'istantanea dal mio metodo che rende java md5 e sha1 corrispondenti a php.
In questo modo ho potuto ottenere la stessa stringa esatta che avevo con hash_hmac in php
String result;
try {
String data = "mydata";
String key = "myKey";
// Get an hmac_sha1 key from the raw key bytes
byte[] keyBytes = key.getBytes();
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
// Get an hmac_sha1 Mac instance and initialize with the signing key
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
// Compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(data.getBytes());
// Convert raw bytes to Hex
byte[] hexBytes = new Hex().encode(rawHmac);
// Covert array of Hex bytes to a String
result = new String(hexBytes, "ISO-8859-1");
out.println("MAC : " + result);
}
catch (Exception e) {
}