01: /*
02: * Wilos Is a cLever process Orchestration Software - http://www.wilos-project.org
03: * Copyright (C) 2006-2007 Paul Sabatier University, IUP ISI (Toulouse, France) <massie@irit.fr>
04: * Copyright (C) Mathieu BENOIT <mathieu-benoit@hotmail.fr>
05: *
06: * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
07: * General Public License as published by the Free Software Foundation; either version 2 of the License,
08: * or (at your option) any later version.
09: *
10: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
11: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License along with this program; if not,
15: * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16: */
17:
18: package wilos.utils;
19:
20: import java.security.*;
21:
22: public class Security {
23: /**
24: * This method can be used to get the MD5 hach of a String.
25: *
26: * @param key
27: * The String to hach
28: * @return The hached String
29: */
30: public static String encode(String key) {
31: byte[] uniqueKey = key.getBytes();
32: byte[] hash = null;
33:
34: try {
35: hash = MessageDigest.getInstance("MD5").digest(uniqueKey);
36: }
37:
38: catch (NoSuchAlgorithmException e) {
39: throw new Error("no MD5 support in this VM");
40: }
41:
42: StringBuffer hashString = new StringBuffer();
43: for (int i = 0; i < hash.length; ++i) {
44: String hex = Integer.toHexString(hash[i]);
45: if (hex.length() == 1) {
46: hashString.append('0');
47: hashString.append(hex.charAt(hex.length() - 1));
48: } else {
49: hashString.append(hex.substring(hex.length() - 2));
50: }
51: }
52: return hashString.toString();
53: }
54: }
|