summaryrefslogtreecommitdiffstats
path: root/src/util/DigestUtils.java
blob: 46337159a52fc638481c578291fe47acb70a7e54 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * $RCSFile$
 *
 * Created on 06.12.2006
 * for Project: 
 * by steins
 *
 * (C) 2005-2006 by 
 */
package util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DigestUtils
{
   private static final String HEX_DIGITS = "0123456789abcdef";

   private DigestUtils()
   {
   }

   public static String bin2hex(byte[] bin)
   {
      StringBuilder sb = new StringBuilder(32);
      for (int i = 0; i < bin.length; ++i)
      {
         byte b = bin[i];
         int h = (b & 0xf0) >> 4;
         sb.append(HEX_DIGITS.charAt(h));
         h = b & 0x0f;
         sb.append(HEX_DIGITS.charAt(h));
      }

      return sb.substring(0);
   }

   public static String md5(String s)
   {
      MessageDigest md = null;
      try
      {
         md = MessageDigest.getInstance("MD5");
         md.update(s.getBytes("ISO-8859-1"));
      }
      catch (NoSuchAlgorithmException e)
      {
         e.printStackTrace();
      }
      catch (UnsupportedEncodingException e)
      {
         e.printStackTrace();
      }

      return bin2hex(md.digest());
   }
}