GP SoftwareTwitter
Opus FAQsManualCommandsObjects

FSUtil.Hash calculates different SHA-1 hash for string?


#1

Example case:

var b = DOpus.Create.Blob();
var string = 'Lets try this string';
b.copyFrom(string);
DOpus.output(new ActiveXObject("WScript.Shell").Exec('perl -mDigest::SHA1 -e "print Digest::SHA1::sha1_hex(\'' + string + '\')').StdOut.readAll()); // correct hash
DOpus.output(DOpus.FSUtil().Hash(b, 'sha1')); // incorrect

Output:

516b09f4441bc72dda70cd88c57a7a5945423176
99d5a7c2b21ba2a3440f4f95db181e5cde4e27ac

(I can confirm that perl calculates the correct hash -- other sources can as well)


#2

You're probably hashing different representations of the string, e.g. UTF-16 vs UTF-8.

StringTools provides ways to convert between encodings.

Also, from the 12.9.1 release notes:

The Blob.CopyFrom method now lets you initialise a Blob from a string. By default the Blob will be set to the Unicode form of the string; if you pass "utf8" as the second parameter it will initialise the Blob with the utf8-encoded form of the string.


#3

Yep Leo, you are right.

var b = DOpus.Create.Blob();
var string = 'Lets try this string';
b.copyFrom(string,'utf8');
DOpus.output(new ActiveXObject("WScript.Shell").Exec('perl -mDigest::SHA1 -e "print Digest::SHA1::sha1_hex(\'' + string + '\')').StdOut.readAll()); // correct hash
DOpus.output(DOpus.FSUtil().Hash(b, 'sha1')); // correct now
516b09f4441bc72dda70cd88c57a7a5945423176
516b09f4441bc72dda70cd88c57a7a5945423176