View Javadoc

1   package org.apache.fulcrum.jce.crypto;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  
23  /**
24   * Helper class to for HEX conversion.
25   *
26   * The code uses parts from Markus Hahn's Blowfish library found at
27   * http://blowfishj.sourceforge.net/
28   *
29   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl </a>
30   * @author <a href="mailto:maakus@earthlink.net">Markus Hahn</a>
31   */
32  
33  public final class HexConverter
34  {
35      /**
36       * Table for byte to hex conversion
37       */
38      final private static char[] HEXTAB =
39      {
40          '0', '1', '2', '3', '4', '5', '6', '7',
41          '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
42      };
43  
44      /**
45       * Converts a byte array to a hex string.
46       *
47       * @param data the byte array
48       * @return the hex string
49       */
50      public final static String toString( byte[] data )
51      {
52          return bytesToHexStr(data, 0, data.length);
53      }
54  
55      /**
56       * Converts a hex string into a byte[]
57       *
58       * @param data the hex string
59       * @return the byte[]
60       */
61  
62      public final static byte[] toBytes( String data )
63      {
64          byte[] result = new byte[data.length()/2];
65          hexStrToBytes( data, result, 0, 0, result.length );
66          return result;
67      }
68  
69      /**
70       * Converts a byte array to a hex string.
71       * @param data the byte array
72       * @param nOfs start index where to get the bytes
73       * @param nLen number of bytes to convert
74       * @return the hex string
75       */
76      private final static String bytesToHexStr(
77          byte[] data,
78          int nOfs,
79          int nLen)
80      {
81          StringBuffer sbuf;
82  
83          sbuf = new StringBuffer();
84          sbuf.setLength(nLen << 1);
85  
86          int nPos = 0;
87          int nC = nOfs + nLen;
88  
89          while (nOfs < nC)
90          {
91              sbuf.setCharAt(nPos++, HEXTAB[(data[nOfs  ] >> 4) & 0x0f]);
92              sbuf.setCharAt(nPos++, HEXTAB[ data[nOfs++]       & 0x0f]);
93          }
94  
95          return sbuf.toString();
96      }
97  
98      /**
99       * Converts a hex string back into a byte array (invalid codes will be
100      * skipped).
101      * @param sHex hex string
102      * @param data the target array
103      * @param nSrcOfs from which character in the string the conversion should
104      * begin, remember that (nSrcPos modulo 2) should equals 0 normally
105      * @param nDstOfs to store the bytes from which position in the array
106      * @param nLen number of bytes to extract
107      * @return number of extracted bytes
108      */
109     private final static int hexStrToBytes(
110         String sHex,
111         byte[] data,
112         int nSrcOfs,
113         int nDstOfs,
114         int nLen)
115     {
116         int nI, nJ, nStrLen, nAvailBytes, nDstOfsBak;
117         byte bActByte;
118         boolean blConvertOK;
119 
120         // check for correct ranges
121 
122         nStrLen = sHex.length();
123 
124         nAvailBytes = (nStrLen - nSrcOfs) >> 1;
125         if (nAvailBytes < nLen)
126         {
127             nLen = nAvailBytes;
128         }
129 
130         int nOutputCapacity = data.length - nDstOfs;
131         if (nLen > nOutputCapacity)
132         {
133             nLen = nOutputCapacity;
134         }
135 
136         // convert now
137 
138         nDstOfsBak = nDstOfs;
139 
140         for (nI = 0; nI < nLen; nI++)
141         {
142             bActByte = 0;
143             blConvertOK = true;
144 
145             for (nJ = 0; nJ < 2; nJ++)
146             {
147                 bActByte <<= 4;
148                 char cActChar = sHex.charAt(nSrcOfs++);
149 
150                 if ((cActChar >= 'a') && (cActChar <= 'f'))
151                 {
152                     bActByte |= (byte) (cActChar - 'a') + 10;
153                 }
154                 else
155                 {
156                     if ((cActChar >= '0') && (cActChar <= '9'))
157                     {
158                         bActByte |= (byte) (cActChar - '0');
159                     }
160                     else
161                     {
162                         blConvertOK = false;
163                     }
164                 }
165             }
166             if (blConvertOK)
167             {
168                 data[nDstOfs++] = bActByte;
169             }
170         }
171 
172         return (nDstOfs - nDstOfsBak);
173     }
174 }