stockage de mot de passe SHA2 avec Java
-
09-10-2019 - |
Question
Je tente de faire un appel XML-RPC qui nécessite hash-256 HmacSHA d'une chaîne particulière. J'utilise actuellement la bibliothèque Jasypt avec le code suivant:
StandardPBEStringEncryptor sha256 = new StandardPBEStringEncryptor();
sha256.setPassword(key);
sha256.setAlgorithm("PBEWithHmacSHA2");
En essayant d'utiliser sha256.encrypt (string) Je reçois cette erreur:
Exception in thread "main" org.jasypt.exceptions.EncryptionInitializationException: java.security.NoSuchAlgorithmException: PBEWithHmacAndSHA256 SecretKeyFactory not available at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.initialize(StandardPBEByteEncryptor.java:597) at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.initialize(StandardPBEStringEncryptor.java:488) at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.encrypt(StandardPBEStringEncryptor.java:541) at nysenateapi.XmlRpc.main(XmlRpc.java:52) Caused by: java.security.NoSuchAlgorithmException: PBEWithHmacAndSHA256 SecretKeyFactory not available at javax.crypto.SecretKeyFactory.(DashoA13*..) at javax.crypto.SecretKeyFactory.getInstance(DashoA13*..) at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.initialize(StandardPBEByteEncryptor.java:584) ... 3 more
J'ai téléchargé l'extension Cryptographie et placé les JCE pots dans mon Buildpath, mais cela ne semble pas avoir rien fait. Je l'ai essayé d'utiliser un certain nombre de combinaisons dans setAlgorithm ci-dessus, y compris "PBE", "PBEWithSha". (1 | 2 | 128 | 256) ?, "PBEWithHmacSha", etc
J'ai aussi essayé d'utiliser BouncyCastle mais je n'a pas eu de chance non plus. Toute aide ou des conseils appréciés!
La solution
Comme souligné à juste titre par @Rook vous devez spécifier un algorithme de PBE qui comprend un cryptage algorithme. Deux exemples parmi tant d'autres sont "PBEWithSHA1AndDESede"
qui est pris en charge par le fournisseur de SunJCE et "PBEWITHSHA256AND128BITAES-CBC-BC"
qui est pris en charge par le fournisseur bouncycastle JCE.
Autres conseils
Les commentaires ont été utiles, mais je suppose que je pose la mauvaise question. Ce que je cherchais à faire était mimer la fonction PHP hash_hmac ( « SHA256 », chaîne, clé) ...
J'ai fini en utilisant le code suivant:
Mac mac = Mac.getInstance("HmacSha256");
SecretKeySpec secret = new SecretKeySpec(key.getBytes(), "HmacSha256");
mac.init(secret);
byte[] shaDigest = mac.doFinal(phrase.getBytes());
String hash = "";
for(byte b:shaDigest) {
hash += String.format("%02x",b);
}
Merci pour les conseils, cependant. Me aidera sûrement à l'avenir.