01: /*
02: * MCS Media Computer Software Copyright (c) 2006 by MCS
03: * -------------------------------------- Created on 10.01.2006 by w.klaas
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
06: * use this file except in compliance with the License. You may obtain a copy of
07: * the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14: * License for the specific language governing permissions and limitations under
15: * the License.
16: */
17: package de.mcs.utils;
18:
19: import java.security.MessageDigest;
20: import java.security.NoSuchAlgorithmException;
21:
22: /**
23: * Some helper methode to build and convert a MD5 hash. For building MD5 from
24: * files see de.mcs.utils.Files#computeMD5FromFile(String)
25: *
26: * @author w.klaas
27: *
28: */
29: public final class MD5Utils {
30:
31: /**
32: * to prevent instancing.
33: */
34: private MD5Utils() {
35: }
36:
37: /**
38: * Building the md5 bytes from byte array.
39: *
40: * @param s
41: * data byte array
42: * @return byte[] byte array with md5
43: */
44: public static byte[] md5Bytes(final byte[] s) {
45: // Build MD5-hashcode
46: MessageDigest md;
47: try {
48: md = MessageDigest.getInstance("MD5");
49: md.update(s);
50: byte[] digest = md.digest();
51:
52: return digest;
53: } catch (NoSuchAlgorithmException e) {
54: e.printStackTrace();
55: }
56: return null;
57: }
58:
59: /**
60: * convinient functions to convert a MD5 stirng to the byte array.
61: *
62: * @param md5String
63: * the string to convert.
64: * @return byte[]
65: */
66: public static byte[] md5StringTobytes(final String md5String) {
67: return StringFormat.fromHexString(md5String);
68: }
69:
70: /**
71: * convinient functions to convert a MD5 digest (as byte[]) to a string.
72: *
73: * @param digest
74: * the byte[] to convert.
75: * @return String
76: */
77: public static String md5BytesToString(final byte[] digest) {
78: return StringFormat.toHexString(digest);
79: }
80: }
|