1   /* $RCSfile: ByteUtil.java,v $
2    * $Revision: 1.2 $
3    * $Date: 2002/01/04 14:05:40 $
4    * $Author: uwe $
5    * $State: Exp $
6    *
7    * Created on July 12, 2001, 5:20 PM
8    *
9    * Copyright (C) 2001 Uwe Guenther <uwe@cscc.de>
10   *
11   * This file is part of the jhbci JCE-ServiceProvider. The jhbci JCE-
12   * ServiceProvider is a library, written in JavaTM, that should be 
13   * used in HBCI banking applications (clients and may be servers),
14   * to do cryptographic operations.
15   *
16   * The jhbci library is free software; you can redistribute it and/or
17   * modify it under the terms of the GNU Lesser General Public
18   * License as published by the Free Software Foundation; either
19   * version 2.1 of the License, or (at your option) any later version.
20   *
21   * The jhbci library is distributed in the hope that it will be useful,
22   * but WITHOUT ANY WARRANTY; without even the implied warranty of
23   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24   * Lesser General Public License for more details.
25   *
26   * You should have received a copy of the GNU Lesser General Public
27   * License along with this library; if not, write to the Free Software
28   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29   *
30   */
31  
32  package de.cscc.crypto.util;
33  
34  import javax.crypto.IllegalBlockSizeException;
35  
36  /**
37   * ByteUtil Class.
38   * 
39   * Class that holds only static methods to convert bytes and byte arrays to
40   * Strings. These Strings holds the hex or binary representation of the
41   * converted byte or byte arrays. This class can't be instance, because
42   * its constructor is private.
43   *
44   * @author  <a href=mailto:uwe@cscc.de>Uwe G&uuml;nther</a>
45   * @version $Revision: 1.2 $
46   */
47  public final class ByteUtil {
48      
49      /** This class can't be instance, because its constructor is private.*/
50      private ByteUtil() {}
51  
52      /** 
53       * Converts byte to binary representation.
54       *
55       * @param in byte that will be converted.
56       * @return String with hex representation of in.
57       */    
58      public static String toBin(int in){
59          
60          return "0b" + toBinGeneric(in);
61      }
62      
63      /**
64       * Converts byte[] to binary representation.
65       *
66       * @param in byte[] that will be converted.
67       * @return String with hex representation of in.
68       */    
69      public static String toBin(byte[] in){
70  
71          StringBuffer tmp = new StringBuffer();
72          
73          for (int i = 0; i < in.length; i++) {
74              tmp.append(toBin(in[i]) + " ");
75          }
76            
77          return "0b " + tmp.toString();
78      }
79      
80      /** 
81       * Converts byte to binary representation.
82       *
83       * @param in byte that will be converted.
84       * @return String with hex representation of in.
85       */    
86      private static String toBinGeneric(int in){
87      
88          StringBuffer tmp = new StringBuffer();
89          
90          for(int i = 0; i < 8; i++){       
91              tmp.insert(0, ((in >> i) & 0x01) );
92          }
93          
94          return tmp.toString();      
95      }
96      
97      /** 
98       * Converts byte to hex representation.
99       *
100      * @param in byte that will be converted.
101      * @return String with hex representation of in.
102      */    
103     public static String toHex(byte in) {
104         
105         return "0x" + toHexGeneric(in);
106     }
107     
108     /** 
109      * Converts byte[] to hex representation.
110      *
111      * @param in byte[] that will be converted.
112      * @return String with hex representation of in.
113      */    
114     public static String toHex(byte[] in) {
115         
116         StringBuffer tmp = new StringBuffer("0x");
117         
118         for (int i = 0; i < in.length; i++) {
119             tmp.append(toHexGeneric(in[i]));
120         }
121         
122         return tmp.toString();
123     }
124     
125     /**
126      * Converts byte to binary representation.
127      *
128      * @param in byte that will be converted.
129      * @return String with hex representation of in.
130      */    
131     private static String toHexGeneric(byte in){
132         
133         String tmp = "";
134         
135         byte b = (byte) (in  & 0x000000f0);
136         
137         switch (b){
138             
139             case 0x00:
140                 tmp += "0";
141                 break;
142                 
143             case 0x10:
144                 tmp += "1";
145                 break;
146                 
147             case 0x20:
148                 tmp += "2";
149                 break;
150                 
151             case 0x30:
152                 tmp += "3";
153                 break;
154                 
155             case 0x40:
156                 tmp += "4";
157                 break;
158                 
159             case 0x50:
160                 tmp += "5";
161                 break;
162                 
163             case 0x60:
164                 tmp += "6";
165                 break;
166                 
167             case 0x70:
168                 tmp += "7";
169                 break;
170                 
171             case (byte)0x80:
172                 tmp += "8";
173                 break;
174                 
175             case (byte)0x90:
176                 tmp += "9";
177                 break;
178                 
179             case (byte)0xa0:
180                 tmp += "a";
181                 break;
182                 
183             case (byte)0xb0:
184                 tmp += "b";
185                 break;
186                 
187             case (byte)0xc0:
188                 tmp += "c";
189                 break;
190                 
191             case (byte)0xd0:
192                 tmp += "d";
193                 break;
194                 
195             case (byte)0xe0:
196                 tmp += "e";
197                 break;
198                 
199             case (byte)0xf0:
200                 tmp += "f";
201                 break;
202                 
203         }
204         
205         b = (byte)(in  & 0x0000000f);
206         
207         switch (b){
208             
209             case 0x00:
210                 tmp += "0";
211                 break;
212                 
213             case 0x01:
214                 tmp += "1";
215                 break;
216                 
217             case 0x02:
218                 tmp += "2";
219                 break;
220                 
221             case 0x03:
222                 tmp += "3";
223                 break;
224                 
225             case 0x04:
226                 tmp += "4";
227                 break;
228                 
229             case 0x05:
230                 tmp += "5";
231                 break;
232                 
233             case 0x06:
234                 tmp += "6";
235                 break;
236                 
237             case 0x07:
238                 tmp += "7";
239                 break;
240                 
241             case 0x08:
242                 tmp += "8";
243                 break;
244                 
245             case 0x09:
246                 tmp += "9";
247                 break;
248                 
249             case 0x0a:
250                 tmp += "a";
251                 break;
252                 
253             case 0x0b:
254                 tmp += "b";
255                 break;
256                 
257             case 0x0c:
258                 tmp += "c";
259                 break;
260                 
261             case 0x0d:
262                 tmp += "d";
263                 break;
264                 
265             case 0x0e:
266                 tmp += "e";
267                 break;
268                 
269             case 0x0f:
270                 tmp += "f";
271                 break;
272                 
273         }
274        
275         return tmp;
276     }
277     
278     /**
279      * Converts byte[8] to long representation.
280      *
281      * @param input byte[8] that will be converted.
282      * @param inputOffset the offset in input where input starts.
283      * @throws IllegalBlockSizeException if input length lesser than 8.
284      * @return the long representation.
285      */    
286     public static long toLong(byte[] input, int inputOffset)
287     throws IllegalBlockSizeException
288     {
289         if (input.length-inputOffset < 8){
290             throw new IllegalBlockSizeException(
291                 "Usable byte range is " + (input.length-inputOffset) + 
292                 " bytes large, but it should be 8 bytes or larger.");
293         }
294         
295         
296         long returnValue =0L;
297         
298         for (int i =inputOffset; i-inputOffset < 8; i++){
299             returnValue |= 
300             (input[i] & 0x00000000000000ffL ) << (64-8 - 8*(i-inputOffset));
301         } 
302         
303         return returnValue;
304     
305     }    
306 }
307