BigInteger(byte[]) constructor bugfix.
Mark J. Roberts
mjr@statesmean.com
Sat Mar 31 23:02:00 GMT 2001
BigInteger(byte[]) currently returns bogus data, due to a bug in the
byteArrayToIntArray method -- it appears to be simply wrong.
Here's a test case illustrating the problem:
public class Test {
public static void main(String[] args)
{
byte val[] = {73, -106, 2, -46}; // aka "1234567890"
int words[] = byteArrayToIntArray(val, val[0] < 0 ? -1 : 0);
System.out.print(words.length+" words:");
for (int i = 0 ; i < words.length ; i++) System.out.print(" "+words[i]);
System.out.println("");
}
private static int[] byteArrayToIntArray(byte[] bytes, int sign)
{
// Determine number of words needed.
int[] words = new int[(bytes.length + 3) / 4 + 1];
int nwords = words.length;
// For simplicity, tack on an extra word of sign at the front,
// it will be canonicalized out later. */
if ((bytes.length % 4) != 0) words[--nwords] = sign;
// Create a int out of modulo 4 high order bytes.
int bptr = 0;
int word = sign;
for (int i = bytes.length % 4; i > 0; --i, bptr++)
word = (word << 8) | (((int) bytes[bptr]) & 0xff);
words[--nwords] = word;
// Elements remaining in byte[] are a multiple of 4.
while (nwords > 0)
words[--nwords] = bytes[bptr++] << 24 |
(((int) bytes[bptr++]) & 0xff) << 16 |
(((int) bytes[bptr++]) & 0xff) << 8 |
(((int) bytes[bptr++]) & 0xff);
return words;
}
}
Anyway, this patch fixed it (Freenet crypto works now):
Index: libjava/java/math/BigInteger.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/math/BigInteger.java,v
retrieving revision 1.10.4.1
diff -r1.10.4.1 BigInteger.java
223c223
< int[] words = new int[(bytes.length + 3) / 4 + 1];
---
> int[] words = new int[bytes.length/4 + 1];
225,228d224
<
< // For simplicity, tack on an extra word of sign at the front,
< // it will be canonicalized out later. */
< words[--nwords] = sign;
--
Satan, oscillate my metallic sonatas!
Mark Roberts | mjr@statesmean.com
More information about the Java
mailing list