From: Albretch Mueller on
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?
From: alexandre_paterson on
On Dec 26, 9:08 am, Albretch Mueller <lbrt...(a)gmail.com> wrote:
>  I justnoticed that Linux's sha256sum of a binary file
> ~
> $ sha256sum -b *.bin
> *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?

Left padding. It's not a hack. It's not hard to write a left pad
method yourself.

Jakarta commons's StringUtils has a leftPad method.

Or you logical OR your BigInteger with a bigint that has its
257 bit set then you discard the char at 0.

Both would work and are really simple.

From: alexandre_paterson on
On Dec 26, 11:53 am, alexandre_pater...(a)yahoo.fr wrote:
> On Dec 26, 9:08 am, Albretch Mueller <lbrt...(a)gmail.com> wrote:
>
> >  I justnoticed that Linux's sha256sum of a binary file
> > ~
> > $ sha256sum -b *.bin
> > *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?
>
> Left padding.  It's not a hack.  It's not hard to write a left pad
> method yourself.

Replying to myself, taking a quick look at the BigInteger method
there's this:

zeros[63] =
"000000000000000000000000000000000000000000000000000000000000000";

Which screams padding :)

Googling-on-"padding"-is-your-friend :)




From: Albretch Mueller on
> Left padding. It's not a hack.
~
well, I see. I could go " ...".endsWith() and then make sure that the
left side matches all 0's or I may use the DecimalFormat class ...
~
I just thought BigInteger or some Base16 method in java which does
not assume you are converting a number, would do
~
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?
~
lbrtchx
From: Mike Schilling on
Albretch Mueller wrote:
>> Left padding. It's not a hack.
> ~
> well, I see. I could go " ...".endsWith() and then make sure that
> the
> left side matches all 0's or I may use the DecimalFormat class ...

Or simply pad with 2 * array.length - string.length() zeros