KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Acme > Crypto > CryptoUtils


1 // CryptoUtils - some cryptography utilities
2
//
3
// Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>. All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions
7
// are met:
8
// 1. Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// 2. Redistributions in binary form must reproduce the above copyright
11
// notice, this list of conditions and the following disclaimer in the
12
// documentation and/or other materials provided with the distribution.
13
//
14
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
// SUCH DAMAGE.
25
//
26
// Visit the ACME Labs Java page for up-to-date versions of this and other
27
// fine Java utilities: http://www.acme.com/java/
28

29 package Acme.Crypto;
30
31 import java.io.*;
32
33 /// Some cryptography utilities.
34
// <P>
35
// These are static methods used by a lot of the cryptography classes.
36
// Most of them operate on byte arrays, which we call blocks.
37
// They could be encapsulated in a "Block" class, but that would
38
// mean a big efficiency hit - method calls are a lot more
39
// expensive than array accesses.
40
// <P>
41
// <A HREF="../../../resources/classes/Acme/Crypto/CryptoUtils.java">Fetch the software.</A><BR>
42
// <A HREF="../../../resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>
43

44 public class CryptoUtils
45     {
46
47     /// Utility routine to fill a block with zeros.
48
public static void zeroBlock( byte[] block, int off, int len )
49     {
50     for ( int i = off; i < off + len; ++i )
51         block[i] = 0;
52     }
53
54     /// Utility routine to fill a block with zeros.
55
public static void zeroBlock( byte[] block )
56     {
57     zeroBlock( block, 0, block.length );
58     }
59
60     /// Utility routine to fill a block with random bytes.
61
public static void randomBlock( byte[] block, int off, int len )
62     {
63     for ( int i = off; i < off + len; ++i )
64         block[i] = (byte) ( Math.random() * 256.0 );
65     }
66
67     /// Utility routine to fill a block with random bytes.
68
public static void randomBlock( byte[] block )
69     {
70     randomBlock( block, 0, block.length );
71     }
72
73     /// Utility routine to XOR two blocks.
74
public static void xorBlock( byte[] a, int aOff, byte[] b, int bOff, byte[] dst, int dstOff, int len )
75     {
76     for ( int i = 0; i < len; ++i )
77         dst[dstOff + i] = (byte) ( a[aOff + i] ^ b[bOff + i] );
78     }
79
80     /// Utility routine to XOR two blocks.
81
public static void xorBlock( byte[] a, byte[] b, byte[] dst )
82     {
83     xorBlock( a, 0, b, 0, dst, 0, a.length );
84     }
85
86     /// Utility routine to copy one block to another.
87
public static void copyBlock( byte[] src, int srcOff, byte[] dst, int dstOff, int len )
88     {
89     for ( int i = 0; i < len; ++i )
90         dst[dstOff + i] = src[srcOff + i];
91     }
92
93     /// Utility routine to copy one block to another.
94
public static void copyBlock( byte[] src, byte[] dst )
95     {
96     copyBlock( src, 0, dst, 0, src.length );
97     }
98
99     /// Utility routine to check two blocks for equality.
100
public static boolean equalsBlock( byte[] a, int aOff, byte[] b, int bOff, int len )
101     {
102     for ( int i = 0; i < len; ++i )
103         if ( a[aOff + i] != b[bOff + i] )
104         return false;
105     return true;
106     }
107
108     /// Utility routine to check two blocks for equality.
109
public static boolean equalsBlock( byte[] a, byte[] b )
110     {
111     return equalsBlock( a, 0, b, 0, a.length );
112     }
113
114     /// Utility routine fill a block with a given byte.
115
public static void fillBlock( byte[] block, int blockOff, byte b, int len )
116     {
117     for ( int i = blockOff; i < blockOff + len; ++i )
118         block[i] = b;
119     }
120
121     /// Utility routine fill a block with a given byte.
122
public static void fillBlock( byte[] block, byte b )
123     {
124     fillBlock( block, 0, b, block.length );
125     }
126
127     /// Squash bytes down to ints.
128
public static void squashBytesToInts( byte[] inBytes, int inOff, int[] outInts, int outOff, int intLen )
129         {
130     for ( int i = 0; i < intLen; ++i )
131         outInts[outOff + i] =
132         ( ( inBytes[inOff + i * 4 ] & 0xff ) << 24 ) |
133         ( ( inBytes[inOff + i * 4 + 1] & 0xff ) << 16 ) |
134         ( ( inBytes[inOff + i * 4 + 2] & 0xff ) << 8 ) |
135         ( ( inBytes[inOff + i * 4 + 3] & 0xff ) );
136         }
137
138     /// Spread ints into bytes.
139
public static void spreadIntsToBytes( int[] inInts, int inOff, byte[] outBytes, int outOff, int intLen )
140         {
141     for ( int i = 0; i < intLen; ++i )
142         {
143         outBytes[outOff + i * 4 ] =
144         (byte) ( ( inInts[inOff + i] >>> 24 ) & 0xff );
145         outBytes[outOff + i * 4 + 1] =
146         (byte) ( ( inInts[inOff + i] >>> 16 ) & 0xff );
147         outBytes[outOff + i * 4 + 2] =
148         (byte) ( ( inInts[inOff + i] >>> 8 ) & 0xff );
149         outBytes[outOff + i * 4 + 3] =
150         (byte) ( ( inInts[inOff + i] ) & 0xff );
151         }
152         }
153
154     /// Squash bytes down to ints, little-endian.
155
public static void squashBytesToIntsLittle( byte[] inBytes, int inOff, int[] outInts, int outOff, int intLen )
156         {
157     for ( int i = 0; i < intLen; ++i )
158         outInts[outOff + i] =
159         ( ( inBytes[inOff + i * 4 ] & 0xff ) ) |
160         ( ( inBytes[inOff + i * 4 + 1] & 0xff ) << 8 ) |
161         ( ( inBytes[inOff + i * 4 + 2] & 0xff ) << 16 ) |
162         ( ( inBytes[inOff + i * 4 + 3] & 0xff ) << 24 );
163         }
164
165     /// Spread ints into bytes, little-endian.
166
public static void spreadIntsToBytesLittle( int[] inInts, int inOff, byte[] outBytes, int outOff, int intLen )
167         {
168     for ( int i = 0; i < intLen; ++i )
169         {
170         outBytes[outOff + i * 4 ] =
171         (byte) ( ( inInts[inOff + i] ) & 0xff );
172         outBytes[outOff + i * 4 + 1] =
173         (byte) ( ( inInts[inOff + i] >>> 8 ) & 0xff );
174         outBytes[outOff + i * 4 + 2] =
175         (byte) ( ( inInts[inOff + i] >>> 16 ) & 0xff );
176         outBytes[outOff + i * 4 + 3] =
177         (byte) ( ( inInts[inOff + i] >>> 24 ) & 0xff );
178         }
179         }
180
181     /// Squash bytes down to shorts.
182
public static void squashBytesToShorts( byte[] inBytes, int inOff, int[] outShorts, int outOff, int shortLen )
183     {
184     for ( int i = 0; i < shortLen; ++i )
185         outShorts[outOff + i] =
186         ( ( inBytes[inOff + i * 2 ] & 0xff ) << 8 ) |
187         ( ( inBytes[inOff + i * 2 + 1] & 0xff ) );
188     }
189
190     /// Spread shorts into bytes.
191
public static void spreadShortsToBytes( int[] inShorts, int inOff, byte[] outBytes, int outOff, int shortLen )
192     {
193     for ( int i = 0; i < shortLen; ++i )
194         {
195         outBytes[outOff + i * 2 ] =
196         (byte) ( ( inShorts[inOff + i] >>> 8 ) & 0xff );
197         outBytes[outOff + i * 2 + 1] =
198         (byte) ( ( inShorts[inOff + i] ) & 0xff );
199         }
200     }
201
202     /// Squash bytes down to shorts, little endian.
203
public static void squashBytesToShortsLittle( byte[] inBytes, int inOff, int[] outShorts, int outOff, int shortLen )
204     {
205     for ( int i = 0; i < shortLen; ++i )
206         outShorts[outOff + i] =
207         ( ( inBytes[inOff + i * 2 ] & 0xff ) ) |
208         ( ( inBytes[inOff + i * 2 + 1] & 0xff ) << 8 );
209     }
210
211     /// Spread shorts into bytes, little endian.
212
public static void spreadShortsToBytesLittle( int[] inShorts, int inOff, byte[] outBytes, int outOff, int shortLen )
213     {
214     for ( int i = 0; i < shortLen; ++i )
215         {
216         outBytes[outOff + i * 2 ] =
217         (byte) ( ( inShorts[inOff + i] ) & 0xff );
218         outBytes[outOff + i * 2 + 1] =
219         (byte) ( ( inShorts[inOff + i] >>> 8 ) & 0xff );
220         }
221     }
222
223     /// Convert a block to a String representation.
224
public static String JavaDoc toStringBlock( byte[] block, int off, int len )
225     {
226     String JavaDoc hexits = "0123456789abcdef";
227     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
228     for ( int i = off; i < off + len; ++i )
229         {
230         buf.append( hexits.charAt( ( block[i] >>> 4 ) & 0xf ) );
231         buf.append( hexits.charAt( block[i] & 0xf ) );
232         }
233     return "[" + buf + "]";
234     }
235
236     /// Convert a block to a String representation.
237
public static String JavaDoc toStringBlock( byte[] block )
238     {
239     return toStringBlock( block, 0, block.length );
240     }
241
242     }
243
Popular Tags