Different Values for MD5 between Ruby and JavaScript -
i need able run md5 hash on same file clientside javascript app , on server ruby.
currently cannot both hashes identical.
on client, i'm using jquery file upload upload file s3 bucket. using crypto hash file in callback:
file = data.files[0] filename = file.name; md5 = cryptojs.md5(cryptojs.enc.hex.parse(data.files[0].tostring()));
this gives me: ee9cd5bf4272fc35bd57d184553bd25b
in ruby, it's module digest::md5 module used gem doing hashing:
digest::md5.file(file).to_s
this gives me: 4d51c9a4d3fd076489d6c96614ebce61
i have no control on ruby side of things, why might checksum generated crypto different?
note can test locally, using same api:
path = 'path/to/file.jpg' digest::md5.file(f).hexdigest # 4d51c9a4d3fd076489d6c96614ebce61
the file large jpg (~ 1.8meg)
update: in response @kxyz's answer, results using different encoders crypto-js are:
cryptojs.md5(cryptojs.enc.hex.parse(data.files[0].tostring())); // ee9cd5bf4272fc35bd57d184553bd25b cryptojs.md5(cryptojs.enc.utf8.parse(data.files[0].tostring())); // 709d1d31dc47636e4f5ccbfd07601c19 cryptojs.md5(cryptojs.enc.latin1.parse(data.files[0].tostring())); // 709d1d31dc47636e4f5ccbfd07601c19
i've checked both orginal file copied file downloaded s3 using bash md5
generated same hash both files:
4d51c9a4d3fd076489d6c96614ebce61 identical hash being generated ruby.
also checked hash using online hasher:
4d51c9a4d3fd076489d6c96614ebce61
you use cryptojs.enc.latin1
, in ruby don't define encoding digest::md5.file(file).to_s
. sure use same encoding.
try
digest::md5.hexdigest('foobar')
and
cryptojs.md5(cryptojs.enc.hex.parse('foobar'));
Comments
Post a Comment