Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created July 4, 2019 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/7c46e4fb688e8ec551fbfbe939fcc1ed to your computer and use it in GitHub Desktop.
Save parzibyte/7c46e4fb688e8ec551fbfbe939fcc1ed to your computer and use it in GitHub Desktop.
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
public class Main {
public static byte[] obtenerChecksum(String nombreArchivo) throws Exception {
InputStream fis = new FileInputStream(nombreArchivo);
byte[] buffer = new byte[1024];
MessageDigest complete = MessageDigest.getInstance("MD5");
int numRead;
// Leer el archivo pedazo por pedazo
do {
// Leer datos y ponerlos dentro del búfer
numRead = fis.read(buffer);
// Si se leyó algo, se actualiza el MessageDigest
if (numRead > 0) {
complete.update(buffer, 0, numRead);
}
} while (numRead != -1);
fis.close();
// Devolver el arreglo de bytes
return complete.digest();
}
public static String obtenerMD5ComoString(String nombreArchivo) throws Exception {
// Convertir el arreglo de bytes a cadena
byte[] b = obtenerChecksum(nombreArchivo);
StringBuilder resultado = new StringBuilder();
for (byte unByte : b) {
resultado.append(Integer.toString((unByte & 0xff) + 0x100, 16).substring(1));
}
return resultado.toString();
}
public static void main(String args[]) {
// El nombre de archivo del que vas a obtener la suma de verificación MD5
String nombreArchivo = "D:\\materiales.json";
try {
String checksum = obtenerMD5ComoString(nombreArchivo);
System.out.println("El MD5 checksum de " + nombreArchivo + " es " + checksum);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment