# Sha1/base64

hello,

i have this string in input :

97975000-5RUT DE
PRUEBA

3312002003-09-040a4O6Kbx8Qj3K4iWSP4w7KneZYeJ+g/prihYtIEolKt3cykSxl1zO8vSXu397QhTmsX7SBEudTUx++2zDXBhZw==Aw==100

i try to compute the SHA-1 : sha = Digest::SHA1.hexdigest(input)
I obtain correct result => 9A2115CC03E05F99A996FBCE47C4554905D75D1D

After this first operation, i try to obtain the same result in Base64
(res=Base64.b64encode(sha)). If the operation was created with success,
i should obtain => miEVzAPgX5mplvvOR8RVSQXXXR0=
But, i find an other result and i don’t understand what is the meaning
of that ?

Can you help me please ?

miEVzAPgX5mplvvOR8RVSQXXXR0=

ben ******* wrote:

i try to compute the SHA-1 : sha = Digest::SHA1.hexdigest(input)
I obtain correct result => 9A2115CC03E05F99A996FBCE47C4554905D75D1D

After this first operation, i try to obtain the same result in Base64
(res=Base64.b64encode(sha)). If the operation was created with success,
i should obtain => miEVzAPgX5mplvvOR8RVSQXXXR0=
But, i find an other result and i don’t understand what is the meaning
of that ?

Since you didn’t show what you actually get from your base64 encoding,
it’s hard to be sure what you’re doing wrong. But I guess you are base64
encoding the output of ‘hexdigest’ which is a hex string (40 ASCII
characters 0-9 and A-F) rather than the 20 bytes binary.

Compare:

“miEVzAPgX5mplvvOR8RVSQXXXR0=”.unpack(“m”)
=> ["\232!\025\314\003\340_\231\251\226\373\316G\304UI\005\327]\035"]

“miEVzAPgX5mplvvOR8RVSQXXXR0=”.unpack(“m”).first.size
=> 20

versus:

str = [“9A2115CC03E05F99A996FBCE47C4554905D75D1D”].pack(“m”)
=> “OUEyMTE1Q0MwM0UwNUY5OUE5OTZGQkNFNDdDNDU1NDkwNUQ3NUQxRA==\n”

str.unpack(“m”)
=> [“9A2115CC03E05F99A996FBCE47C4554905D75D1D”]

str.unpack(“m”).first.size
=> 40

If you want to generate the former, then use Digest::SHA1.digest()

require ‘digest/sha1’
=> true

Digest::SHA1.digest(“hello”)
=> “\252\364\306\035\334\305\350\242\332\276\336\017;H,\331\256\251CM”

Digest::SHA1.hexdigest(“hello”)
=> “aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d”

ben ******* wrote:

I don’t understand how the software compute the digestvalue =>
vKNk56BFnbFAM7/65dGNZCaIW2Q=

The error message tells you: follow the link to the XML Canonicalisation
algorithm it has used.

I don’t know if there is an existing Ruby implementation of this you can
re-use.

In any case, the “signature” it has added is most definitely not just a
Hash.

i have an other question. To find =>miEVzAPgX5mplvvOR8RVSQXXXR0=, i use
:

Base64.encode64(Digest::SHA1.digest(‘97975000-5RUT DE
PRUEBA

3312002003-09-040a4O6Kbx8Qj3K4iWSP4w7KneZYeJ+g/prihYtIEolKt3cykSxl1zO8vSXu397QhTmsX7SBEudTUx++2zDXBhZw==Aw==100’)).chomp

For example, i open this XML file :

<?xml version="1.0" encoding="UTF-8"?> version17 2009-08-07T00:00:00+00:00 asterix rue des abricotiers machin

With “KeyToolIuI” (freeware), i sign the file to create an embedded
signature. i obtain :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

version17
2009-08-07T00:00:00+00:00

asterix