001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: package gov.nist.core;
026:
027: import com.sun.midp.crypto.*;
028:
029: import com.sun.midp.log.Logging;
030: import com.sun.midp.log.LogChannels;
031:
032: /**
033: * A set of utilities that compensate for things that are missing in CLDC 1.0
034: * @version 1.0
035: */
036: public class Utils {
037:
038: /** Instatce of MessageDigest */
039: private static MessageDigest messageDigest;
040:
041: /** Instatce loads once */
042: static {
043: try {
044: messageDigest = MessageDigest.getInstance("MD5");
045: } catch (NoSuchAlgorithmException ex) {
046: if (Logging.REPORT_LEVEL <= Logging.ERROR) {
047: Logging.report(Logging.ERROR, LogChannels.LC_JSR180,
048: "Exception on MessageDigest instance creating "
049: + ex);
050: }
051: }
052: }
053:
054: /**
055: * Do an MD5 Digest.
056: *
057: * @param digestBytes input data
058: * @return MD5 hash value of the input data
059: */
060: public static byte[] digest(byte[] digestBytes) {
061: byte[] returnValue;
062:
063: returnValue = new byte[messageDigest.getDigestLength()];
064: messageDigest.update(digestBytes, 0, digestBytes.length);
065:
066: try {
067: messageDigest.digest(returnValue, 0, returnValue.length);
068: } catch (DigestException de) {
069: // nothing to do
070: }
071:
072: return returnValue;
073: }
074:
075: /**
076: * Generate a tag for a FROM header or TO header. Just return a
077: * random 4 digit integer (should be enough to avoid any clashes!)
078: * @return a string that can be used as a tag parameter.
079: */
080: public static String generateTag() {
081: return new Long(System.currentTimeMillis()).toString();
082: }
083:
084: /**
085: * to hex converter
086: */
087: private static final char[] toHex = { '0', '1', '2', '3', '4', '5',
088: '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
089:
090: /**
091: * Compares two strings lexicographically, ignoring case
092: * considerations.
093: * @param s1 string to compare
094: * @param s2 string to compare.
095: * @return 1, -1, 0 as in compare To
096: */
097: public static int compareToIgnoreCase(String s1, String s2) {
098: // System.out.println(s1+" "+s2);
099: String su1 = s1.toUpperCase();
100: String su2 = s2.toUpperCase();
101: return su1.compareTo(su2);
102: }
103:
104: /**
105: * Compares two strings lexicographically.
106: * @param s1 string to compare
107: * @param s2 string to compare.
108: * @return 1,-1,0 as in compare To
109: */
110: public static boolean equalsIgnoreCase(String s1, String s2) {
111: return s1.toLowerCase().equals(s2.toLowerCase());
112: }
113:
114: /**
115: * convert an array of bytes to an hexadecimal string
116: * @return a string
117: * @param b bytes array to convert to a hexadecimal
118: * string
119: */
120:
121: public static String toHexString(byte b[]) {
122: int pos = 0;
123: char[] c = new char[b.length * 2];
124: for (int i = 0; i < b.length; i++) {
125: c[pos++] = toHex[(b[i] >> 4) & 0x0F];
126: c[pos++] = toHex[b[i] & 0x0f];
127: }
128: return new String(c);
129: }
130:
131: /**
132: * Put quotes around a string and return it.
133: * @return a quoted string
134: * @param str string to be quoted
135: */
136: public static String getQuotedString(String str) {
137: return '"' + str + '"';
138: }
139:
140: /**
141: * Squeeze out white space from a string and return the reduced
142: * string.
143: * @param input input string to sqeeze.
144: * @return String a reduced string.
145: */
146: public static String reduceString(String input) {
147: String newString = input.toLowerCase();
148: int len = newString.length();
149: String retval = "";
150: for (int i = 0; i < len; i++) {
151: if (newString.charAt(i) == ' '
152: || newString.charAt(i) == '\t')
153: continue;
154: else
155: retval += newString.charAt(i);
156: }
157: return retval;
158: }
159: }
|