001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.pki;
028:
029: /**
030: * This class implements miscellaneous utility methods including
031: * those used for conversion of BigIntegers to byte arrays,
032: * hexadecimal printing of byte arrays etc.
033: */
034: public class Utils {
035:
036: /** Hexadecimal digits. */
037: private static char[] hc = { '0', '1', '2', '3', '4', '5', '6',
038: '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
039:
040: /**
041: * Converts a subsequence of bytes in a byte array into a
042: * corresponding string of hexadecimal digits, each separated by a ":".
043: *
044: * @param b byte array containing the bytes to be converted
045: * @param off starting offset of the byte subsequence inside b
046: * @param len number of bytes to be converted
047: * @return a string of corresponding hexadecimal digits or
048: * an error string
049: */
050: public static String hexEncode(byte[] b, int off, int len) {
051: return new String(hexEncodeToChars(b, off, len));
052: }
053:
054: /**
055: * Converts a subsequence of bytes in a byte array into a
056: * corresponding string of hexadecimal digits, each separated by a ":".
057: *
058: * @param b byte array containing the bytes to be converted
059: * @param off starting offset of the byte subsequence inside b
060: * @param len number of bytes to be converted
061: * @return a string of corresponding hexadecimal digits or
062: * an error string
063: */
064: public static char[] hexEncodeToChars(byte[] b, int off, int len) {
065: char[] r;
066: int v;
067: int i;
068: int j;
069:
070: if ((b == null) || (len == 0)) {
071: return new char[0];
072: }
073:
074: if ((off < 0) || (len < 0)) {
075: throw new ArrayIndexOutOfBoundsException();
076: }
077:
078: if (len == 1) {
079: r = new char[len * 2];
080: } else {
081: r = new char[(len * 3) - 1];
082: }
083:
084: for (i = 0, j = 0;;) {
085: v = b[off + i] & 0xff;
086: r[j++] = hc[v >>> 4];
087: r[j++] = hc[v & 0x0f];
088:
089: i++;
090: if (i >= len) {
091: break;
092: }
093:
094: r[j++] = ':';
095: }
096:
097: return r;
098: }
099:
100: /**
101: * Converts a byte array into a corresponding string of hexadecimal
102: * digits. This is equivalent to hexEncode(b, 0, b.length).
103: * <P />
104: * @param b byte array to be converted
105: * @return corresponding hexadecimal string
106: */
107: public static String hexEncode(byte[] b) {
108: if (b == null)
109: return ("");
110: else
111: return hexEncode(b, 0, b.length);
112: }
113:
114: /**
115: * Converts a long value to a cooresponding 8-byte array
116: * starting with the most significant byte.
117: * <P />
118: * @param n 64-bit long integer value
119: * @return a corresponding 8-byte array in network byte order
120: */
121: public static byte[] longToBytes(long n) {
122: byte[] b = new byte[8];
123:
124: for (int i = 0; i < 64; i += 8) {
125: b[i >> 3] = (byte) ((n >> (56 - i)) & 0xff);
126: }
127: return b;
128: }
129:
130: /**
131: * Checks if two byte arrays match.
132: * <P />
133: * @param a first byte array
134: * @param aOff starting offset for comparison within a
135: * @param b second byte array
136: * @param bOff starting offset for comparison within b
137: * @param len number of bytes to be compared
138: * @return true if the sequence of len bytes in a starting at
139: * aOff matches those in b starting at bOff, false otherwise
140: */
141: public static boolean byteMatch(byte[] a, int aOff, byte[] b,
142: int bOff, int len) {
143: if ((a.length < aOff + len) || (b.length < bOff + len))
144: return false;
145:
146: for (int i = 0; i < len; i++) {
147: if (a[i + aOff] != b[i + bOff])
148: return false;
149: }
150:
151: return true;
152: }
153: }
|