Hola, encontramos la solución a lo que estabas buscando, has scroll y la hallarás a continuación.
Ejemplo: contraseña hash de java
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Arrays;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;/**
* Hash passwords for storage, and test passwords against password tokens.
*
* Instances of this class can be used concurrently by multiple threads.
*
* @author erickson
* @see StackOverflow
*/publicfinalclassPasswordAuthentication/**
* Each token produced by this class uses this identifier as a prefix.
*/publicstaticfinalStringID="$31$";/**
* The minimum recommended cost, used by default
*/publicstaticfinalintDEFAULT_COST=16;privatestaticfinalStringALGORITHM="PBKDF2WithHmacSHA1";privatestaticfinalintSIZE=128;privatestaticfinal Pattern layout = Pattern.compile("\$31\$(\d\d?)\$(.43)");privatefinal SecureRandom random;privatefinalint cost;publicPasswordAuthentication()this(DEFAULT_COST);/**
* Create a password manager with a specified cost
*
* @paramcost the exponential computational cost of hashing a password, 0 to 30
*/publicPasswordAuthentication(int cost)iterations(cost);/* Validate cost */
this.cost = cost;
this.random =newSecureRandom();privatestaticintiterations(int cost)(cost >30))thrownewIllegalArgumentException("cost: "+ cost);return1<< cost;/**
* Hash a password for storage.
*
* @returna secure authentication token to be stored for later authentication
*/publicStringhash(char[] password)
byte[] salt =newbyte[SIZE/8];
random.nextBytes(salt);
byte[] dk =pbkdf2(password, salt,1<< cost);
byte[] hash =newbyte[salt.length + dk.length];
System.arraycopy(salt,0, hash,0, salt.length);
System.arraycopy(dk,0, hash, salt.length, dk.length);
Base64.Encoder enc = Base64.getUrlEncoder().withoutPadding();returnID+ cost +'$'+ enc.encodeToString(hash);/**
* Authenticate with a password and a stored password token.
*
* @returntrue if the password and token match
*/public boolean authenticate(char[] password,String token)= hash[salt.length + idx]^ check[idx];return zero ==0;privatestatic byte[]pbkdf2(char[] password, byte[] salt,int iterations)
KeySpec spec =newPBEKeySpec(password, salt, iterations,SIZE);try
SecretKeyFactory f = SecretKeyFactory.getInstance(ALGORITHM);return f.generateSecret(spec).getEncoded();catch(NoSuchAlgorithmException ex)thrownewIllegalStateException("Missing algorithm: "+ALGORITHM, ex);catch(InvalidKeySpecException ex)thrownewIllegalStateException("Invalid SecretKeyFactory", ex);/**
* Hash a password in an immutable @code String.
*
* Passwords should be stored in a @code char[] so that it can be filled
* with zeros after use instead of lingering on the heap and elsewhere.
*
* @deprecated Use @link #hash(char[]) instead
*/
@Deprecated
publicStringhash(String password)returnhash(password.toCharArray());/**
* Authenticate with a password in an immutable @code String and a stored
* password token.
*
* @deprecated Use @link #authenticate(char[],String) instead.
* @see #hash(String)
*/
@Deprecated
public boolean authenticate(String password,String token)returnauthenticate(password.toCharArray(), token);
Si estás de acuerdo, tienes la libertad de dejar un artículo acerca de qué le añadirías a este tutorial.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)