Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

CryptoUtils.java

00001 // CryptoUtils - some cryptography utilities
00002 //
00003 // Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>.  All rights reserved.
00004 //
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions
00007 // are met:
00008 // 1. Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 // 2. Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //
00014 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00015 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00016 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00017 // ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00018 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00019 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00020 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00021 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00022 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00023 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00024 // SUCH DAMAGE.
00025 //
00026 // Visit the ACME Labs Java page for up-to-date versions of this and other
00027 // fine Java utilities: http://www.acme.com/java/
00028 
00029 package Acme.Crypto;
00030 
00031 import java.io.*;
00032 
00034 // <P>
00035 // These are static methods used by a lot of the cryptography classes.
00036 // Most of them operate on byte arrays, which we call blocks.
00037 // They could be encapsulated in a "Block" class, but that would
00038 // mean a big efficiency hit - method calls are a lot more
00039 // expensive than array accesses.
00040 // <P>
00041 // <A HREF="/resources/classes/Acme/Crypto/CryptoUtils.java">Fetch the software.</A><BR>
00042 // <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>
00043 
00044 public class CryptoUtils implements Serializable
00045     {
00046 
00048     public static void zeroBlock( byte[] block, int off, int len )
00049         {
00050         for ( int i = off; i < off + len; ++i )
00051             block[i] = 0;
00052         }
00053 
00055     public static void zeroBlock( byte[] block )
00056         {
00057         zeroBlock( block, 0, block.length );
00058         }
00059 
00061     public static void randomBlock( byte[] block, int off, int len )
00062         {
00063         for ( int i = off; i < off + len; ++i )
00064             block[i] = (byte) ( Math.random() * 256.0 );
00065         }
00066 
00068     public static void randomBlock( byte[] block )
00069         {
00070         randomBlock( block, 0, block.length );
00071         }
00072 
00074     public static void xorBlock( byte[] a, int aOff, byte[] b, int bOff, byte[] dst, int dstOff, int len )
00075         {
00076         for ( int i = 0; i < len; ++i )
00077             dst[dstOff + i] = (byte) ( a[aOff + i] ^ b[bOff + i] );
00078         }
00079 
00081     public static void xorBlock( byte[] a, byte[] b, byte[] dst )
00082         {
00083         xorBlock( a, 0, b, 0, dst, 0, a.length );
00084         }
00085 
00087     public static void copyBlock( byte[] src, int srcOff, byte[] dst, int dstOff, int len )
00088         {
00089         for ( int i = 0; i < len; ++i )
00090             dst[dstOff + i] = src[srcOff + i];
00091         }
00092 
00094     public static void copyBlock( byte[] src, byte[] dst )
00095         {
00096         copyBlock( src, 0, dst, 0, src.length );
00097         }
00098 
00100     public static boolean equalsBlock( byte[] a, int aOff, byte[] b, int bOff, int len )
00101         {
00102         for ( int i = 0; i < len; ++i )
00103             if ( a[aOff + i] != b[bOff + i] )
00104                 return false;
00105         return true;
00106         }
00107 
00109     public static boolean equalsBlock( byte[] a, byte[] b )
00110         {
00111         return equalsBlock( a, 0, b, 0, a.length );
00112         }
00113 
00115     public static void fillBlock( byte[] block, int blockOff, byte b, int len )
00116         {
00117         for ( int i = blockOff; i < blockOff + len; ++i )
00118             block[i] = b;
00119         }
00120 
00122     public static void fillBlock( byte[] block, byte b )
00123         {
00124         fillBlock( block, 0, b, block.length );
00125         }
00126 
00128     public static void squashBytesToInts( byte[] inBytes, int inOff, int[] outInts, int outOff, int intLen )
00129         {
00130         for ( int i = 0; i < intLen; ++i )
00131             outInts[outOff + i] = 
00132                 ( ( inBytes[inOff + i * 4    ] & 0xff ) << 24 ) |
00133                 ( ( inBytes[inOff + i * 4 + 1] & 0xff ) << 16 ) |
00134                 ( ( inBytes[inOff + i * 4 + 2] & 0xff ) <<  8 ) |
00135                 ( ( inBytes[inOff + i * 4 + 3] & 0xff )       );
00136         }
00137 
00139     public static void spreadIntsToBytes( int[] inInts, int inOff, byte[] outBytes, int outOff, int intLen )
00140         {
00141         for ( int i = 0; i < intLen; ++i )
00142             {
00143             outBytes[outOff + i * 4    ] =
00144                 (byte) ( ( inInts[inOff + i] >>> 24 ) & 0xff );
00145             outBytes[outOff + i * 4 + 1] =
00146                 (byte) ( ( inInts[inOff + i] >>> 16 ) & 0xff );
00147             outBytes[outOff + i * 4 + 2] =
00148                 (byte) ( ( inInts[inOff + i] >>>  8 ) & 0xff );
00149             outBytes[outOff + i * 4 + 3] =
00150                 (byte) ( ( inInts[inOff + i]        ) & 0xff );
00151             }
00152         }
00153 
00155     public static void squashBytesToIntsLittle( byte[] inBytes, int inOff, int[] outInts, int outOff, int intLen )
00156         {
00157         for ( int i = 0; i < intLen; ++i )
00158             outInts[outOff + i] = 
00159                 ( ( inBytes[inOff + i * 4    ] & 0xff )       ) |
00160                 ( ( inBytes[inOff + i * 4 + 1] & 0xff ) <<  8 ) |
00161                 ( ( inBytes[inOff + i * 4 + 2] & 0xff ) << 16 ) |
00162                 ( ( inBytes[inOff + i * 4 + 3] & 0xff ) << 24 );
00163         }
00164 
00166     public static void spreadIntsToBytesLittle( int[] inInts, int inOff, byte[] outBytes, int outOff, int intLen )
00167         {
00168         for ( int i = 0; i < intLen; ++i )
00169             {
00170             outBytes[outOff + i * 4    ] =
00171                 (byte) ( ( inInts[inOff + i]        ) & 0xff );
00172             outBytes[outOff + i * 4 + 1] =
00173                 (byte) ( ( inInts[inOff + i] >>>  8 ) & 0xff );
00174             outBytes[outOff + i * 4 + 2] =
00175                 (byte) ( ( inInts[inOff + i] >>> 16 ) & 0xff );
00176             outBytes[outOff + i * 4 + 3] =
00177                 (byte) ( ( inInts[inOff + i] >>> 24 ) & 0xff );
00178             }
00179         }
00180 
00182     public static void squashBytesToShorts( byte[] inBytes, int inOff, int[] outShorts, int outOff, int shortLen )
00183         {
00184         for ( int i = 0; i < shortLen; ++i )
00185             outShorts[outOff + i] =
00186                 ( ( inBytes[inOff + i * 2    ] & 0xff ) << 8 ) |
00187                 ( ( inBytes[inOff + i * 2 + 1] & 0xff )      );
00188         }
00189 
00191     public static void spreadShortsToBytes( int[] inShorts, int inOff, byte[] outBytes, int outOff, int shortLen )
00192         {
00193         for ( int i = 0; i < shortLen; ++i )
00194             {
00195             outBytes[outOff + i * 2    ] =
00196                 (byte) ( ( inShorts[inOff + i] >>> 8 ) & 0xff );
00197             outBytes[outOff + i * 2 + 1] =
00198                 (byte) ( ( inShorts[inOff + i]       ) & 0xff );
00199             }
00200         }
00201 
00203     public static void squashBytesToShortsLittle( byte[] inBytes, int inOff, int[] outShorts, int outOff, int shortLen )
00204         {
00205         for ( int i = 0; i < shortLen; ++i )
00206             outShorts[outOff + i] =
00207                 ( ( inBytes[inOff + i * 2    ] & 0xff )      ) |
00208                 ( ( inBytes[inOff + i * 2 + 1] & 0xff ) << 8 );
00209         }
00210 
00212     public static void spreadShortsToBytesLittle( int[] inShorts, int inOff, byte[] outBytes, int outOff, int shortLen )
00213         {
00214         for ( int i = 0; i < shortLen; ++i )
00215             {
00216             outBytes[outOff + i * 2    ] =
00217                 (byte) ( ( inShorts[inOff + i]       ) & 0xff );
00218             outBytes[outOff + i * 2 + 1] =
00219                 (byte) ( ( inShorts[inOff + i] >>> 8 ) & 0xff );
00220             }
00221         }
00222 
00224     public static String toStringBlock( byte[] block, int off, int len )
00225         {
00226         String hexits = "0123456789abcdef";
00227         StringBuffer buf = new StringBuffer();
00228         for ( int i = off; i < off + len; ++i )
00229             {
00230             buf.append( hexits.charAt( ( block[i] >>> 4 ) & 0xf ) );
00231             buf.append( hexits.charAt( block[i] & 0xf ) );
00232             }
00233         return "[" + buf + "]";
00234         }
00235 
00237     public static String toStringBlock( byte[] block )
00238         {
00239         return toStringBlock( block, 0, block.length );
00240         }
00241 
00242     }

Generated on Wed Dec 14 21:05:32 2005 for OpenMobileIS by  doxygen 1.4.4