Code review request: 7197245: Eliminate sun.security.ssl.JsseJce dependency on sun.security.ec
Michael StJohns
mstjohns at comcast.net
Wed Sep 12 17:15:09 UTC 2012
At 06:31 AM 9/12/2012, Vincent Ryan wrote:
>>Also, instead of the function trimZeroes, how about instead a
>>"static byte getMagnitude (BigInteger val, int magSize)" method which is really what you're trying to do. Still throw the RuntimeError, but do it inside that method. This is a method I really wish were part of BigInteger.
>
>Returns a byte[] I guess. What value is passed in magSize?
>
Here's a version that covers all the possibilities for getting the magnitude from a BigInteger. It has the property that
orig.equals (new BigInteger (orig.signum(), getMagnitude(orig, magSize)))
for any value of magSize >= ((orig.bitLength() +7 ) >> 3)
Mike
> static byte[] getMagnitude (BigInteger value, int magSize) {
>
> if (BigInteger.ZERO.equals(value))
> return new byte[1];
> BigInteger val = value;
> if (value.signum() == -1)
> val = value.negate();
> int repLength = (val.bitLength()+7) >> 3;
>
> if (magSize < repLength)
> throw new RuntimeException ("Magnitude size invalid");
> byte[] data = val.toByteArray();
> if (magSize == data.length)
> return data;
>
> byte[] result = new byte[magSize];
> if (data.length > result.length) { // leading zeros
> System.arraycopy (data, data.length-magSize,
> result, 0, magSize);
> } else { // needs padding
> System.arraycopy (data, 0,
> result, magSize-data.length,
> data.length);
> }
>
>
> return result;
> }
More information about the security-dev
mailing list