From: Eric Sosman on
On 12/26/2009 3:08 AM, Albretch Mueller wrote:
> I justnoticed that Linux's sha256sum of a binary file
> ~
> $ sha256sum -b *.bin
> 035dfc8af407ad305c9b6ad9c265efb57cc283684a37e7b94853c7aa76919ad0
> *p69.bin
> ~
> and java sha-256 signatures were not the same because (new BigInteger
> (1, MD.digest())).toString(16) discards the leading 0's producing
> ~
> 35dfc8af407ad305c9b6ad9c265efb57cc283684a37e7b94853c7aa76919ad0
> ~
> How do you deal with these subtle differences without a hack? Is
> there a way to instruct BigInteger to keep leading 0's? Or anyother
> way to do that?

How many leading zeroes should a BigInteger keep? (Hint:
A BigInteger can have as many as 2147483647 value bits, or
536870912 hexadecimal digits. How many of those should toString()
produce?)

In other words, your problem is not with "retaining leading
zeroes," but with padding a number to a specified width. At a
brief glance, it seems the problem is self-inflicted: It's the
conversion from byte[] to BigInteger that "loses" the information
about width, not the conversion from BigInteger to String.

--
Eric Sosman
esosman(a)ieee-dot-org.invalid
From: Lew on
Albretch Mueller wrote:
> By the way. Where does java [sic] stash its Base{16, 32, 64} libraries? is
> there any way to easily/reliably access them, or will I have to use
> the jakarta [sic] commons libraries?

What do you mean by "its Base{16, 32, 64} libraries"? What functionality do
you seek?

Since number base is strictly a display artifact, you find Java's support in
various conversion methods:

<http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html#BigInteger(java.lang.String,%20int)>
<http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html#toString(int)>
<http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#valueOf(java.lang.String,%20int)>
<http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#toString(int,%20int)>
<http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#toHexString(int)>
<http://java.sun.com/javase/6/docs/api/java/lang/String.html#format(java.lang.String,%20java.lang.Object...)>

to name a few.

--
Lew
From: markspace on
Lew wrote:

> Albretch Mueller wrote:
>> By the way. Where does java [sic] stash its Base{16, 32, 64}
>> libraries? is
>> there any way to easily/reliably access them, or will I have to use
>> the jakarta [sic] commons libraries?
>
> What do you mean by "its Base{16, 32, 64} libraries"? What
> functionality do you seek?
>


I think he means the utilities provided by Jakarta Commons.

<http://commons.apache.org/codec/>
From: Tom Anderson on
On Sat, 26 Dec 2009, Albretch Mueller wrote:

> By the way. Where does java stash its Base{16, 32, 64} libraries? is
> there any way to easily/reliably access them, or will I have to use the
> jakarta commons libraries?

You can get base64 from javax.mail:

http://www.rgagnon.com/javadetails/java-0598.html

You can get short kinds of base16 from Integer et al, but you'll have to
roll your own for longer data.

I don't know of any base32 support. Nor, tragically, base85, which is of
course the best encoding.

tom

--
IMPORTANCE MEMO: >>> WHEN YOU BUY AN N-GAGE QD <<< PLEASE, please CONTINUE
TO TALK ON THE SIDE!!$ Note: the other party will not be able to hear you,
BUT WHO REALLY CRAPS A THING, SIDETALKIN' 2009++!!!
From: markspace on
Albretch Mueller wrote:
>> Left padding. It's not a hack.
> ~
> I just thought BigInteger or some Base16 method in java which does
> not assume you are converting a number, would do


I wouldn't use either left padding or DecimalFormat. What happens when
you get a negative number in BigInteger? I don't think this is easy to
deal with.

I also wouldn't use Jakarta Commons' codec just for this one function.
It's easy enough to roll your own. Lightly tested:


private static final String hexDigigs = "0123456789abcdef";
public static String asHexString( byte[] bytes ) {
StringBuilder sb = new StringBuilder();
for( byte b : bytes ) {
sb.append( hexDigigs.charAt( (b & 0xFF) >>> 4 ) );
sb.append( hexDigigs.charAt( b & 0xF ) );
}
return sb.toString();
}