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()
instead of Digest::SHA1.hexdigest()

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”

Thanks for your answer. I don’t mind “hexdigest”.

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

rue des abricotiers machin vKNk56BFnbFAM7/65dGNZCaIW2Q=fC4EZOO2aLCuXI5YvTxTUGWUVdAv1KAz22KnVhDV5v2bzzFsGlJVT8/shTLLt34hpjcl1Z/E/vAa PVbUuGSRKfG3tD7JA/hx04UZNMpPPQ18TjcjHBoF3xWuyclIavmRQoC5tKZQqccNDWpy3EwYFxvF 4HdnXah8xDgN8jSvzepuE4YY2UGQEsAy2dJRCfcjFngISkfdW9zZC3KtdKOcaaZmqi4ksJYgMhiM 76Y+amYP80imQ2qym4bdOXc4xkXg//pO3x+IRKoO/tJ0xfLBiIrJJIwbbTfFJI/oZxi45lyBpFik tZRrR/2jaxoADOa15ZdrWvdfgDMgaTGNChdqFQ==CN=me,C=FRMIICtzCCAZ8CBEp/88wwDQYJKoZIhvcNAQEFBQAwIDELMAkGA1UEBhMCRlIxETAPBgNVBAMMCGJv dXNxdWV0MB4XDTA5MDgxMDEwMTc0OFoXDTEyMDgwOTEwMTc0OFowIDELMAkGA1UEBhMCRlIxETAP BgNVBAMMCGJvdXNxdWV0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkaftCNBNm++9 lDFAteXfJu9nfqcfsOvIlqhUEQHvLfJy1yQqPH1wgSXL2/y3FGUvegGuRkkdxYckuwQncuQImvxE KI0ovuQt5PIt0cc4CAdTR1W2WoTgqRuB0Fu092Xjr7sbWM5yy11j22qSdGk2zWQjFdKB9awLEQs9 ASnJK/zAdNs2XtEaTN9Nrb1LTQmq7CKNUv0HUhzZ6I6GkJUn2Kk3hnD+Re7zTUUSDFMKss+Tre05 kdLIMBEeXxuwNr6M5/lx8d934XBucrC1DbKHOJvQzL/1Q+45+w6ZSgyYafxOBvWPHHOJ057f+MPK iAlGjN/z0jWY5gZlyPNkZXBuAwIDAQABMA0GCSqGSIb3DQEBBQUAA4IBAQBpK4OKjmoolPvYQojj nNvcNbTwVXyvPLizU+mKSlinr6WT2qpcDq4pH/CmzPBlsLRuG/eTXHiUQPU8o7tMZRwDebOPgHW3 3AdNV6eQH+IWeXHz14z+y0Fgfp+4faQ0R+bWATcUgiDjFtFRmH4SzY8AzE+j5iph8dgUeYpQxeXs wS/gU5d2wT24M3wFJ2GYk/Ehzi9gQ4uL6IF2b7LVXn9tHSyLOIMyMXXSjsqFyRXsx6wJMIyc5khH y6OtifG3mFLuuzlUDrwwSdVbVjkNQFeK/ujb/mUOqP5dqXD6M0wj4QB8QwswQPgRwBgHK4tJ30ZL tbzKt6cMpxFcJ3/YG7Wv

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

I’am not able to find this value using sha1 and base64 method on data!

Do you have solution to do it ?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs