Pessoal,
Gostaria de uma ajuda de vcs para eu gerar um hash md5 em hexadecimal de um aquivo .csv
[code]
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
public class GerarHashparaCSV {
public static void main(String[] args) throws Exception {
MessageDigest md = MessageDigest.getInstance(“MD5”);
BufferedInputStream in = new BufferedInputStream(new FileInputStream(“c:/servidorPostgres.csv”));
StringBuilder hexFileCSV = new StringBuilder();
int theByte = 0;
while ((theByte = in.read()) != -1) {
md.update((byte) theByte);
}
in.close();
byte[] theDigest = md.digest();
for(int i = 0; i < theDigest.length; i++){
hexFileCSV.append(Integer.toHexString(theDigest[i]));
}
String saida = hexFileCSV.toString();
OutputStream output = new FileOutputStream(new File("c:/serv.csv"));
output.write(saida.getBytes());
output.flush();
output.close();
}
}[/code]
Só que o hash calculado pelo código a acima esta gerando errado exemplo: hash do aquivo c:/serv.csv que é ffffffcf26ffffff9f0fffffff7fffffff3fffffff4ffffffe6ffffff8cffffffccffffffe5ffffff9fffffffa26653. (gerado errado)
Ao compara o hash de saida do aquivo c:/serv.csv em um programinha que calcula o hash em hexadecimal da 5dbb9b1fcd7620f2bcdbf6f0b8704856. (gerado certo)
Meu código esta gerando o hash hexdacimal errado, o que devo esta errando no meu código ?
Obrigado.
O verbo é “estar” e não “esta”.
Isto posto, não se lê um arquivo um byte de cada vez, porque isso é lento e pode dar problemas. Normalmente eu faço o seguinte:
package guj;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* Sintaxe: java -cp . guj.Hasher algoritmo arquivo
* Exemplo: java -cp . guj.Hasher md5 arquivo.csv
*/
public class Hasher {
private static char[] hexDigits = "0123456789ABCDEF".toCharArray();
public static String hex (byte[] digest) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < digest.length; i++) {
sb.append(hexDigits[(digest[i] >> 4) & 0xF]);
sb.append(hexDigits[digest[i] & 0xF]);
}
return sb.toString();
}
public void hash (String algorithm, String filename) throws NoSuchAlgorithmException, IOException {
MessageDigest digest = MessageDigest.getInstance(algorithm);
DigestInputStream dis = new DigestInputStream (new FileInputStream (filename), digest);
byte[] bytes = new byte[1024];
while (dis.read(bytes) > 0) {
};
dis.close();
// Imprimir o hash
System.out.println (hex (dis.getMessageDigest().digest()));
}
public static void main (String[] args) throws Exception {
Hasher hasher = new Hasher ();
hasher.hash (args[0], args[1]);
}
}
Testei o programa acima com os seguintes hashes: md5, sha1, sha-256, sha-384, sha-512 e conferi com o OpenSSL.
Obrigado pela dica de vcs. Resolvi da seguinte forma:
[code]
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
public class GerarHashparaCSV {
public static void main(String[] args) throws Exception {
MessageDigest md = MessageDigest.getInstance(“MD5”);
BufferedInputStream in = new BufferedInputStream(new FileInputStream(“c:/servidorPostgres.csv”));
StringBuilder hexFileCSV = new StringBuilder();
int theByte = 0;
while ((theByte = in.read()) != -1) {
// chamar o método update() para passar os dados a serem criptografados
md.update((byte) theByte);
}
in.close();
byte[] theDigest = md.digest();
for(int i = 0; i < theDigest.length; i++){
if ((0xff & theDigest[i]) < 0x10){
hexFileCSV.append("0" + Integer.toHexString((0xFF & theDigest[i])));
} else {
hexFileCSV.append(Integer.toHexString(0xFF & theDigest[i]));
}
}
OutputStream output = new FileOutputStream(new File("c:/servidorHash.csv"));
output.write(hexFileCSV.toString().getBytes());
output.flush();
output.close();
System.out.println("Saiu");
}
}[/code]