Last active
January 3, 2024 20:11
-
-
Save hsmiranda/0f82647002c8e01ee92d3362be23b813 to your computer and use it in GitHub Desktop.
PBKDF2Example.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.NoSuchAlgorithmException; | |
import java.security.spec.InvalidKeySpecException; | |
import javax.crypto.SecretKeyFactory; | |
import javax.crypto.spec.PBEKeySpec; | |
import java.util.Base64; | |
/* | |
Este código usa a classe `SecretKeyFactory` para gerar um hash de senha usando o algoritmo PBKDF2 com HMAC-SHA256. O hash é gerado a partir de uma senha e um salt aleatório. O número de iterações e o comprimento da chave são definidos como 65536 e 256, respectivamente. O hash resultante é codificado em Base64 e impresso na tela. | |
Espero que isso ajude! Se você tiver alguma dúvida, por favor, não hesite em perguntar. | |
Origem: conversa com o Bing, 1/3/2024 | |
(1) Java Hashing using MD5, SHA, PBKDF2, Bcrypt and Scrypt - HowToDoInJava. https://howtodoinjava.com/java/java-security/how-to-generate-secure-password-hash-md5-sha-pbkdf2-bcrypt-examples/. | |
(2) How to use PBKDF2 in java to get the same result as c#?. https://stackoverflow.com/questions/63624541/how-to-use-pbkdf2-in-java-to-get-the-same-result-as-c. | |
(3) SebastianDeiss/Java-PBKDF2 - GitHub. https://github.com/SebastianDeiss/Java-PBKDF2. | |
(4) Using PBKDF2 for hash and AES key generation implementation. https://security.stackexchange.com/questions/179204/using-pbkdf2-for-hash-and-aes-key-generation-implementation. | |
(5) Encryption with PBKDF2 - Knoldus Blogs. https://blog.knoldus.com/encryption-with-pbkdf2/. | |
(6) Password Verification with PBKDF2 in Java - Stack Overflow. https://stackoverflow.com/questions/2375541/password-verification-with-pbkdf2-in-java. | |
(7) Decrypt and encrypt using PBKDF2 java - Stack Overflow. https://stackoverflow.com/questions/42292832/decrypt-and-encrypt-using-pbkdf2-java. | |
(8) undefined. http://howtodoinjava.com/security/how-to-generate-secure-password-hash-md5-sha-pbkdf2-bcrypt-examples/. | |
*/ | |
public class PBKDF2Example { | |
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException { | |
// Define a senha a ser criptografada | |
String password = "senha"; | |
// Define o salt aleatório | |
byte[] salt = new byte[16]; | |
// Cria uma instância da classe SecretKeyFactory para o algoritmo PBKDF2 | |
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); | |
// Define o número de iterações | |
int iterations = 65536; | |
// Define o comprimento da chave | |
int keyLength = 256; | |
// Cria uma instância da classe PBEKeySpec com a senha, o salt, o número de iterações e o comprimento da chave | |
PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, keyLength); | |
// Gera o hash da senha usando o algoritmo PBKDF2 | |
byte[] hash = factory.generateSecret(spec).getEncoded(); | |
// Codifica o hash em Base64 | |
String encodedHash = Base64.getEncoder().encodeToString(hash); | |
// Imprime o hash codificado na tela | |
System.out.println("Hash gerado: " + encodedHash); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment