The Google App Script function computeDigest returns a byte array of the signature. How can I get the string representation of the digest?
I have already tried the bin2String() function.
function sign(){
var signature = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, "thisisteststring")
Logger.log(bin2String(signature));
}
function bin2String(array) {
var result = "";
for (var i = 0; i < array.length; i++) {
result += String.fromCharCode(parseInt(array[i], 2));
}
return result;
}
but it puts "" in the Logs
Just in case this is helpful to anyone else, I've put together a more succinct version of Mogsdad's solution:
Here is an easy way to transform a Byte[] into a String.
Found this in the documentation provided by Google here : https://developers.google.com/apps-script/reference/utilities/utilities#base64Decode(String)
Better late than never. (And since this topic still comes first when searching this topic in Goole, this might help some folks).
Did somebody say succinct? (/fulldecent arrives to party with the drinking hat, including straws, after everyone else already passed out)
From this post:
If we put
Logger.log(signature);
right after the call tocomputeDigest()
, we get:As represented in javascript, the digest includes both positive and negative integers, so we can't simply treat them as ascii characters. The MD5 algorithm, however, should provide us with 8-bit values, in the range 0x00 to 0xFF (255). Those negative values, then, are just a misinterpretation of the high-order bit; taking it to be a sign bit. To correct, we need to add 256 to any negative value.
How to convert decimal to hex in JavaScript? gives us this for retrieving hex characters:
Putting that together, here's your
sign()
function, which is also available as a gist:And here's what the logs contain:
Let's see what we get from this on-line MD5 Hash Generator:
I tried it with a few other strings, and they consistently matched the result from the on-line generator.
One-liner: