1
2
3
4
5 package org.musicontroller.security;
6
7 import java.security.MessageDigest;
8 import java.security.NoSuchAlgorithmException;
9
10
11
12
13
14
15 public class Encrypter {
16
17
18
19
20
21
22 public static String encrypt(String unencrypted) {
23 if (unencrypted==null) { unencrypted=""; };
24 MessageDigest md;
25 try {
26 md = MessageDigest.getInstance("SHA");
27 } catch (NoSuchAlgorithmException e) {
28 return e.getLocalizedMessage();
29 }
30 byte[] bytes = md.digest(unencrypted.getBytes());
31 StringBuffer hexString = new StringBuffer();
32 for (int i=0;i<bytes.length;i++) {
33 String hexdigit =Integer.toHexString(0xFF & bytes[i]);
34 if (hexdigit.length()==1) {
35 hexdigit = '0' + hexdigit;
36 }
37 hexString.append(hexdigit);
38 }
39 return hexString.toString();
40 }
41
42 }