1 package twcsckernel.serverKernel.utils; 2 3 import java.security.MessageDigest; 4 5 /*** 6 * Klasa służąca do hashowania haseł użytkowników algorytmem MD5. 7 * 8 * @author VMD Group 9 * 10 */ 11 public class Encoder { 12 /*** 13 * Metoda zwraca MD5(parametr) 14 * @param input - łańcuch wejściowy 15 * @return - łańcuch wyjściowy (hash MD5) 16 */ 17 public static String hashPassword(String input) { 18 MessageDigest md = null; 19 String hex; 20 StringBuffer out = new StringBuffer(32); 21 try { 22 md = MessageDigest.getInstance("MD5"); 23 } catch (Exception e) { 24 return null; 25 } 26 final byte[] inputBytes = input.getBytes(); 27 final byte[] outputBytes = md.digest(inputBytes); 28 for (int i = 0; i < outputBytes.length; i++) { 29 hex = Integer.toHexString(((int) outputBytes[i]) & 0xff); 30 if (hex.length() == 1) 31 hex = "0" + hex; 32 out.append(hex); 33 } 34 return out.toString(); 35 } 36 37 public static void main(String argv[]) { 38 System.out.println(hashPassword("haslo")); 39 } 40 41 }