Como comparar hashing com dados String?

1 resposta
Jefersonquagliottole
package teste;

import java.security.NoSuchAlgorithmException;

import java.security.spec.InvalidKeySpecException;
import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.PBEKeySpec;

import javax.swing.JOptionPane;
public class Testebd2

{

private static byte[] er;

private static byte[] er2;
protected static byte[] getEr2() {
	return er2;
}
protected static void setEr2(byte[] er2) {
	Testebd2.er2 = er2;
}
protected static byte[] getEr() {
	return er;
}
protected static void setEr(byte[] er) {
	Testebd2.er = er;
}
protected static byte[] hashPassword(char[] password, byte[] salt, int iterations, int keyLength ) {
	 
	password = "555".toCharArray();
	salt = "5".getBytes();
	iterations = 2;
	keyLength = 2;
	
       try {
    	   SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
           PBEKeySpec spec = new PBEKeySpec( password, salt, iterations, keyLength );
           SecretKey key = skf.generateSecret( spec );
           byte[] res = key.getEncoded();
           setEr(res);
           return res;
           
       } catch( NoSuchAlgorithmException | InvalidKeySpecException e ) {
           throw new RuntimeException( e );
       }
	}

protected static byte[] hashPassword2(char[] password, byte[] salt, int iterations, int keyLength ) {
	 
	password = "555".toCharArray();
	salt = "5".getBytes();
	iterations = 2;
	keyLength = 2;
	
       try {
    	   SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
           PBEKeySpec spec = new PBEKeySpec( password, salt, iterations, keyLength );
           SecretKey key = skf.generateSecret( spec );
           byte[] res = key.getEncoded();
           setEr2(res);
           return res;
           
       } catch( NoSuchAlgorithmException | InvalidKeySpecException e ) {
           throw new RuntimeException( e );
       }
	}

public static void main(String[] args){

	hashPassword(null, null, 0, 0);
	hashPassword2(null, null, 0, 0);
	
	JOptionPane.showMessageDialog(null, "valor 1 "+getEr()+" valor 2 "+getEr2());

}

1 Resposta

Jefersonquagliottole

Fiz esse código mas os valores finais percebi que são diferente, desconheço essa parte de segurança, o fato é como comparar um dado que entra em hashing com uma senha string.

Criado 26 de janeiro de 2016
Ultima resposta 26 de jan. de 2016
Respostas 1
Participantes 1