30 |
23 Feb 05 |
nicklas |
1 |
/* |
192 |
17 Mar 05 |
jari |
$Id$ |
30 |
23 Feb 05 |
nicklas |
3 |
|
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2005 Jari Häkkinen, Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson |
30 |
23 Feb 05 |
nicklas |
6 |
|
2304 |
22 May 06 |
jari |
This file is part of BASE - BioArray Software Environment. |
2304 |
22 May 06 |
jari |
Available at http://base.thep.lu.se/ |
30 |
23 Feb 05 |
nicklas |
9 |
|
30 |
23 Feb 05 |
nicklas |
BASE is free software; you can redistribute it and/or |
30 |
23 Feb 05 |
nicklas |
modify it under the terms of the GNU General Public License |
4479 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
30 |
23 Feb 05 |
nicklas |
of the License, or (at your option) any later version. |
30 |
23 Feb 05 |
nicklas |
14 |
|
30 |
23 Feb 05 |
nicklas |
BASE is distributed in the hope that it will be useful, |
30 |
23 Feb 05 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
30 |
23 Feb 05 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
30 |
23 Feb 05 |
nicklas |
GNU General Public License for more details. |
30 |
23 Feb 05 |
nicklas |
19 |
|
30 |
23 Feb 05 |
nicklas |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
30 |
23 Feb 05 |
nicklas |
22 |
*/ |
49 |
28 Feb 05 |
nicklas |
23 |
package net.sf.basedb.util; |
30 |
23 Feb 05 |
nicklas |
24 |
|
2884 |
09 Nov 06 |
martin |
25 |
import net.sf.basedb.core.BaseException; |
2884 |
09 Nov 06 |
martin |
26 |
|
30 |
23 Feb 05 |
nicklas |
27 |
import java.security.MessageDigest; |
30 |
23 Feb 05 |
nicklas |
28 |
import java.math.BigInteger; |
7714 |
21 May 19 |
nicklas |
29 |
import java.nio.charset.StandardCharsets; |
30 |
23 Feb 05 |
nicklas |
30 |
|
30 |
23 Feb 05 |
nicklas |
31 |
/** |
30 |
23 Feb 05 |
nicklas |
This class calculates the MD5 hash of a string and |
30 |
23 Feb 05 |
nicklas |
returns it represented as a hexadecimal string. |
30 |
23 Feb 05 |
nicklas |
34 |
|
30 |
23 Feb 05 |
nicklas |
@author Nicklas |
30 |
23 Feb 05 |
nicklas |
@version 2.0 |
30 |
23 Feb 05 |
nicklas |
37 |
*/ |
30 |
23 Feb 05 |
nicklas |
38 |
public class MD5 |
30 |
23 Feb 05 |
nicklas |
39 |
{ |
4951 |
27 May 09 |
nicklas |
40 |
/** |
4951 |
27 May 09 |
nicklas |
Calculates the MD5 hash of an UTF-8 encoded string. |
4951 |
27 May 09 |
nicklas |
42 |
*/ |
30 |
23 Feb 05 |
nicklas |
43 |
public static String getHashString(String in) |
30 |
23 Feb 05 |
nicklas |
44 |
{ |
30 |
23 Feb 05 |
nicklas |
45 |
try |
30 |
23 Feb 05 |
nicklas |
46 |
{ |
30 |
23 Feb 05 |
nicklas |
47 |
MessageDigest md5 = MessageDigest.getInstance("MD5"); |
7714 |
21 May 19 |
nicklas |
48 |
BigInteger result = new BigInteger(1, md5.digest(in.getBytes(StandardCharsets.UTF_8))); |
2243 |
11 May 06 |
nicklas |
49 |
return leftPad(result.toString(16), '0', 32); |
30 |
23 Feb 05 |
nicklas |
50 |
} |
30 |
23 Feb 05 |
nicklas |
51 |
catch (Exception ex) |
30 |
23 Feb 05 |
nicklas |
52 |
{ |
2884 |
09 Nov 06 |
martin |
53 |
throw new BaseException(ex); |
30 |
23 Feb 05 |
nicklas |
54 |
} |
30 |
23 Feb 05 |
nicklas |
55 |
} |
710 |
02 Jun 05 |
nicklas |
56 |
|
710 |
02 Jun 05 |
nicklas |
57 |
public static MessageDigest newInstance() |
710 |
02 Jun 05 |
nicklas |
58 |
{ |
710 |
02 Jun 05 |
nicklas |
59 |
try |
710 |
02 Jun 05 |
nicklas |
60 |
{ |
710 |
02 Jun 05 |
nicklas |
61 |
return MessageDigest.getInstance("MD5"); |
710 |
02 Jun 05 |
nicklas |
62 |
} |
710 |
02 Jun 05 |
nicklas |
63 |
catch (Exception ex) |
710 |
02 Jun 05 |
nicklas |
64 |
{ |
2884 |
09 Nov 06 |
martin |
65 |
throw new BaseException(ex); |
710 |
02 Jun 05 |
nicklas |
66 |
} |
710 |
02 Jun 05 |
nicklas |
67 |
} |
710 |
02 Jun 05 |
nicklas |
68 |
|
710 |
02 Jun 05 |
nicklas |
69 |
public static String getHashString(MessageDigest md5) |
710 |
02 Jun 05 |
nicklas |
70 |
{ |
710 |
02 Jun 05 |
nicklas |
71 |
BigInteger result = new BigInteger(1, md5.digest()); |
2243 |
11 May 06 |
nicklas |
72 |
return leftPad(result.toString(16), '0', 32); |
710 |
02 Jun 05 |
nicklas |
73 |
} |
710 |
02 Jun 05 |
nicklas |
74 |
|
2243 |
11 May 06 |
nicklas |
75 |
/** |
2243 |
11 May 06 |
nicklas |
If the string is shorter than the specified length, it is left padded |
2243 |
11 May 06 |
nicklas |
with the specified character. If the string is null or longer than the specified |
2243 |
11 May 06 |
nicklas |
length it is returned unchanged. |
2243 |
11 May 06 |
nicklas |
79 |
|
2243 |
11 May 06 |
nicklas |
@param toPad The string to pad |
2243 |
11 May 06 |
nicklas |
@param c The character to pad with |
2243 |
11 May 06 |
nicklas |
@param length The length of the resulting string |
2243 |
11 May 06 |
nicklas |
@return The left-padded string |
2243 |
11 May 06 |
nicklas |
84 |
*/ |
2243 |
11 May 06 |
nicklas |
85 |
public static String leftPad(String toPad, char c, int length) |
2243 |
11 May 06 |
nicklas |
86 |
{ |
2243 |
11 May 06 |
nicklas |
87 |
|
2243 |
11 May 06 |
nicklas |
88 |
if (toPad != null && toPad.length() < length) |
2243 |
11 May 06 |
nicklas |
89 |
{ |
2243 |
11 May 06 |
nicklas |
90 |
int numToPad = length - toPad.length(); |
2243 |
11 May 06 |
nicklas |
91 |
char[] padding = new char[numToPad]; |
2243 |
11 May 06 |
nicklas |
92 |
for (int i = 0; i < numToPad; ++i) |
2243 |
11 May 06 |
nicklas |
93 |
{ |
2243 |
11 May 06 |
nicklas |
94 |
padding[i] = c; |
2243 |
11 May 06 |
nicklas |
95 |
} |
2243 |
11 May 06 |
nicklas |
96 |
toPad = new String(padding) + toPad; |
2243 |
11 May 06 |
nicklas |
97 |
} |
2243 |
11 May 06 |
nicklas |
98 |
return toPad; |
2243 |
11 May 06 |
nicklas |
99 |
} |
2243 |
11 May 06 |
nicklas |
100 |
|
30 |
23 Feb 05 |
nicklas |
101 |
} |