Génération d'un HMAC SHA 256
Salut a tous, voila je suis pas un super pro de la crypto sous java. Je veux
generer une cle hmac sha256 a partir d'une cle de 40 bytes (caracteres ?)
Je suis un peu perdu dans tous ca, et voici donc la fonction que j'aicommencé a ecrire, et je suis stoppé net aux ??????? . JE ne sais pas quoi faire apres pour
recuperer ce que je veux en resultat de la fonction.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| static String compute_hmac256 (String from_key, String id_hash) {
// Calcul d'un hash Hmac256 à partir d'une clé ? Longueur 64 car.
String result = "";
System.out.println("Fonction :" + from_key);
System.out.println("Fonction :" + id_hash);
byte[] theByteArray = new byte[40];
byte[] hmacData = null;
theByteArray = from_key.getBytes();
/*for (int i= 0; i < theByteArray.length; i++) {
System.out.print((char)theByteArray[i]);
};*/
try {
SecretKeySpec secret_key = new SecretKeySpec (from_key.getBytes("UTF-8"),"HmacSHA256");
//System.out.println();
//System.out.println("Fonction : secret_key : " + secret_key.toString());
//final SecretKeySpec secret_key = new javax.crypto.spec.SecretKeySpec(charSet.encode("key").array(), "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(secret_key);
//mac.reset();
theByteArray = id_hash.getBytes("UTF-8");
hmacData=mac.doFinal(id_hash.getBytes("UTF-8"));
}
catch (UnsupportedEncodingException e){e.printStackTrace();}
catch (NoSuchAlgorithmException e){e.printStackTrace();}
catch (InvalidKeyException e) {e.printStackTrace();};
for (int i= 0; i < hmacData.length-1; i++) {
System.out.print(hmacData[i]+"/");
};
return "---";
} |
Voici le resultat temporaire de la fonction :
Citation:
1 / TOKEN : 43f6168e635b9a90774cc4d3212d5703c11c9302
2 / Hash login : 8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918
3 / Hash password : 4a7eaba57f40aa68ae46365c9ae5a219d097e29ab0f2dec9e8b576a437baac20
Fonction :43f6168e635b9a90774cc4d3212d5703c11c9302
Fonction :8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918
122/-93/-24/-77/-19/125/-3/119/-106/-128/11/76/76/103/-96/-59/108/94/74/102/80/33/85/-63/122/123/-50/-11/-82/-108/95/4 / ==================== hashmac256, 64 car.========================
---
Appel de la fonction :
Citation:
token_code = 43f6168e635b9a90774cc4d3212d5703c11c9302
login_hash = 8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918
hashmac_string = compute_hmac256(token_code,login_hash);
MERCI pour vos aides précieuses ;-)
Stef.