From: Brian Candler Date: 2009-08-12T19:52:50+09:00 Subject: Re: sha1/base64 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" >> -- Posted via http://www.ruby-forum.com/.