1   /* $RCSfile: LongUtil.java,v $
2    * $Revision: 1.2 $
3    * $Date: 2002/01/04 14:05:20 $
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.ShortBufferException;
35  
36  /** 
37   * LongUtil Class.
38   *
39   * Class that hold only static methods to convert longs and long arrays to
40   * Strings. These Strings holds the hex or binary representation of the
41   * converted longs or long 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 LongUtil {
48      
49      /** This class can't be instance, because its constructor is private. */
50      private LongUtil() {}
51  
52      /** 
53       * Converts byte to binary representation.
54       *
55       * @param in long that will be converted.
56       * @return String with hex representation of in.
57       */    
58      public static String toBin(long in){
59          
60          return toBinGeneric(in);
61      }
62      
63      /** 
64       * Converts long[] to binary representation.
65       *
66       * @param in long[] that will be converted.
67       * @return String with hex representation of in.
68       */        
69      public static String toBin(long[] 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 "[ " + tmp.toString() + "]";
78      
79      }
80      
81      /**
82       * Converts long to binary representation.
83       *
84       * @param in long that will be converted
85       * @return String with hex representation of in
86       */        
87      private static String toBinGeneric(long in){
88  
89          StringBuffer tmp = new StringBuffer();
90          
91          for(int i = 0; i < 64; i++) {
92              tmp.insert(0, (in >>> i) & 0x0000000000000001 );
93              
94          }
95          
96          return "0b" + tmp.toString();
97      }
98      
99      /**
100      * Converts long to hex representation.
101      *
102      * @param in long that will be converted.
103      * @return String with hex representation of in.
104      */       
105     public static String toHex(long in) {
106         
107         return toHexGeneric(in);
108     }
109 
110     /**
111      * Converts long[] to hex representation.
112      *
113      * @param in long[] that will be converted.
114      * @return String with hex representation of in.
115      */      
116     public static String toHex(long[] in) {
117         
118         StringBuffer tmp = new StringBuffer();
119         
120         for (int i = 0; i < in.length; i++) {
121             tmp.append(toHexGeneric(in[i]) + " ");
122         }
123         
124         return "[ " + tmp.toString() + "]";
125     }
126     
127     /** 
128      * Converts long to binary representation.
129      *
130      * @param in long that will be converted
131      * @return String with hex representation of in
132      */        
133     private static String toHexGeneric(long in){
134         return ByteUtil.toHex(toByteArray(in));     
135     }
136 
137     /**
138      * Converts long to a new byte[8]
139      *
140      * @return byte[8] array that holds the long in as byte array.
141      * @param input that will be converted to a new byte[8] array.
142      */    
143     public static byte[] toByteArray(long input) {
144         
145         byte[] returnValue = new byte[8];
146         
147         try{
148             toByteArray(input, returnValue, 0);
149         } catch (ShortBufferException e){
150             e.printStackTrace();
151         }
152         
153         return returnValue;
154     }
155     
156     /**
157      * Converts long to a byte[]
158      *
159      * @param input that will be converted to a byte[] array.
160      * @param output the buffer for the result.
161      * @param outputOffset the offset in output where the result is stored.
162      * @throws ShortBufferException if the given output is too small to hold the result.
163      * @return the number of bytes stored in output.
164      */    
165     public static int toByteArray(long input,  byte[] output, int outputOffset)
166     throws ShortBufferException
167     {
168         if (output.length-outputOffset < 8) {
169             throw new ShortBufferException(
170                       "Usable byte range is " + (output.length-outputOffset) + 
171                       " bytes large, but it should be 8 bytes or larger.");            
172         }
173         
174 
175         for (int i = 0; i < 8; i++){
176             output[outputOffset+i] = 
177             (byte)((input >>> (64-8 - 8*i)) & 0x00000000000000ffL);
178         }    
179         
180         return 8;
181     }    
182 }
183