package br.ufrgs.inf.dsmoura.repository.controller.util; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class SecurityUtil { public static String generateHash(String plaintext) { try { StringBuilder hash = new StringBuilder(); MessageDigest sha = MessageDigest.getInstance("SHA-1"); byte[] hashedBytes = sha.digest(plaintext.getBytes()); char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; for (int idx = 0; idx < hashedBytes.length; ++idx) { byte b = hashedBytes[idx]; hash.append(digits[(b & 0xf0) >> 4]); hash.append(digits[b & 0x0f]); } return hash.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } }