1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import java.io.*; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.Provider; import java.security.Security; public class Hash { public static void main(String[] args) throws FileNotFoundException { String string, hash; byte dataBytes[]; File doc; BufferedReader obj; try { doc = new File("fichero.txt"); obj = new BufferedReader(new FileReader(doc)); while ((string = obj.readLine()) != null) { hash = get_SHA_512_SecurePassword(string, "123"); System.out.println("Fichero hasheado en SHA-512 : " + hash); } } catch (IOException e) { e.printStackTrace(); } } public static String get_SHA_512_SecurePassword(String passwordToHash, String salt) { String generarHash = null; try { MessageDigest md = MessageDigest.getInstance("SHA-512"); md.update(salt.getBytes(StandardCharsets.UTF_8)); byte[] bytes = md.digest(passwordToHash.getBytes(StandardCharsets.UTF_8)); StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); } generarHash = sb.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return generarHash; } } |