Imported GNU Classpath 0.92
2006-08-14 Mark Wielaard <mark@klomp.org> Imported GNU Classpath 0.92 * HACKING: Add more importing hints. Update automake version requirement. * configure.ac (gconf-peer): New enable AC argument. Add --disable-gconf-peer and --enable-default-preferences-peer to classpath configure when gconf is disabled. * scripts/makemake.tcl: Set gnu/java/util/prefs/gconf and gnu/java/awt/dnd/peer/gtk to bc. Classify gnu/java/security/Configuration.java as generated source file. * gnu/java/lang/management/VMGarbageCollectorMXBeanImpl.java, gnu/java/lang/management/VMMemoryPoolMXBeanImpl.java, gnu/java/lang/management/VMClassLoadingMXBeanImpl.java, gnu/java/lang/management/VMRuntimeMXBeanImpl.java, gnu/java/lang/management/VMMemoryManagerMXBeanImpl.java, gnu/java/lang/management/VMThreadMXBeanImpl.java, gnu/java/lang/management/VMMemoryMXBeanImpl.java, gnu/java/lang/management/VMCompilationMXBeanImpl.java: New VM stub classes. * java/lang/management/VMManagementFactory.java: Likewise. * java/net/VMURLConnection.java: Likewise. * gnu/java/nio/VMChannel.java: Likewise. * java/lang/Thread.java (getState): Add stub implementation. * java/lang/Class.java (isEnum): Likewise. * java/lang/Class.h (isEnum): Likewise. * gnu/awt/xlib/XToolkit.java (getClasspathTextLayoutPeer): Removed. * javax/naming/spi/NamingManager.java: New override for StackWalker functionality. * configure, sources.am, Makefile.in, gcj/Makefile.in, include/Makefile.in, testsuite/Makefile.in: Regenerated. From-SVN: r116139
This commit is contained in:
parent
abab460491
commit
ac1ed908de
1294 changed files with 99479 additions and 35933 deletions
|
@ -38,8 +38,10 @@ exception statement from your version. */
|
|||
|
||||
package gnu.java.security.util;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import gnu.java.security.Configuration;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Most of this implementation is from Robert Harder's public domain Base64
|
||||
|
@ -47,25 +49,7 @@ import java.io.UnsupportedEncodingException;
|
|||
*/
|
||||
public class Base64
|
||||
{
|
||||
|
||||
// Debugging methods and variables
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private static final String NAME = "Base64";
|
||||
|
||||
private static final boolean DEBUG = true;
|
||||
|
||||
private static final int debuglevel = 9;
|
||||
|
||||
private static final PrintWriter err = new PrintWriter(System.out, true);
|
||||
|
||||
private static void debug(String s)
|
||||
{
|
||||
err.println(">>> " + NAME + ": " + s);
|
||||
}
|
||||
|
||||
// Constants and variables
|
||||
// -------------------------------------------------------------------------
|
||||
private static final Logger log = Logger.getLogger(Base64.class.getName());
|
||||
|
||||
/** Maximum line length (76) of Base64 output. */
|
||||
private static final int MAX_LINE_LENGTH = 76;
|
||||
|
@ -81,79 +65,58 @@ public class Base64
|
|||
private static final byte EQUALS_SIGN_ENC = -1; // equals sign in encoding
|
||||
|
||||
/** The 64 valid Base64 values. */
|
||||
private static final byte[] ALPHABET = { (byte) 'A', (byte) 'B', (byte) 'C',
|
||||
(byte) 'D', (byte) 'E', (byte) 'F',
|
||||
(byte) 'G', (byte) 'H', (byte) 'I',
|
||||
(byte) 'J', (byte) 'K', (byte) 'L',
|
||||
(byte) 'M', (byte) 'N', (byte) 'O',
|
||||
(byte) 'P', (byte) 'Q', (byte) 'R',
|
||||
(byte) 'S', (byte) 'T', (byte) 'U',
|
||||
(byte) 'V', (byte) 'W', (byte) 'X',
|
||||
(byte) 'Y', (byte) 'Z', (byte) 'a',
|
||||
(byte) 'b', (byte) 'c', (byte) 'd',
|
||||
(byte) 'e', (byte) 'f', (byte) 'g',
|
||||
(byte) 'h', (byte) 'i', (byte) 'j',
|
||||
(byte) 'k', (byte) 'l', (byte) 'm',
|
||||
(byte) 'n', (byte) 'o', (byte) 'p',
|
||||
(byte) 'q', (byte) 'r', (byte) 's',
|
||||
(byte) 't', (byte) 'u', (byte) 'v',
|
||||
(byte) 'w', (byte) 'x', (byte) 'y',
|
||||
(byte) 'z', (byte) '0', (byte) '1',
|
||||
(byte) '2', (byte) '3', (byte) '4',
|
||||
(byte) '5', (byte) '6', (byte) '7',
|
||||
(byte) '8', (byte) '9', (byte) '+',
|
||||
(byte) '/' };
|
||||
private static final byte[] ALPHABET = {
|
||||
(byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F',
|
||||
(byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L',
|
||||
(byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R',
|
||||
(byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X',
|
||||
(byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
|
||||
(byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j',
|
||||
(byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p',
|
||||
(byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u', (byte) 'v',
|
||||
(byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z', (byte) '0', (byte) '1',
|
||||
(byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
|
||||
(byte) '8', (byte) '9', (byte) '+', (byte) '/'
|
||||
};
|
||||
|
||||
/**
|
||||
* Translates a Base64 value to either its 6-bit reconstruction value or a
|
||||
* negative number indicating some other meaning.
|
||||
*/
|
||||
private static final byte[] DECODABET = { -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 0 - 8
|
||||
-5, -5, // Whitespace: Tab and Linefeed
|
||||
-9, -9, // Decimal 11 - 12
|
||||
-5, // Whitespace: Carriage Return
|
||||
-9, -9, -9, -9, -9, -9, -9, -9, -9,
|
||||
-9, -9, -9, -9, // Decimal 14 - 26
|
||||
-9, -9, -9, -9, -9, // Decimal 27 - 31
|
||||
-5, // Whitespace: Space
|
||||
-9, -9, -9, -9, -9, -9, -9, -9, -9,
|
||||
-9, // Decimal 33 - 42
|
||||
62, // Plus sign at decimal 43
|
||||
-9, -9, -9, // Decimal 44 - 46
|
||||
63, // Slash at decimal 47
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60,
|
||||
61, // Numbers zero through nine
|
||||
-9, -9, -9, // Decimal 58 - 60
|
||||
-1, // Equals sign at decimal 61
|
||||
-9, -9, -9, // Decimal 62 - 64
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
|
||||
11, 12, 13, // Letters 'A' through 'N'
|
||||
14, 15, 16, 17, 18, 19, 20, 21, 22,
|
||||
23, 24, 25, // Letters 'O' through 'Z'
|
||||
-9, -9, -9, -9, -9, -9, // Decimal 91 - 96
|
||||
26, 27, 28, 29, 30, 31, 32, 33, 34,
|
||||
35, 36, 37, 38, // Letters 'a' through 'm'
|
||||
39, 40, 41, 42, 43, 44, 45, 46, 47,
|
||||
48, 49, 50, 51, // Letters 'n' through 'z'
|
||||
-9, -9, -9, -9 // Decimal 123 - 126
|
||||
private static final byte[] DECODABET = {
|
||||
-9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 0 - 8
|
||||
-5, -5, // Whitespace: Tab and Linefeed
|
||||
-9, -9, // Decimal 11 - 12
|
||||
-5, // Whitespace: Carriage Return
|
||||
-9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 14 - 26
|
||||
-9, -9, -9, -9, -9, // Decimal 27 - 31
|
||||
-5, // Whitespace: Space
|
||||
-9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 33 - 42
|
||||
62, // Plus sign at decimal 43
|
||||
-9, -9, -9, // Decimal 44 - 46
|
||||
63, // Slash at decimal 47
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // Numbers zero through nine
|
||||
-9, -9, -9, // Decimal 58 - 60
|
||||
-1, // Equals sign at decimal 61
|
||||
-9, -9, -9, // Decimal 62 - 64
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // Letters 'A' through 'N'
|
||||
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // Letters 'O' through 'Z'
|
||||
-9, -9, -9, -9, -9, -9, // Decimal 91 - 96
|
||||
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // Letters 'a' through 'm'
|
||||
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // Letters 'n' through 'z'
|
||||
-9, -9, -9, -9 // Decimal 123 - 126
|
||||
};
|
||||
|
||||
// Constructor(s)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/** Trivial private ctor to enfore Singleton pattern. */
|
||||
private Base64()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
// Class methods
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Encodes a byte array into Base64 notation. Equivalent to calling
|
||||
* <code>encode(source, 0, source.length)</code>.
|
||||
*
|
||||
*
|
||||
* @param src the data to convert.
|
||||
*/
|
||||
public static final String encode(final byte[] src)
|
||||
|
@ -163,7 +126,7 @@ public class Base64
|
|||
|
||||
/**
|
||||
* Encodes a byte array into Base64 notation.
|
||||
*
|
||||
*
|
||||
* @param src the data to convert.
|
||||
* @param off offset in array where conversion should begin.
|
||||
* @param len length of data to convert.
|
||||
|
@ -176,7 +139,7 @@ public class Base64
|
|||
final byte[] outBuff = new byte[len43 // Main 4:3
|
||||
+ ((len % 3) > 0 ? 4 : 0) // Account for padding
|
||||
+ (breakLines ? (len43 / MAX_LINE_LENGTH)
|
||||
: 0)]; // New lines
|
||||
: 0)]; // New lines
|
||||
int d = 0;
|
||||
int e = 0;
|
||||
final int len2 = len - 2;
|
||||
|
@ -192,13 +155,11 @@ public class Base64
|
|||
lineLength = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (d < len)
|
||||
{ // padding needed
|
||||
if (d < len) // padding needed
|
||||
{
|
||||
encode3to4(src, d + off, len - d, outBuff, e);
|
||||
e += 4;
|
||||
}
|
||||
|
||||
return new String(outBuff, 0, e);
|
||||
}
|
||||
|
||||
|
@ -255,31 +216,26 @@ public class Base64
|
|||
} // end if: quartet built
|
||||
} // end if: equals sign or better
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("Illegal BASE-64 character at #"
|
||||
+ i + ": " + src[i]
|
||||
+ "(decimal)");
|
||||
}
|
||||
throw new IllegalArgumentException("Illegal BASE-64 character at #"
|
||||
+ i + ": " + src[i] + "(decimal)");
|
||||
}
|
||||
|
||||
final byte[] result = new byte[outBuffPosn];
|
||||
System.arraycopy(outBuff, 0, result, 0, outBuffPosn);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Encodes up to three bytes of the array <code>src</code> and writes
|
||||
* the resulting four Base64 bytes to <code>dest</code>. The source and
|
||||
* Encodes up to three bytes of the array <code>src</code> and writes the
|
||||
* resulting four Base64 bytes to <code>dest</code>. The source and
|
||||
* destination arrays can be manipulated anywhere along their length by
|
||||
* specifying <code>sOffset</code> and <code>dOffset</code>.</p>
|
||||
*
|
||||
* <p>This method does not check to make sure the arrays are large enough to
|
||||
* specifying <code>sOffset</code> and <code>dOffset</code>.
|
||||
* <p>
|
||||
* This method does not check to make sure the arrays are large enough to
|
||||
* accomodate <code>sOffset + 3</code> for the <code>src</code> array or
|
||||
* <code>dOffset + 4</code> for the <code>dest</code> array. The actual
|
||||
* number of significant bytes in the input array is given by
|
||||
* <code>numBytes</code>.</p>
|
||||
*
|
||||
* <code>numBytes</code>.
|
||||
*
|
||||
* @param src the array to convert.
|
||||
* @param sOffset the index where conversion begins.
|
||||
* @param numBytes the number of significant bytes in your array.
|
||||
|
@ -302,25 +258,25 @@ public class Base64
|
|||
// significant bytes passed in the array.
|
||||
// We have to shift left 24 in order to flush out the 1's that appear
|
||||
// when Java treats a value as negative that is cast from a byte to an int.
|
||||
final int inBuff = (numBytes > 0 ? ((src[sOffset] << 24) >>> 8) : 0)
|
||||
final int inBuff = (numBytes > 0 ? ((src[sOffset] << 24) >>> 8) : 0)
|
||||
| (numBytes > 1 ? ((src[sOffset + 1] << 24) >>> 16) : 0)
|
||||
| (numBytes > 2 ? ((src[sOffset + 2] << 24) >>> 24) : 0);
|
||||
switch (numBytes)
|
||||
{
|
||||
case 3:
|
||||
dest[dOffset] = ALPHABET[(inBuff >>> 18)];
|
||||
dest[dOffset ] = ALPHABET[(inBuff >>> 18)];
|
||||
dest[dOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3F];
|
||||
dest[dOffset + 2] = ALPHABET[(inBuff >>> 6) & 0x3F];
|
||||
dest[dOffset + 3] = ALPHABET[(inBuff) & 0x3F];
|
||||
dest[dOffset + 2] = ALPHABET[(inBuff >>> 6) & 0x3F];
|
||||
dest[dOffset + 3] = ALPHABET[(inBuff) & 0x3F];
|
||||
break;
|
||||
case 2:
|
||||
dest[dOffset] = ALPHABET[(inBuff >>> 18)];
|
||||
dest[dOffset ] = ALPHABET[(inBuff >>> 18)];
|
||||
dest[dOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3F];
|
||||
dest[dOffset + 2] = ALPHABET[(inBuff >>> 6) & 0x3F];
|
||||
dest[dOffset + 2] = ALPHABET[(inBuff >>> 6) & 0x3F];
|
||||
dest[dOffset + 3] = EQUALS_SIGN;
|
||||
break;
|
||||
case 1:
|
||||
dest[dOffset] = ALPHABET[(inBuff >>> 18)];
|
||||
dest[dOffset ] = ALPHABET[(inBuff >>> 18)];
|
||||
dest[dOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3F];
|
||||
dest[dOffset + 2] = EQUALS_SIGN;
|
||||
dest[dOffset + 3] = EQUALS_SIGN;
|
||||
|
@ -330,19 +286,18 @@ public class Base64
|
|||
}
|
||||
|
||||
/**
|
||||
* <p>Decodes four bytes from array <code>src</code> and writes the
|
||||
* resulting bytes (up to three of them) to <code>dest</code>.</p>
|
||||
*
|
||||
* <p>The source and destination arrays can be manipulated anywhere along
|
||||
* their length by specifying <code>sOffset</code> and <code>dOffset</code>.
|
||||
* </p>
|
||||
*
|
||||
* <p>This method does not check to make sure your arrays are large enough
|
||||
* to accomodate <code>sOffset + 4</code> for the <code>src</code> array or
|
||||
* Decodes four bytes from array <code>src</code> and writes the resulting
|
||||
* bytes (up to three of them) to <code>dest</code>.
|
||||
* <p>
|
||||
* The source and destination arrays can be manipulated anywhere along their
|
||||
* length by specifying <code>sOffset</code> and <code>dOffset</code>.
|
||||
* <p>
|
||||
* This method does not check to make sure your arrays are large enough to
|
||||
* accomodate <code>sOffset + 4</code> for the <code>src</code> array or
|
||||
* <code>dOffset + 3</code> for the <code>dest</code> array. This method
|
||||
* returns the actual number of bytes that were converted from the Base64
|
||||
* encoding.</p>
|
||||
*
|
||||
* encoding.
|
||||
*
|
||||
* @param src the array to convert.
|
||||
* @param sOffset the index where conversion begins.
|
||||
* @param dest the array to hold the conversion.
|
||||
|
@ -352,43 +307,41 @@ public class Base64
|
|||
private static final int decode4to3(final byte[] src, final int sOffset,
|
||||
final byte[] dest, final int dOffset)
|
||||
{
|
||||
if (src[sOffset + 2] == EQUALS_SIGN)
|
||||
{ // Example: Dk==
|
||||
final int outBuff = ((DECODABET[src[sOffset]] & 0xFF) << 18)
|
||||
| ((DECODABET[src[sOffset + 1]] & 0xFF) << 12);
|
||||
dest[dOffset] = (byte) (outBuff >>> 16);
|
||||
if (src[sOffset + 2] == EQUALS_SIGN) // Example: Dk==
|
||||
{
|
||||
final int outBuff = ((DECODABET[src[sOffset ]] & 0xFF) << 18)
|
||||
| ((DECODABET[src[sOffset + 1]] & 0xFF) << 12);
|
||||
dest[dOffset] = (byte)(outBuff >>> 16);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (src[sOffset + 3] == EQUALS_SIGN)
|
||||
{ // Example: DkL=
|
||||
final int outBuff = ((DECODABET[src[sOffset]] & 0xFF) << 18)
|
||||
| ((DECODABET[src[sOffset + 1]] & 0xFF) << 12)
|
||||
| ((DECODABET[src[sOffset + 2]] & 0xFF) << 6);
|
||||
dest[dOffset] = (byte) (outBuff >>> 16);
|
||||
dest[dOffset + 1] = (byte) (outBuff >>> 8);
|
||||
if (src[sOffset + 3] == EQUALS_SIGN) // Example: DkL=
|
||||
{
|
||||
final int outBuff = ((DECODABET[src[sOffset ]] & 0xFF) << 18)
|
||||
| ((DECODABET[src[sOffset + 1]] & 0xFF) << 12)
|
||||
| ((DECODABET[src[sOffset + 2]] & 0xFF) << 6);
|
||||
dest[dOffset ] = (byte)(outBuff >>> 16);
|
||||
dest[dOffset + 1] = (byte)(outBuff >>> 8);
|
||||
return 2;
|
||||
}
|
||||
|
||||
try
|
||||
{ // Example: DkLE
|
||||
final int outBuff = ((DECODABET[src[sOffset]] & 0xFF) << 18)
|
||||
| ((DECODABET[src[sOffset + 1]] & 0xFF) << 12)
|
||||
| ((DECODABET[src[sOffset + 2]] & 0xFF) << 6)
|
||||
| ((DECODABET[src[sOffset + 3]] & 0xFF));
|
||||
dest[dOffset] = (byte) (outBuff >> 16);
|
||||
dest[dOffset + 1] = (byte) (outBuff >> 8);
|
||||
try // Example: DkLE
|
||||
{
|
||||
final int outBuff = ((DECODABET[src[sOffset ]] & 0xFF) << 18)
|
||||
| ((DECODABET[src[sOffset + 1]] & 0xFF) << 12)
|
||||
| ((DECODABET[src[sOffset + 2]] & 0xFF) << 6)
|
||||
| ((DECODABET[src[sOffset + 3]] & 0xFF));
|
||||
dest[dOffset ] = (byte)(outBuff >> 16);
|
||||
dest[dOffset + 1] = (byte)(outBuff >> 8);
|
||||
dest[dOffset + 2] = (byte) outBuff;
|
||||
return 3;
|
||||
}
|
||||
catch (Exception x)
|
||||
{
|
||||
if (DEBUG && debuglevel > 8)
|
||||
if (Configuration.DEBUG)
|
||||
{
|
||||
debug("" + src[sOffset] + ": " + (DECODABET[src[sOffset]]));
|
||||
debug("" + src[sOffset + 1] + ": " + (DECODABET[src[sOffset + 1]]));
|
||||
debug("" + src[sOffset + 2] + ": " + (DECODABET[src[sOffset + 2]]));
|
||||
debug("" + src[sOffset + 3] + ": " + (DECODABET[src[sOffset + 3]]));
|
||||
log.fine("" + src[sOffset ] + ": " + (DECODABET[src[sOffset ]]));
|
||||
log.fine("" + src[sOffset + 1] + ": " + (DECODABET[src[sOffset + 1]]));
|
||||
log.fine("" + src[sOffset + 2] + ": " + (DECODABET[src[sOffset + 2]]));
|
||||
log.fine("" + src[sOffset + 3] + ": " + (DECODABET[src[sOffset + 3]]));
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
|
109
libjava/classpath/gnu/java/security/util/ByteArray.java
Normal file
109
libjava/classpath/gnu/java/security/util/ByteArray.java
Normal file
|
@ -0,0 +1,109 @@
|
|||
/* ByteArray.java -- wrapper around a byte array, with nice toString output.
|
||||
Copyright (C) 2005 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of GNU Classpath.
|
||||
|
||||
GNU Classpath is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2, or (at your option)
|
||||
any later version.
|
||||
|
||||
GNU Classpath is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GNU Classpath; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
02110-1301 USA.
|
||||
|
||||
Linking this library statically or dynamically with other modules is
|
||||
making a combined work based on this library. Thus, the terms and
|
||||
conditions of the GNU General Public License cover the whole
|
||||
combination.
|
||||
|
||||
As a special exception, the copyright holders of this library give you
|
||||
permission to link this library with independent modules to produce an
|
||||
executable, regardless of the license terms of these independent
|
||||
modules, and to copy and distribute the resulting executable under
|
||||
terms of your choice, provided that you also meet, for each linked
|
||||
independent module, the terms and conditions of the license of that
|
||||
module. An independent module is a module which is not derived from
|
||||
or based on this library. If you modify this library, you may extend
|
||||
this exception to your version of the library, but you are not
|
||||
obligated to do so. If you do not wish to do so, delete this
|
||||
exception statement from your version. */
|
||||
|
||||
|
||||
package gnu.java.security.util;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
|
||||
public final class ByteArray
|
||||
{
|
||||
private final byte[] value;
|
||||
|
||||
public ByteArray (final byte[] value)
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public byte[] getValue ()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
public String toString ()
|
||||
{
|
||||
StringWriter str = new StringWriter ();
|
||||
PrintWriter out = new PrintWriter (str);
|
||||
int i = 0;
|
||||
int len = value.length;
|
||||
while (i < len)
|
||||
{
|
||||
out.print (formatInt (i, 16, 8));
|
||||
out.print (" ");
|
||||
int l = Math.min (16, len - i);
|
||||
String s = toHexString (value, i, l, ' ');
|
||||
out.print (s);
|
||||
for (int j = 56 - (56 - s.length ()); j < 56; j++)
|
||||
out.print (" ");
|
||||
for (int j = 0; j < l; j++)
|
||||
{
|
||||
byte b = value[i+j];
|
||||
if ((b & 0xFF) < 0x20 || (b & 0xFF) > 0x7E)
|
||||
out.print (".");
|
||||
else
|
||||
out.print ((char) (b & 0xFF));
|
||||
}
|
||||
out.println ();
|
||||
i += 16;
|
||||
}
|
||||
return str.toString ();
|
||||
}
|
||||
|
||||
public static String toHexString (byte[] buf, int off, int len, char sep)
|
||||
{
|
||||
StringBuffer str = new StringBuffer();
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
str.append (Character.forDigit (buf[i+off] >>> 4 & 0x0F, 16));
|
||||
str.append (Character.forDigit (buf[i+off] & 0x0F, 16));
|
||||
if (i < len - 1)
|
||||
str.append(sep);
|
||||
}
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
public static String formatInt (int value, int radix, int len)
|
||||
{
|
||||
String s = Integer.toString (value, radix);
|
||||
StringBuffer buf = new StringBuffer ();
|
||||
for (int j = 0; j < len - s.length(); j++)
|
||||
buf.append ("0");
|
||||
buf.append (s);
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
|
@ -46,26 +46,23 @@ import javax.security.auth.Destroyable;
|
|||
|
||||
/**
|
||||
* The base class for objects with sensitive data that are automatically
|
||||
* destroyed after a timeout elapses. On creation, an object that extends
|
||||
* this class will automatically be added to a {@link Timer} object that,
|
||||
* once a timeout elapses, will automatically call the {@link
|
||||
* Destroyable#destroy()} method.
|
||||
*
|
||||
* <p>Concrete subclasses must implement the {@link doDestroy()} method
|
||||
* instead of {@link Destroyable#destroy()}; the behavior of that method
|
||||
* should match exactly the behavior desired of <code>destroy()</code>.
|
||||
*
|
||||
* <p>Note that if a {@link DestroyFailedException} occurs when the timeout
|
||||
* destroyed after a timeout elapses. On creation, an object that extends this
|
||||
* class will automatically be added to a {@link Timer} object that, once a
|
||||
* timeout elapses, will automatically call the {@link Destroyable#destroy()}
|
||||
* method.
|
||||
* <p>
|
||||
* Concrete subclasses must implement the {@link #doDestroy()} method instead of
|
||||
* {@link Destroyable#destroy()}; the behavior of that method should match
|
||||
* exactly the behavior desired of <code>destroy()</code>.
|
||||
* <p>
|
||||
* Note that if a {@link DestroyFailedException} occurs when the timeout
|
||||
* expires, it will not be reported.
|
||||
*
|
||||
*
|
||||
* @see Destroyable
|
||||
*/
|
||||
public abstract class ExpirableObject implements Destroyable
|
||||
public abstract class ExpirableObject
|
||||
implements Destroyable
|
||||
{
|
||||
|
||||
// Constants and fields.
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The default timeout, used in the default constructor.
|
||||
*/
|
||||
|
@ -82,9 +79,6 @@ public abstract class ExpirableObject implements Destroyable
|
|||
*/
|
||||
private final Destroyer destroyer;
|
||||
|
||||
// Constructors.
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Create a new expirable object that will expire after one hour.
|
||||
*/
|
||||
|
@ -94,12 +88,11 @@ public abstract class ExpirableObject implements Destroyable
|
|||
}
|
||||
|
||||
/**
|
||||
* Create a new expirable object that will expire after the specified
|
||||
* timeout.
|
||||
*
|
||||
* Create a new expirable object that will expire after the specified timeout.
|
||||
*
|
||||
* @param delay The delay before expiration.
|
||||
* @throws IllegalArgumentException If <i>delay</i> is negative, or if
|
||||
* <code>delay + System.currentTimeMillis()</code> is negative.
|
||||
* <code>delay + System.currentTimeMillis()</code> is negative.
|
||||
*/
|
||||
protected ExpirableObject(final long delay)
|
||||
{
|
||||
|
@ -107,14 +100,11 @@ public abstract class ExpirableObject implements Destroyable
|
|||
EXPIRER.schedule(destroyer, delay);
|
||||
}
|
||||
|
||||
// Instance methods.
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Destroys this object. This method calls {@link doDestroy}, then, if
|
||||
* no exception is thrown, cancels the task that would destroy this object
|
||||
* when the timeout is reached.
|
||||
*
|
||||
* Destroys this object. This method calls {@link #doDestroy}, then, if no
|
||||
* exception is thrown, cancels the task that would destroy this object when
|
||||
* the timeout is reached.
|
||||
*
|
||||
* @throws DestroyFailedException If this operation fails.
|
||||
*/
|
||||
public final void destroy() throws DestroyFailedException
|
||||
|
@ -126,42 +116,30 @@ public abstract class ExpirableObject implements Destroyable
|
|||
/**
|
||||
* Subclasses must implement this method instead of the {@link
|
||||
* Destroyable#destroy()} method.
|
||||
*
|
||||
*
|
||||
* @throws DestroyFailedException If this operation fails.
|
||||
*/
|
||||
protected abstract void doDestroy() throws DestroyFailedException;
|
||||
|
||||
// Inner classes.
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The task that destroys the target when the timeout elapses.
|
||||
*/
|
||||
private final class Destroyer extends TimerTask
|
||||
private final class Destroyer
|
||||
extends TimerTask
|
||||
{
|
||||
|
||||
// Fields.
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
private final ExpirableObject target;
|
||||
|
||||
// Constructor.
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
Destroyer(final ExpirableObject target)
|
||||
{
|
||||
super();
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
// Instance methods.
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!target.isDestroyed())
|
||||
if (! target.isDestroyed())
|
||||
target.doDestroy();
|
||||
}
|
||||
catch (DestroyFailedException dfe)
|
||||
|
|
109
libjava/classpath/gnu/java/security/util/IntegerUtil.java
Normal file
109
libjava/classpath/gnu/java/security/util/IntegerUtil.java
Normal file
|
@ -0,0 +1,109 @@
|
|||
/* IntegerUtil.java -- JDK 5 Integer methods with 1.4 API
|
||||
Copyright (C) 2006 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of GNU Classpath.
|
||||
|
||||
GNU Classpath is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2, or (at your option)
|
||||
any later version.
|
||||
|
||||
GNU Classpath is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GNU Classpath; see the file COPYING. If not, write to the
|
||||
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
02110-1301 USA.
|
||||
|
||||
Linking this library statically or dynamically with other modules is
|
||||
making a combined work based on this library. Thus, the terms and
|
||||
conditions of the GNU General Public License cover the whole
|
||||
combination.
|
||||
|
||||
As a special exception, the copyright holders of this library give you
|
||||
permission to link this library with independent modules to produce an
|
||||
executable, regardless of the license terms of these independent
|
||||
modules, and to copy and distribute the resulting executable under
|
||||
terms of your choice, provided that you also meet, for each linked
|
||||
independent module, the terms and conditions of the license of that
|
||||
module. An independent module is a module which is not derived from
|
||||
or based on this library. If you modify this library, you may extend
|
||||
this exception to your version of the library, but you are not
|
||||
obligated to do so. If you do not wish to do so, delete this
|
||||
exception statement from your version. */
|
||||
|
||||
|
||||
package gnu.java.security.util;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Utility class which offers Integer related methods found in RI's version 5
|
||||
* but written with RI's 1.4 API.
|
||||
*/
|
||||
public abstract class IntegerUtil
|
||||
{
|
||||
/** Maximum size of our cache of constructed Integers. */
|
||||
private static final int CACHE_SIZE = 100;
|
||||
/** LRU (Least Recently Used) cache, of the last accessed 100 Integers. */
|
||||
private static final Map cache = new LinkedHashMap(CACHE_SIZE + 1, 0.75F, true)
|
||||
{
|
||||
public boolean removeEldestEntry(Map.Entry entry)
|
||||
{
|
||||
return size() > CACHE_SIZE;
|
||||
}
|
||||
};
|
||||
|
||||
/** Trivial private constructor to enforce Singleton usage. */
|
||||
private IntegerUtil()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Similar to {@link Integer#valueOf(String)} except it caches the result in
|
||||
* a local LRU cache of 100 elements, organized by access order.
|
||||
* <p>
|
||||
* This method MUST be used in the gnu.java.security and gnu.javax.crypto
|
||||
* packages to ensure they would work with a version 1.4 only of the Java
|
||||
* class library API.
|
||||
*
|
||||
* @param aString a string representation of an integer.
|
||||
* @return the {@link Integer} object representing the designated string.
|
||||
*/
|
||||
public static final Integer valueOf(String aString)
|
||||
{
|
||||
Integer result;
|
||||
synchronized (cache)
|
||||
{
|
||||
result = (Integer) cache.get(aString);
|
||||
if (result == null)
|
||||
{
|
||||
result = Integer.valueOf(aString);
|
||||
cache.put(aString, result);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulates the <code>valueOf(int)</code> method found in {@link Integer} of
|
||||
* the RI's version 1.5 using a local LRU cache of 100 elements, organized by
|
||||
* access order.
|
||||
* <p>
|
||||
* This method MUST be used in the gnu.java.security and gnu.javax.crypto
|
||||
* packages to ensure they would work with a version 1.4 only of the Java
|
||||
* class library API.
|
||||
*
|
||||
* @param anInt a decimal integer.
|
||||
* @return the {@link Integer} object representing the designated primitive.
|
||||
*/
|
||||
public static final Integer valueOf(int anInt)
|
||||
{
|
||||
return valueOf(Integer.toString(anInt, 10));
|
||||
}
|
||||
}
|
|
@ -45,27 +45,20 @@ import gnu.java.security.prng.LimitReachedException;
|
|||
import gnu.java.security.prng.MDGenerator;
|
||||
|
||||
/**
|
||||
* A useful hash-based (SHA) pseudo-random number generator used
|
||||
* throughout this library.
|
||||
* A useful hash-based (SHA) pseudo-random number generator used throughout this
|
||||
* library.
|
||||
*
|
||||
* @see MDGenerator
|
||||
*/
|
||||
public class PRNG
|
||||
{
|
||||
// Constans and fields
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/** The underlying {@link IRandom}. */
|
||||
private IRandom delegate;
|
||||
|
||||
// Constructor(s)
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Private constructor to enforce using the Factory method.
|
||||
*
|
||||
* @param delegate
|
||||
* the undelying {@link IRandom} object used.
|
||||
* @param delegate the undelying {@link IRandom} object used.
|
||||
*/
|
||||
private PRNG(IRandom delegate)
|
||||
{
|
||||
|
@ -74,9 +67,6 @@ public class PRNG
|
|||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
// Class methods
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
public static final PRNG getInstance()
|
||||
{
|
||||
IRandom delegate = new MDGenerator();
|
||||
|
@ -86,10 +76,10 @@ public class PRNG
|
|||
// initialise it with a seed
|
||||
long t = System.currentTimeMillis();
|
||||
byte[] seed = new byte[] {
|
||||
(byte) (t >>> 56), (byte) (t >>> 48),
|
||||
(byte) (t >>> 40), (byte) (t >>> 32),
|
||||
(byte) (t >>> 24), (byte) (t >>> 16),
|
||||
(byte) (t >>> 8), (byte) t};
|
||||
(byte)(t >>> 56), (byte)(t >>> 48),
|
||||
(byte)(t >>> 40), (byte)(t >>> 32),
|
||||
(byte)(t >>> 24), (byte)(t >>> 16),
|
||||
(byte)(t >>> 8), (byte) t };
|
||||
map.put(MDGenerator.SEEED, seed);
|
||||
delegate.init(map); // default is to use SHA-1 hash
|
||||
}
|
||||
|
@ -97,19 +87,14 @@ public class PRNG
|
|||
{
|
||||
throw new ExceptionInInitializerError(x);
|
||||
}
|
||||
|
||||
return new PRNG(delegate);
|
||||
}
|
||||
|
||||
// Instance methods
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Completely fills the designated <code>buffer</code> with random data
|
||||
* generated by the underlying delegate.
|
||||
*
|
||||
* @param buffer
|
||||
* the place holder of random bytes generated by the underlying
|
||||
* @param buffer the place holder of random bytes generated by the underlying
|
||||
* delegate. On output, the contents of <code>buffer</code> are
|
||||
* replaced with pseudo-random data, iff the <code>buffer</code>
|
||||
* size is not zero.
|
||||
|
|
|
@ -1,417 +0,0 @@
|
|||
/* Prime2.java --
|
||||
Copyright (C) 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
|
||||
|
||||
This file is a part of GNU Classpath.
|
||||
|
||||
GNU Classpath is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or (at
|
||||
your option) any later version.
|
||||
|
||||
GNU Classpath is distributed in the hope that it will be useful, but
|
||||
WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with GNU Classpath; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
|
||||
USA
|
||||
|
||||
Linking this library statically or dynamically with other modules is
|
||||
making a combined work based on this library. Thus, the terms and
|
||||
conditions of the GNU General Public License cover the whole
|
||||
combination.
|
||||
|
||||
As a special exception, the copyright holders of this library give you
|
||||
permission to link this library with independent modules to produce an
|
||||
executable, regardless of the license terms of these independent
|
||||
modules, and to copy and distribute the resulting executable under
|
||||
terms of your choice, provided that you also meet, for each linked
|
||||
independent module, the terms and conditions of the license of that
|
||||
module. An independent module is a module which is not derived from
|
||||
or based on this library. If you modify this library, you may extend
|
||||
this exception to your version of the library, but you are not
|
||||
obligated to do so. If you do not wish to do so, delete this
|
||||
exception statement from your version. */
|
||||
|
||||
|
||||
package gnu.java.security.util;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
/**
|
||||
* <p>A collection of prime number related utilities used in this library.</p>
|
||||
*/
|
||||
public class Prime2
|
||||
{
|
||||
|
||||
// Debugging methods and variables
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private static final String NAME = "prime";
|
||||
|
||||
private static final boolean DEBUG = false;
|
||||
|
||||
private static final int debuglevel = 5;
|
||||
|
||||
private static final PrintWriter err = new PrintWriter(System.out, true);
|
||||
|
||||
private static void debug(String s)
|
||||
{
|
||||
err.println(">>> " + NAME + ": " + s);
|
||||
}
|
||||
|
||||
// Constants and variables
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private static final int DEFAULT_CERTAINTY = 20; // XXX is this a good value?
|
||||
|
||||
private static final BigInteger ZERO = BigInteger.ZERO;
|
||||
|
||||
private static final BigInteger ONE = BigInteger.ONE;
|
||||
|
||||
private static final BigInteger TWO = BigInteger.valueOf(2L);
|
||||
|
||||
/**
|
||||
* The first SMALL_PRIME primes: Algorithm P, section 1.3.2, The Art of
|
||||
* Computer Programming, Donald E. Knuth.
|
||||
*/
|
||||
private static final int SMALL_PRIME_COUNT = 1000;
|
||||
|
||||
private static final BigInteger[] SMALL_PRIME = new BigInteger[SMALL_PRIME_COUNT];
|
||||
static
|
||||
{
|
||||
long time = -System.currentTimeMillis();
|
||||
SMALL_PRIME[0] = TWO;
|
||||
int N = 3;
|
||||
int J = 0;
|
||||
int prime;
|
||||
P2: while (true)
|
||||
{
|
||||
SMALL_PRIME[++J] = BigInteger.valueOf(N);
|
||||
if (J >= 999)
|
||||
{
|
||||
break P2;
|
||||
}
|
||||
P4: while (true)
|
||||
{
|
||||
N += 2;
|
||||
P6: for (int K = 1; true; K++)
|
||||
{
|
||||
prime = SMALL_PRIME[K].intValue();
|
||||
if ((N % prime) == 0)
|
||||
{
|
||||
continue P4;
|
||||
}
|
||||
else if ((N / prime) <= prime)
|
||||
{
|
||||
continue P2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
time += System.currentTimeMillis();
|
||||
if (DEBUG && debuglevel > 8)
|
||||
{
|
||||
StringBuffer sb;
|
||||
for (int i = 0; i < (SMALL_PRIME_COUNT / 10); i++)
|
||||
{
|
||||
sb = new StringBuffer();
|
||||
for (int j = 0; j < 10; j++)
|
||||
{
|
||||
sb.append(String.valueOf(SMALL_PRIME[i * 10 + j])).append(" ");
|
||||
}
|
||||
debug(sb.toString());
|
||||
}
|
||||
}
|
||||
if (DEBUG && debuglevel > 4)
|
||||
{
|
||||
debug("Generating first " + String.valueOf(SMALL_PRIME_COUNT)
|
||||
+ " primes took: " + String.valueOf(time) + " ms.");
|
||||
}
|
||||
}
|
||||
|
||||
private static final Map knownPrimes = new WeakHashMap();
|
||||
|
||||
// Constructor(s)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/** Trivial constructor to enforce Singleton pattern. */
|
||||
private Prime2()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
// Class methods
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* <p>Trial division for the first 1000 small primes.</p>
|
||||
*
|
||||
* <p>Returns <code>true</code> if at least one small prime, among the first
|
||||
* 1000 ones, was found to divide the designated number. Retuens <code>false</code>
|
||||
* otherwise.</p>
|
||||
*
|
||||
* @param w the number to test.
|
||||
* @return <code>true</code> if at least one small prime was found to divide
|
||||
* the designated number.
|
||||
*/
|
||||
public static boolean hasSmallPrimeDivisor(BigInteger w)
|
||||
{
|
||||
BigInteger prime;
|
||||
for (int i = 0; i < SMALL_PRIME_COUNT; i++)
|
||||
{
|
||||
prime = SMALL_PRIME[i];
|
||||
if (w.mod(prime).equals(ZERO))
|
||||
{
|
||||
if (DEBUG && debuglevel > 4)
|
||||
{
|
||||
debug(prime.toString(16) + " | " + w.toString(16) + "...");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (DEBUG && debuglevel > 4)
|
||||
{
|
||||
debug(w.toString(16) + " has no small prime divisors...");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Java port of Colin Plumb primality test (Euler Criterion)
|
||||
* implementation for a base of 2 --from bnlib-1.1 release, function
|
||||
* primeTest() in prime.c. this is his comments.</p>
|
||||
*
|
||||
* <p>"Now, check that bn is prime. If it passes to the base 2, it's prime
|
||||
* beyond all reasonable doubt, and everything else is just gravy, but it
|
||||
* gives people warm fuzzies to do it.</p>
|
||||
*
|
||||
* <p>This starts with verifying Euler's criterion for a base of 2. This is
|
||||
* the fastest pseudoprimality test that I know of, saving a modular squaring
|
||||
* over a Fermat test, as well as being stronger. 7/8 of the time, it's as
|
||||
* strong as a strong pseudoprimality test, too. (The exception being when
|
||||
* <code>bn == 1 mod 8</code> and <code>2</code> is a quartic residue, i.e.
|
||||
* <code>bn</code> is of the form <code>a^2 + (8*b)^2</code>.) The precise
|
||||
* series of tricks used here is not documented anywhere, so here's an
|
||||
* explanation. Euler's criterion states that if <code>p</code> is prime
|
||||
* then <code>a^((p-1)/2)</code> is congruent to <code>Jacobi(a,p)</code>,
|
||||
* modulo <code>p</code>. <code>Jacobi(a, p)</code> is a function which is
|
||||
* <code>+1</code> if a is a square modulo <code>p</code>, and <code>-1</code>
|
||||
* if it is not. For <code>a = 2</code>, this is particularly simple. It's
|
||||
* <code>+1</code> if <code>p == +/-1 (mod 8)</code>, and <code>-1</code> if
|
||||
* <code>m == +/-3 (mod 8)</code>. If <code>p == 3 (mod 4)</code>, then all
|
||||
* a strong test does is compute <code>2^((p-1)/2)</code>. and see if it's
|
||||
* <code>+1</code> or <code>-1</code>. (Euler's criterion says <i>which</i>
|
||||
* it should be.) If <code>p == 5 (mod 8)</code>, then <code>2^((p-1)/2)</code>
|
||||
* is <code>-1</code>, so the initial step in a strong test, looking at
|
||||
* <code>2^((p-1)/4)</code>, is wasted --you're not going to find a
|
||||
* <code>+/-1</code> before then if it <b>is</b> prime, and it shouldn't
|
||||
* have either of those values if it isn't. So don't bother.</p>
|
||||
*
|
||||
* <p>The remaining case is <code>p == 1 (mod 8)</code>. In this case, we
|
||||
* expect <code>2^((p-1)/2) == 1 (mod p)</code>, so we expect that the
|
||||
* square root of this, <code>2^((p-1)/4)</code>, will be <code>+/-1 (mod p)
|
||||
* </code>. Evaluating this saves us a modular squaring 1/4 of the time. If
|
||||
* it's <code>-1</code>, a strong pseudoprimality test would call <code>p</code>
|
||||
* prime as well. Only if the result is <code>+1</code>, indicating that
|
||||
* <code>2</code> is not only a quadratic residue, but a quartic one as well,
|
||||
* does a strong pseudoprimality test verify more things than this test does.
|
||||
* Good enough.</p>
|
||||
*
|
||||
* <p>We could back that down another step, looking at <code>2^((p-1)/8)</code>
|
||||
* if there was a cheap way to determine if <code>2</code> were expected to
|
||||
* be a quartic residue or not. Dirichlet proved that <code>2</code> is a
|
||||
* quadratic residue iff <code>p</code> is of the form <code>a^2 + (8*b^2)</code>.
|
||||
* All primes <code>== 1 (mod 4)</code> can be expressed as <code>a^2 +
|
||||
* (2*b)^2</code>, but I see no cheap way to evaluate this condition."</p>
|
||||
*
|
||||
* @param bn the number to test.
|
||||
* @return <code>true</code> iff the designated number passes Euler criterion
|
||||
* as implemented by Colin Plumb in his <i>bnlib</i> version 1.1.
|
||||
*/
|
||||
public static boolean passEulerCriterion(final BigInteger bn)
|
||||
{
|
||||
BigInteger bn_minus_one = bn.subtract(ONE);
|
||||
BigInteger e = bn_minus_one;
|
||||
// l is the 3 least-significant bits of e
|
||||
int l = e.and(BigInteger.valueOf(7L)).intValue();
|
||||
int j = 1; // Where to start in prime array for strong prime tests
|
||||
BigInteger a;
|
||||
int k;
|
||||
|
||||
if (l != 0)
|
||||
{
|
||||
e = e.shiftRight(1);
|
||||
a = TWO.modPow(e, bn);
|
||||
if (l == 6) // bn == 7 mod 8, expect +1
|
||||
{
|
||||
if (a.bitLength() != 1)
|
||||
{
|
||||
debugBI("Fails Euler criterion #1", bn);
|
||||
return false; // Not prime
|
||||
}
|
||||
k = 1;
|
||||
}
|
||||
else // bn == 3 or 5 mod 8, expect -1 == bn-1
|
||||
{
|
||||
a = a.add(ONE);
|
||||
if (a.compareTo(bn) != 0)
|
||||
{
|
||||
debugBI("Fails Euler criterion #2", bn);
|
||||
return false; // Not prime
|
||||
}
|
||||
k = 1;
|
||||
if ((l & 4) != 0) // bn == 5 mod 8, make odd for strong tests
|
||||
{
|
||||
e = e.shiftRight(1);
|
||||
k = 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
else // bn == 1 mod 8, expect 2^((bn-1)/4) == +/-1 mod bn
|
||||
{
|
||||
e = e.shiftRight(2);
|
||||
a = TWO.modPow(e, bn);
|
||||
if (a.bitLength() == 1)
|
||||
j = 0; // Re-do strong prime test to base 2
|
||||
else
|
||||
{
|
||||
a = a.add(ONE);
|
||||
if (a.compareTo(bn) != 0)
|
||||
{
|
||||
debugBI("Fails Euler criterion #3", bn);
|
||||
return false; // Not prime
|
||||
}
|
||||
}
|
||||
// bnMakeOdd(n) = d * 2^s. Replaces n with d and returns s.
|
||||
k = e.getLowestSetBit();
|
||||
e = e.shiftRight(k);
|
||||
k += 2;
|
||||
}
|
||||
// It's prime! Now go on to confirmation tests
|
||||
|
||||
// Now, e = (bn-1)/2^k is odd. k >= 1, and has a given value with
|
||||
// probability 2^-k, so its expected value is 2. j = 1 in the usual case
|
||||
// when the previous test was as good as a strong prime test, but 1/8 of
|
||||
// the time, j = 0 because the strong prime test to the base 2 needs to
|
||||
// be re-done.
|
||||
for (int i = j; i < 7; i++) // try only the first 7 primes
|
||||
{
|
||||
a = SMALL_PRIME[i];
|
||||
a = a.modPow(e, bn);
|
||||
if (a.bitLength() == 1)
|
||||
continue; // Passed this test
|
||||
|
||||
l = k;
|
||||
while (true)
|
||||
{
|
||||
// a = a.add(ONE);
|
||||
// if (a.compareTo(w) == 0) { // Was result bn-1?
|
||||
if (a.compareTo(bn_minus_one) == 0) // Was result bn-1?
|
||||
break; // Prime
|
||||
|
||||
if (--l == 0) // Reached end, not -1? luck?
|
||||
{
|
||||
debugBI("Fails Euler criterion #4", bn);
|
||||
return false; // Failed, not prime
|
||||
}
|
||||
// This portion is executed, on average, once
|
||||
// a = a.subtract(ONE); // Put a back where it was
|
||||
a = a.modPow(TWO, bn);
|
||||
if (a.bitLength() == 1)
|
||||
{
|
||||
debugBI("Fails Euler criterion #5", bn);
|
||||
return false; // Failed, not prime
|
||||
}
|
||||
}
|
||||
// It worked (to the base primes[i])
|
||||
}
|
||||
debugBI("Passes Euler criterion", bn);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isProbablePrime(BigInteger w)
|
||||
{
|
||||
return isProbablePrime(w, DEFAULT_CERTAINTY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper around {@link BigInteger#isProbablePrime(int)} with few pre-checks.
|
||||
*
|
||||
* @param w the integer to test.
|
||||
* @param certainty the certainty with which to compute the test.
|
||||
*/
|
||||
public static boolean isProbablePrime(BigInteger w, int certainty)
|
||||
{
|
||||
// Nonnumbers are not prime.
|
||||
if (w == null)
|
||||
return false;
|
||||
|
||||
// eliminate trivial cases when w == 0 or 1
|
||||
if (w.equals(ZERO) || w.equals(ONE))
|
||||
return false;
|
||||
|
||||
// Test if w is a known small prime.
|
||||
for (int i = 0; i < SMALL_PRIME_COUNT; i++)
|
||||
if (w.equals(SMALL_PRIME[i]))
|
||||
{
|
||||
if (DEBUG && debuglevel > 4)
|
||||
debug(w.toString(16) + " is a small prime");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if it's already a known prime
|
||||
WeakReference obj = (WeakReference) knownPrimes.get(w);
|
||||
if (obj != null && w.equals(obj.get()))
|
||||
{
|
||||
if (DEBUG && debuglevel > 4)
|
||||
debug("found in known primes");
|
||||
return true;
|
||||
}
|
||||
|
||||
// trial division with first 1000 primes
|
||||
if (hasSmallPrimeDivisor(w))
|
||||
{
|
||||
if (DEBUG && debuglevel > 4)
|
||||
debug(w.toString(16) + " has a small prime divisor. Rejected...");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Euler's criterion.
|
||||
// if (passEulerCriterion(w)) {
|
||||
// if (DEBUG && debuglevel > 4) {
|
||||
// debug(w.toString(16)+" passes Euler's criterion...");
|
||||
// }
|
||||
// } else {
|
||||
// if (DEBUG && debuglevel > 4) {
|
||||
// debug(w.toString(16)+" fails Euler's criterion. Rejected...");
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// if (DEBUG && debuglevel > 4)
|
||||
// {
|
||||
// debug(w.toString(16) + " is probable prime. Accepted...");
|
||||
// }
|
||||
|
||||
boolean result = w.isProbablePrime(certainty);
|
||||
if (result && certainty > 0) // store it in the known primes weak hash-map
|
||||
knownPrimes.put(w, new WeakReference(w));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// helper methods -----------------------------------------------------------
|
||||
|
||||
private static final void debugBI(String msg, BigInteger bn)
|
||||
{
|
||||
if (DEBUG && debuglevel > 4)
|
||||
debug("*** " + msg + ": 0x" + bn.toString(16));
|
||||
}
|
||||
}
|
|
@ -44,24 +44,18 @@ import java.util.LinkedList;
|
|||
/**
|
||||
* A monotonic sequence of integers in the finite field 2<sup>32</sup>.
|
||||
*/
|
||||
public final class Sequence extends AbstractList
|
||||
public final class Sequence
|
||||
extends AbstractList
|
||||
{
|
||||
|
||||
// Field.
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
private final Integer[] sequence;
|
||||
|
||||
// Constructor.
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Create a sequence of integers from 0 to <i>end</i>, with an increment
|
||||
* of 1. If <i>end</i> is less than 0, then the sequence will wrap around
|
||||
* through all positive integers then negative integers until the end
|
||||
* value is reached. Naturally, this will result in an enormous object,
|
||||
* so don't do this.
|
||||
*
|
||||
* Create a sequence of integers from 0 to <i>end</i>, with an increment of
|
||||
* 1. If <i>end</i> is less than 0, then the sequence will wrap around
|
||||
* through all positive integers then negative integers until the end value is
|
||||
* reached. Naturally, this will result in an enormous object, so don't do
|
||||
* this.
|
||||
*
|
||||
* @param end The ending value.
|
||||
*/
|
||||
public Sequence(int end)
|
||||
|
@ -71,10 +65,10 @@ public final class Sequence extends AbstractList
|
|||
|
||||
/**
|
||||
* Create a sequence of integers from <i>start</i> to <i>end</i>, with an
|
||||
* increment of 1. If <i>end</i> is less than <i>start</i>, then the sequence
|
||||
* will wrap around until the end value is reached. Naturally, this will
|
||||
* result in an enormous object, so don't do this.
|
||||
*
|
||||
* increment of 1. If <i>end</i> is less than <i>start</i>, then the
|
||||
* sequence will wrap around until the end value is reached. Naturally, this
|
||||
* will result in an enormous object, so don't do this.
|
||||
*
|
||||
* @param start The starting value.
|
||||
* @param end The ending value.
|
||||
*/
|
||||
|
@ -88,13 +82,13 @@ public final class Sequence extends AbstractList
|
|||
* increment of <i>span</i>. If <i>end</i> is less than <i>start</i>, then
|
||||
* the sequence will wrap around until the end value is reached. Naturally,
|
||||
* this will result in an enormous object, so don't do this.
|
||||
*
|
||||
* <p><i>span</i> can be negative, resulting in a decresing sequence.
|
||||
*
|
||||
* <p>If <i>span</i> is 0, then the sequence will contain {<i>start</i>,
|
||||
* <p>
|
||||
* <i>span</i> can be negative, resulting in a decresing sequence.
|
||||
* <p>
|
||||
* If <i>span</i> is 0, then the sequence will contain {<i>start</i>,
|
||||
* <i>end</i>} if <i>start</i> != <i>end</i>, or just the singleton
|
||||
* <i>start</i> if <i>start</i> == <i>end</i>.
|
||||
*
|
||||
*
|
||||
* @param start The starting value.
|
||||
* @param end The ending value.
|
||||
* @param span The increment value.
|
||||
|
@ -104,36 +98,26 @@ public final class Sequence extends AbstractList
|
|||
if (span == 0)
|
||||
{
|
||||
if (start != end)
|
||||
{
|
||||
sequence = new Integer[] { new Integer(start), new Integer(end) };
|
||||
}
|
||||
sequence = new Integer[] { Integer.valueOf(start),
|
||||
Integer.valueOf(end) };
|
||||
else
|
||||
{
|
||||
sequence = new Integer[] { new Integer(start) };
|
||||
}
|
||||
sequence = new Integer[] { Integer.valueOf(start) };
|
||||
}
|
||||
else
|
||||
{
|
||||
LinkedList l = new LinkedList();
|
||||
for (int i = start; i != end; i += span)
|
||||
{
|
||||
l.add(new Integer(i));
|
||||
}
|
||||
l.add(new Integer(end));
|
||||
l.add(Integer.valueOf(i));
|
||||
|
||||
l.add(Integer.valueOf(end));
|
||||
sequence = (Integer[]) l.toArray(new Integer[l.size()]);
|
||||
}
|
||||
}
|
||||
|
||||
// Instance methods.
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
public Object get(int index)
|
||||
{
|
||||
if (index < 0 || index >= size())
|
||||
{
|
||||
throw new IndexOutOfBoundsException("index=" + index + ", size="
|
||||
+ size());
|
||||
}
|
||||
throw new IndexOutOfBoundsException("index=" + index + ", size=" + size());
|
||||
return sequence[index];
|
||||
}
|
||||
|
||||
|
|
|
@ -43,25 +43,19 @@ import java.util.Collection;
|
|||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* A simple way to create immutable n-tuples. This class can be created with
|
||||
* up to four elements specified via one of the constructors, or with a
|
||||
* collection of arbitrary size.
|
||||
* A simple way to create immutable n-tuples. This class can be created with up
|
||||
* to four elements specified via one of the constructors, or with a collection
|
||||
* of arbitrary size.
|
||||
*/
|
||||
public final class SimpleList extends AbstractList
|
||||
public final class SimpleList
|
||||
extends AbstractList
|
||||
{
|
||||
|
||||
// Fields.
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
private final Object[] elements;
|
||||
|
||||
// Constructors.
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Create a singleton list.
|
||||
*
|
||||
* @param e1 The first element.
|
||||
*
|
||||
* @param element The first element.
|
||||
*/
|
||||
public SimpleList(final Object element)
|
||||
{
|
||||
|
@ -71,7 +65,7 @@ public final class SimpleList extends AbstractList
|
|||
|
||||
/**
|
||||
* Create an ordered pair (2-tuple).
|
||||
*
|
||||
*
|
||||
* @param e1 The first element.
|
||||
* @param e2 The second element.
|
||||
*/
|
||||
|
@ -84,7 +78,7 @@ public final class SimpleList extends AbstractList
|
|||
|
||||
/**
|
||||
* Create a 3-tuple.
|
||||
*
|
||||
*
|
||||
* @param e1 The first element.
|
||||
* @param e2 The second element.
|
||||
* @param e3 The third element.
|
||||
|
@ -99,7 +93,7 @@ public final class SimpleList extends AbstractList
|
|||
|
||||
/**
|
||||
* Create a 4-tuple.
|
||||
*
|
||||
*
|
||||
* @param e1 The first element.
|
||||
* @param e2 The second element.
|
||||
* @param e3 The third element.
|
||||
|
@ -124,10 +118,10 @@ public final class SimpleList extends AbstractList
|
|||
}
|
||||
|
||||
/**
|
||||
* Create an n-tuple of arbitrary size. Even if the supplied collection has
|
||||
* no natural order, the created n-tuple will have the order that the
|
||||
* elements are returned by the collection's iterator.
|
||||
*
|
||||
* Create an n-tuple of arbitrary size. Even if the supplied collection has no
|
||||
* natural order, the created n-tuple will have the order that the elements
|
||||
* are returned by the collection's iterator.
|
||||
*
|
||||
* @param c The collection.
|
||||
*/
|
||||
public SimpleList(Collection c)
|
||||
|
@ -135,14 +129,9 @@ public final class SimpleList extends AbstractList
|
|||
elements = new Object[c.size()];
|
||||
int i = 0;
|
||||
for (Iterator it = c.iterator(); it.hasNext() && i < elements.length;)
|
||||
{
|
||||
elements[i++] = it.next();
|
||||
}
|
||||
elements[i++] = it.next();
|
||||
}
|
||||
|
||||
// Instance methods.
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
public int size()
|
||||
{
|
||||
if (elements == null)
|
||||
|
@ -153,14 +142,9 @@ public final class SimpleList extends AbstractList
|
|||
public Object get(int index)
|
||||
{
|
||||
if (elements == null)
|
||||
{
|
||||
throw new IndexOutOfBoundsException("list is empty");
|
||||
}
|
||||
throw new IndexOutOfBoundsException("list is empty");
|
||||
if (index < 0 || index >= elements.length)
|
||||
{
|
||||
throw new IndexOutOfBoundsException("index=" + index + ", size="
|
||||
+ size());
|
||||
}
|
||||
throw new IndexOutOfBoundsException("index=" + index + ", size=" + size());
|
||||
return elements[index];
|
||||
}
|
||||
|
||||
|
|
|
@ -41,47 +41,37 @@ package gnu.java.security.util;
|
|||
import java.math.BigInteger;
|
||||
|
||||
/**
|
||||
* <p>A collection of utility methods used throughout this project.</p>
|
||||
* A collection of utility methods used throughout this project.
|
||||
*/
|
||||
public class Util
|
||||
{
|
||||
|
||||
// Constants and variables
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
// Hex charset
|
||||
private static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
|
||||
|
||||
// Base-64 charset
|
||||
private static final String BASE64_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
|
||||
private static final String BASE64_CHARS =
|
||||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
|
||||
|
||||
private static final char[] BASE64_CHARSET = BASE64_CHARS.toCharArray();
|
||||
|
||||
// Constructor(s)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/** Trivial constructor to enforce Singleton pattern. */
|
||||
private Util()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
// Class methods
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* <p>Returns a string of hexadecimal digits from a byte array. Each byte is
|
||||
* converted to 2 hex symbols; zero(es) included.</p>
|
||||
*
|
||||
* <p>This method calls the method with same name and three arguments as:</p>
|
||||
*
|
||||
* Returns a string of hexadecimal digits from a byte array. Each byte is
|
||||
* converted to 2 hex symbols; zero(es) included.
|
||||
* <p>
|
||||
* This method calls the method with same name and three arguments as:
|
||||
* <pre>
|
||||
* toString(ba, 0, ba.length);
|
||||
* toString(ba, 0, ba.length);
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param ba the byte array to convert.
|
||||
* @return a string of hexadecimal characters (two for each byte)
|
||||
* representing the designated input byte array.
|
||||
* @return a string of hexadecimal characters (two for each byte) representing
|
||||
* the designated input byte array.
|
||||
*/
|
||||
public static String toString(byte[] ba)
|
||||
{
|
||||
|
@ -89,17 +79,17 @@ public class Util
|
|||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a string of hexadecimal digits from a byte array, starting at
|
||||
* <code>offset</code> and consisting of <code>length</code> bytes. Each byte
|
||||
* is converted to 2 hex symbols; zero(es) included.</p>
|
||||
*
|
||||
* Returns a string of hexadecimal digits from a byte array, starting at
|
||||
* <code>offset</code> and consisting of <code>length</code> bytes. Each
|
||||
* byte is converted to 2 hex symbols; zero(es) included.
|
||||
*
|
||||
* @param ba the byte array to convert.
|
||||
* @param offset the index from which to start considering the bytes to
|
||||
* convert.
|
||||
* convert.
|
||||
* @param length the count of bytes, starting from the designated offset to
|
||||
* convert.
|
||||
* @return a string of hexadecimal characters (two for each byte)
|
||||
* representing the designated input byte sub-array.
|
||||
* convert.
|
||||
* @return a string of hexadecimal characters (two for each byte) representing
|
||||
* the designated input byte sub-array.
|
||||
*/
|
||||
public static final String toString(byte[] ba, int offset, int length)
|
||||
{
|
||||
|
@ -108,26 +98,24 @@ public class Util
|
|||
{
|
||||
k = ba[offset + i++];
|
||||
buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[k & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[ k & 0x0F];
|
||||
}
|
||||
return new String(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a string of hexadecimal digits from a byte array. Each byte is
|
||||
* converted to 2 hex symbols; zero(es) included. The argument is
|
||||
* treated as a large little-endian integer and is returned as a
|
||||
* large big-endian integer.</p>
|
||||
*
|
||||
* <p>This method calls the method with same name and three arguments as:</p>
|
||||
*
|
||||
* Returns a string of hexadecimal digits from a byte array. Each byte is
|
||||
* converted to 2 hex symbols; zero(es) included. The argument is treated as a
|
||||
* large little-endian integer and is returned as a large big-endian integer.
|
||||
* <p>
|
||||
* This method calls the method with same name and three arguments as:
|
||||
* <pre>
|
||||
* toReversedString(ba, 0, ba.length);
|
||||
* toReversedString(ba, 0, ba.length);
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param ba the byte array to convert.
|
||||
* @return a string of hexadecimal characters (two for each byte)
|
||||
* representing the designated input byte array.
|
||||
* @return a string of hexadecimal characters (two for each byte) representing
|
||||
* the designated input byte array.
|
||||
*/
|
||||
public static String toReversedString(byte[] ba)
|
||||
{
|
||||
|
@ -135,20 +123,20 @@ public class Util
|
|||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a string of hexadecimal digits from a byte array, starting at
|
||||
* <code>offset</code> and consisting of <code>length</code> bytes. Each byte
|
||||
* is converted to 2 hex symbols; zero(es) included.</p>
|
||||
*
|
||||
* <p>The byte array is treated as a large little-endian integer, and
|
||||
* is returned as a large big-endian integer.</p>
|
||||
*
|
||||
* Returns a string of hexadecimal digits from a byte array, starting at
|
||||
* <code>offset</code> and consisting of <code>length</code> bytes. Each
|
||||
* byte is converted to 2 hex symbols; zero(es) included.
|
||||
* <p>
|
||||
* The byte array is treated as a large little-endian integer, and is returned
|
||||
* as a large big-endian integer.
|
||||
*
|
||||
* @param ba the byte array to convert.
|
||||
* @param offset the index from which to start considering the bytes to
|
||||
* convert.
|
||||
* convert.
|
||||
* @param length the count of bytes, starting from the designated offset to
|
||||
* convert.
|
||||
* @return a string of hexadecimal characters (two for each byte)
|
||||
* representing the designated input byte sub-array.
|
||||
* convert.
|
||||
* @return a string of hexadecimal characters (two for each byte) representing
|
||||
* the designated input byte sub-array.
|
||||
*/
|
||||
public static final String toReversedString(byte[] ba, int offset, int length)
|
||||
{
|
||||
|
@ -157,14 +145,16 @@ public class Util
|
|||
{
|
||||
k = ba[offset + i--];
|
||||
buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[k & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[ k & 0x0F];
|
||||
}
|
||||
return new String(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a byte array from a string of hexadecimal digits.</p>
|
||||
*
|
||||
* <p>
|
||||
* Returns a byte array from a string of hexadecimal digits.
|
||||
* </p>
|
||||
*
|
||||
* @param s a string of hexadecimal ASCII characters
|
||||
* @return the decoded byte array from the input hexadecimal string.
|
||||
*/
|
||||
|
@ -174,22 +164,20 @@ public class Util
|
|||
byte[] result = new byte[((limit + 1) / 2)];
|
||||
int i = 0, j = 0;
|
||||
if ((limit % 2) == 1)
|
||||
{
|
||||
result[j++] = (byte) fromDigit(s.charAt(i++));
|
||||
}
|
||||
result[j++] = (byte) fromDigit(s.charAt(i++));
|
||||
while (i < limit)
|
||||
{
|
||||
result[j] = (byte) (fromDigit(s.charAt(i++)) << 4);
|
||||
result[j ] = (byte) (fromDigit(s.charAt(i++)) << 4);
|
||||
result[j++] |= (byte) fromDigit(s.charAt(i++));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a byte array from a string of hexadecimal digits, interpreting
|
||||
* them as a large big-endian integer and returning it as a large
|
||||
* little-endian integer.</p>
|
||||
*
|
||||
* Returns a byte array from a string of hexadecimal digits, interpreting them
|
||||
* as a large big-endian integer and returning it as a large little-endian
|
||||
* integer.
|
||||
*
|
||||
* @param s a string of hexadecimal ASCII characters
|
||||
* @return the decoded byte array from the input hexadecimal string.
|
||||
*/
|
||||
|
@ -199,45 +187,37 @@ public class Util
|
|||
byte[] result = new byte[((limit + 1) / 2)];
|
||||
int i = 0;
|
||||
if ((limit % 2) == 1)
|
||||
{
|
||||
result[i++] = (byte) fromDigit(s.charAt(--limit));
|
||||
}
|
||||
result[i++] = (byte) fromDigit(s.charAt(--limit));
|
||||
while (limit > 0)
|
||||
{
|
||||
result[i] = (byte) fromDigit(s.charAt(--limit));
|
||||
result[i ] = (byte) fromDigit(s.charAt(--limit));
|
||||
result[i++] |= (byte) (fromDigit(s.charAt(--limit)) << 4);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a number from <code>0</code> to <code>15</code> corresponding
|
||||
* to the designated hexadecimal digit.</p>
|
||||
*
|
||||
* Returns a number from <code>0</code> to <code>15</code> corresponding
|
||||
* to the designated hexadecimal digit.
|
||||
*
|
||||
* @param c a hexadecimal ASCII symbol.
|
||||
*/
|
||||
public static int fromDigit(char c)
|
||||
{
|
||||
if (c >= '0' && c <= '9')
|
||||
{
|
||||
return c - '0';
|
||||
}
|
||||
return c - '0';
|
||||
else if (c >= 'A' && c <= 'F')
|
||||
{
|
||||
return c - 'A' + 10;
|
||||
}
|
||||
return c - 'A' + 10;
|
||||
else if (c >= 'a' && c <= 'f')
|
||||
{
|
||||
return c - 'a' + 10;
|
||||
}
|
||||
return c - 'a' + 10;
|
||||
else
|
||||
throw new IllegalArgumentException("Invalid hexadecimal digit: " + c);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a string of 8 hexadecimal digits (most significant digit first)
|
||||
* corresponding to the unsigned integer <code>n</code>.</p>
|
||||
*
|
||||
* Returns a string of 8 hexadecimal digits (most significant digit first)
|
||||
* corresponding to the unsigned integer <code>n</code>.
|
||||
*
|
||||
* @param n the unsigned integer to convert.
|
||||
* @return a hexadecimal string 8-character long.
|
||||
*/
|
||||
|
@ -253,8 +233,8 @@ public class Util
|
|||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a string of hexadecimal digits from an integer array. Each int
|
||||
* is converted to 4 hex symbols.</p>
|
||||
* Returns a string of hexadecimal digits from an integer array. Each int is
|
||||
* converted to 4 hex symbols.
|
||||
*/
|
||||
public static String toString(int[] ia)
|
||||
{
|
||||
|
@ -268,17 +248,17 @@ public class Util
|
|||
buf[j++] = HEX_DIGITS[(k >>> 20) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[(k >>> 16) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[(k >>> 12) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[(k >>> 8) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[k & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[(k >>> 8) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[ k & 0x0F];
|
||||
}
|
||||
return new String(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a string of 16 hexadecimal digits (most significant digit first)
|
||||
* corresponding to the unsigned long <code>n</code>.</p>
|
||||
*
|
||||
* Returns a string of 16 hexadecimal digits (most significant digit first)
|
||||
* corresponding to the unsigned long <code>n</code>.
|
||||
*
|
||||
* @param n the unsigned long to convert.
|
||||
* @return a hexadecimal string 16-character long.
|
||||
*/
|
||||
|
@ -287,18 +267,18 @@ public class Util
|
|||
char[] b = new char[16];
|
||||
for (int i = 15; i >= 0; i--)
|
||||
{
|
||||
b[i] = HEX_DIGITS[(int) (n & 0x0FL)];
|
||||
b[i] = HEX_DIGITS[(int)(n & 0x0FL)];
|
||||
n >>>= 4;
|
||||
}
|
||||
return new String(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Similar to the <code>toString()</code> method except that the Unicode
|
||||
* Similar to the <code>toString()</code> method except that the Unicode
|
||||
* escape character is inserted before every pair of bytes. Useful to
|
||||
* externalise byte arrays that will be constructed later from such strings;
|
||||
* eg. s-box values.</p>
|
||||
*
|
||||
* eg. s-box values.
|
||||
*
|
||||
* @throws ArrayIndexOutOfBoundsException if the length is odd.
|
||||
*/
|
||||
public static String toUnicodeString(byte[] ba)
|
||||
|
@ -307,11 +287,11 @@ public class Util
|
|||
}
|
||||
|
||||
/**
|
||||
* <p>Similar to the <code>toString()</code> method except that the Unicode
|
||||
* Similar to the <code>toString()</code> method except that the Unicode
|
||||
* escape character is inserted before every pair of bytes. Useful to
|
||||
* externalise byte arrays that will be constructed later from such strings;
|
||||
* eg. s-box values.</p>
|
||||
*
|
||||
* eg. s-box values.
|
||||
*
|
||||
* @throws ArrayIndexOutOfBoundsException if the length is odd.
|
||||
*/
|
||||
public static final String toUnicodeString(byte[] ba, int offset, int length)
|
||||
|
@ -324,31 +304,27 @@ public class Util
|
|||
while (i < length)
|
||||
{
|
||||
sb.append("\\u");
|
||||
|
||||
k = ba[offset + i++];
|
||||
sb.append(HEX_DIGITS[(k >>> 4) & 0x0F]);
|
||||
sb.append(HEX_DIGITS[k & 0x0F]);
|
||||
|
||||
sb.append(HEX_DIGITS[ k & 0x0F]);
|
||||
k = ba[offset + i++];
|
||||
sb.append(HEX_DIGITS[(k >>> 4) & 0x0F]);
|
||||
sb.append(HEX_DIGITS[k & 0x0F]);
|
||||
|
||||
sb.append(HEX_DIGITS[ k & 0x0F]);
|
||||
if ((++j % 8) == 0)
|
||||
{
|
||||
sb.append("\"+").append('\n').append("\"");
|
||||
}
|
||||
sb.append("\"+").append('\n').append("\"");
|
||||
}
|
||||
sb.append("\"").append('\n');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Similar to the <code>toString()</code> method except that the Unicode
|
||||
* Similar to the <code>toString()</code> method except that the Unicode
|
||||
* escape character is inserted before every pair of bytes. Useful to
|
||||
* externalise integer arrays that will be constructed later from such
|
||||
* strings; eg. s-box values.</p>
|
||||
*
|
||||
* @throws ArrayIndexOutOfBoundsException if the length is not a multiple of 4.
|
||||
* strings; eg. s-box values.
|
||||
*
|
||||
* @throws ArrayIndexOutOfBoundsException if the length is not a multiple of
|
||||
* 4.
|
||||
*/
|
||||
public static String toUnicodeString(int[] ia)
|
||||
{
|
||||
|
@ -367,14 +343,11 @@ public class Util
|
|||
sb.append(HEX_DIGITS[(k >>> 16) & 0x0F]);
|
||||
sb.append("\\u");
|
||||
sb.append(HEX_DIGITS[(k >>> 12) & 0x0F]);
|
||||
sb.append(HEX_DIGITS[(k >>> 8) & 0x0F]);
|
||||
sb.append(HEX_DIGITS[(k >>> 4) & 0x0F]);
|
||||
sb.append(HEX_DIGITS[k & 0x0F]);
|
||||
|
||||
sb.append(HEX_DIGITS[(k >>> 8) & 0x0F]);
|
||||
sb.append(HEX_DIGITS[(k >>> 4) & 0x0F]);
|
||||
sb.append(HEX_DIGITS[ k & 0x0F]);
|
||||
if ((++j % 4) == 0)
|
||||
{
|
||||
sb.append("\"+").append('\n').append("\"");
|
||||
}
|
||||
sb.append("\"+").append('\n').append("\"");
|
||||
}
|
||||
sb.append("\"").append('\n');
|
||||
return sb.toString();
|
||||
|
@ -388,20 +361,20 @@ public class Util
|
|||
for (int i = 0; i < limit; i++)
|
||||
{
|
||||
c = s.charAt(i >>> 1);
|
||||
result[i] = (byte) (((i & 1) == 0) ? c >>> 8 : c);
|
||||
result[i] = (byte)(((i & 1) == 0) ? c >>> 8 : c);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Dumps a byte array as a string, in a format that is easy to read for
|
||||
* Dumps a byte array as a string, in a format that is easy to read for
|
||||
* debugging. The string <code>m</code> is prepended to the start of each
|
||||
* line.</p>
|
||||
*
|
||||
* <p>If <code>offset</code> and <code>length</code> are omitted, the whole
|
||||
* line.
|
||||
* <p>
|
||||
* If <code>offset</code> and <code>length</code> are omitted, the whole
|
||||
* array is used. If <code>m</code> is omitted, nothing is prepended to each
|
||||
* line.</p>
|
||||
*
|
||||
* line.
|
||||
*
|
||||
* @param data the byte array to be dumped.
|
||||
* @param offset the offset within <i>data</i> to start from.
|
||||
* @param length the number of bytes to dump.
|
||||
|
@ -411,23 +384,17 @@ public class Util
|
|||
public static String dumpString(byte[] data, int offset, int length, String m)
|
||||
{
|
||||
if (data == null)
|
||||
{
|
||||
return m + "null\n";
|
||||
}
|
||||
return m + "null\n";
|
||||
StringBuffer sb = new StringBuffer(length * 3);
|
||||
if (length > 32)
|
||||
{
|
||||
sb.append(m).append("Hexadecimal dump of ").append(length).append(
|
||||
" bytes...\n");
|
||||
}
|
||||
sb.append(m).append("Hexadecimal dump of ")
|
||||
.append(length).append(" bytes...\n");
|
||||
// each line will list 32 bytes in 4 groups of 8 each
|
||||
int end = offset + length;
|
||||
String s;
|
||||
int l = Integer.toString(length).length();
|
||||
if (l < 4)
|
||||
{
|
||||
l = 4;
|
||||
}
|
||||
l = 4;
|
||||
for (; offset < end; offset += 32)
|
||||
{
|
||||
if (length > 32)
|
||||
|
@ -437,16 +404,10 @@ public class Util
|
|||
}
|
||||
int i = 0;
|
||||
for (; i < 32 && offset + i + 7 < end; i += 8)
|
||||
{
|
||||
sb.append(toString(data, offset + i, 8)).append(' ');
|
||||
}
|
||||
sb.append(toString(data, offset + i, 8)).append(' ');
|
||||
if (i < 32)
|
||||
{
|
||||
for (; i < 32 && offset + i < end; i++)
|
||||
{
|
||||
sb.append(byteToString(data[offset + i]));
|
||||
}
|
||||
}
|
||||
for (; i < 32 && offset + i < end; i++)
|
||||
sb.append(byteToString(data[offset + i]));
|
||||
sb.append('\n');
|
||||
}
|
||||
return sb.toString();
|
||||
|
@ -468,9 +429,9 @@ public class Util
|
|||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a string of 2 hexadecimal digits (most significant digit first)
|
||||
* corresponding to the lowest 8 bits of <code>n</code>.</p>
|
||||
*
|
||||
* Returns a string of 2 hexadecimal digits (most significant digit first)
|
||||
* corresponding to the lowest 8 bits of <code>n</code>.
|
||||
*
|
||||
* @param n the byte value to convert.
|
||||
* @return a string of 2 hex characters representing the input.
|
||||
*/
|
||||
|
@ -481,15 +442,15 @@ public class Util
|
|||
}
|
||||
|
||||
/**
|
||||
* <p>Converts a designated byte array to a Base-64 representation, with the
|
||||
* Converts a designated byte array to a Base-64 representation, with the
|
||||
* exceptions that (a) leading 0-byte(s) are ignored, and (b) the character
|
||||
* '.' (dot) shall be used instead of "+' (plus).</p>
|
||||
*
|
||||
* <p>Used by SASL password file manipulation primitives.</p>
|
||||
*
|
||||
* '.' (dot) shall be used instead of "+' (plus).
|
||||
* <p>
|
||||
* Used by SASL password file manipulation primitives.
|
||||
*
|
||||
* @param buffer an arbitrary sequence of bytes to represent in Base-64.
|
||||
* @return unpadded (without the '=' character(s)) Base-64 representation of
|
||||
* the input.
|
||||
* the input.
|
||||
*/
|
||||
public static final String toBase64(byte[] buffer)
|
||||
{
|
||||
|
@ -535,9 +496,7 @@ public class Util
|
|||
notleading = true;
|
||||
}
|
||||
if (pos >= len)
|
||||
{
|
||||
break;
|
||||
}
|
||||
break;
|
||||
else
|
||||
{
|
||||
try
|
||||
|
@ -555,44 +514,38 @@ public class Util
|
|||
while (true);
|
||||
|
||||
if (notleading)
|
||||
{
|
||||
return sb.toString();
|
||||
}
|
||||
return sb.toString();
|
||||
return "0";
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>The inverse function of the above.</p>
|
||||
*
|
||||
* <p>Converts a string representing the encoding of some bytes in Base-64
|
||||
* to their original form.</p>
|
||||
*
|
||||
* The inverse function of the above.
|
||||
* <p>
|
||||
* Converts a string representing the encoding of some bytes in Base-64 to
|
||||
* their original form.
|
||||
*
|
||||
* @param str the Base-64 encoded representation of some byte(s).
|
||||
* @return the bytes represented by the <code>str</code>.
|
||||
* @throws NumberFormatException if <code>str</code> is <code>null</code>, or
|
||||
* <code>str</code> contains an illegal Base-64 character.
|
||||
* @throws NumberFormatException if <code>str</code> is <code>null</code>,
|
||||
* or <code>str</code> contains an illegal Base-64 character.
|
||||
* @see #toBase64(byte[])
|
||||
*/
|
||||
public static final byte[] fromBase64(String str)
|
||||
{
|
||||
int len = str.length();
|
||||
if (len == 0)
|
||||
{
|
||||
throw new NumberFormatException("Empty string");
|
||||
}
|
||||
throw new NumberFormatException("Empty string");
|
||||
byte[] a = new byte[len + 1];
|
||||
int i, j;
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
a[i] = (byte) BASE64_CHARS.indexOf(str.charAt(i));
|
||||
}
|
||||
catch (ArrayIndexOutOfBoundsException x)
|
||||
{
|
||||
throw new NumberFormatException("Illegal character at #" + i);
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
a[i] = (byte) BASE64_CHARS.indexOf(str.charAt(i));
|
||||
}
|
||||
catch (ArrayIndexOutOfBoundsException x)
|
||||
{
|
||||
throw new NumberFormatException("Illegal character at #" + i);
|
||||
}
|
||||
i = len - 1;
|
||||
j = len;
|
||||
try
|
||||
|
@ -601,42 +554,31 @@ public class Util
|
|||
{
|
||||
a[j] = a[i];
|
||||
if (--i < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
break;
|
||||
a[j] |= (a[i] & 0x03) << 6;
|
||||
j--;
|
||||
a[j] = (byte) ((a[i] & 0x3C) >>> 2);
|
||||
a[j] = (byte)((a[i] & 0x3C) >>> 2);
|
||||
if (--i < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
break;
|
||||
a[j] |= (a[i] & 0x0F) << 4;
|
||||
j--;
|
||||
a[j] = (byte) ((a[i] & 0x30) >>> 4);
|
||||
a[j] = (byte)((a[i] & 0x30) >>> 4);
|
||||
if (--i < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
break;
|
||||
a[j] |= (a[i] << 2);
|
||||
j--;
|
||||
a[j] = 0;
|
||||
if (--i < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ignored)
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{ // ignore leading 0-bytes
|
||||
while (a[j] == 0)
|
||||
{
|
||||
j++;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
catch (Exception x)
|
||||
{
|
||||
|
@ -650,36 +592,31 @@ public class Util
|
|||
// BigInteger utilities ----------------------------------------------------
|
||||
|
||||
/**
|
||||
* <p>Treats the input as the MSB representation of a number, and discards
|
||||
* Treats the input as the MSB representation of a number, and discards
|
||||
* leading zero elements. For efficiency, the input is simply returned if no
|
||||
* leading zeroes are found.</p>
|
||||
*
|
||||
* leading zeroes are found.
|
||||
*
|
||||
* @param n the {@link BigInteger} to trim.
|
||||
* @return the byte array representation of the designated {@link BigInteger}
|
||||
* with no leading 0-bytes.
|
||||
* with no leading 0-bytes.
|
||||
*/
|
||||
public static final byte[] trim(BigInteger n)
|
||||
{
|
||||
byte[] in = n.toByteArray();
|
||||
if (in.length == 0 || in[0] != 0)
|
||||
{
|
||||
return in;
|
||||
}
|
||||
return in;
|
||||
int len = in.length;
|
||||
int i = 1;
|
||||
while (in[i] == 0 && i < len)
|
||||
{
|
||||
++i;
|
||||
}
|
||||
++i;
|
||||
byte[] result = new byte[len - i];
|
||||
System.arraycopy(in, i, result, 0, len - i);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a hexadecimal dump of the trimmed bytes of a {@link BigInteger}.
|
||||
* </p>
|
||||
*
|
||||
* Returns a hexadecimal dump of the trimmed bytes of a {@link BigInteger}.
|
||||
*
|
||||
* @param x the {@link BigInteger} to display.
|
||||
* @return the string representation of the designated {@link BigInteger}.
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue