I use two different libraries to generate the SHA-1 hash for use in file verification β an older version of the Crypto ++ library and the Digest :: SHA1 class implemented by Ruby. Although I saw other instances of inconsistent hashes caused by differences in encoding, the two libraries output hashes that are almost identical.
For example, transferring a file through each process gives the following results:
Crypto ++ 01c15e4f46d8181b984fa2a2c740f8f67130acac
Ruby: eac15e4f46d8181b984fa2a2c740f8f67130acac
As you can see, only the first two characters of the hash string are different, and this behavior is repeated in many files. I reviewed the source code for each implementation, and the only difference I found at first glance was in the hex format, which is used for 160-bit hashing. I have no idea how this hex is used in the algorithm, and I thought it would probably be faster for me to ask a question if someone had this problem before.
I have included data from the respective libraries below. I also included values ββfrom OpenSSL, as each of the three libraries had slightly different values.
Crypto ++:
digest[0] = 0x67452301L; digest[1] = 0xEFCDAB89L; digest[2] = 0x98BADCFEL; digest[3] = 0x10325476L; digest[4] = 0xC3D2E1F0L;
Ruby:
context->state[0] = 0x67452301; context->state[1] = 0xEFCDAB89; context->state[2] = 0x98BADCFE; context->state[3] = 0x10325476; context->state[4] = 0xC3D2E1F0;
OpenSSL:
#define INIT_DATA_h0 0x67452301UL #define INIT_DATA_h1 0xefcdab89UL #define INIT_DATA_h2 0x98badcfeUL #define INIT_DATA_h3 0x10325476UL #define INIT_DATA_h4 0xc3d2e1f0UL
By the way, here is the code that is used to generate the hash in Ruby. I do not have access to the source code for the implementation of Crypto ++.
File.class_eval do def self.hash_digest filename, options = {} opts = {:buffer_length => 1024, :method => :sha1}.update(options) hash_func = (opts[:method].to_s == 'sha1') ? Digest::SHA1.new : Digest::MD5.new open(filename, "r") do |f| while !f.eof b = f.read hash_func.update(b) end end hash_func.hexdigest end end