javascript - get back a string representation from computeDigest(algorithm, value) byte[] -
the google app script function computedigest returns byte array of signature. how can string representation of digest?
i have tried bin2string() function.
function sign(){ var signature = utilities.computedigest(utilities.digestalgorithm.md5, "thisisteststring") logger.log(bin2string(signature)); } function bin2string(array) { var result = ""; (var = 0; < array.length; i++) { result += string.fromcharcode(parseint(array[i], 2)); } return result; }
but puts "" in logs
if put logger.log(signature);
right after call computedigest()
, get:
[8, 30, -43, 124, -101, 114, -37, 10, 78, -13, -102, 51, 65, -24, -83, 81]
as represented in javascript, digest includes both positive , negative integers, can't treat them ascii characters. md5 algorithm, however, should provide 8-bit values, in range 0x00 0xff (255). negative values, then, misinterpretation of high-order bit; taking sign bit. correct, need add 256 negative value.
how convert decimal hex in javascript? gives retrieving hex characters:
hexstring = yournumber.tostring(16);
putting together, here's sign()
function, available gist:
function sign(message){ message = message || "thisisteststring"; var signature = utilities.computedigest( utilities.digestalgorithm.md5, message, utilities.charset.us_ascii); logger.log(signature); var signaturestr = ''; (i = 0; < signature.length; i++) { var byte = signature[i]; if (byte < 0) byte += 256; var bytestr = byte.tostring(16); // ensure have 2 chars in our byte, pad 0 if (bytestr.length == 1) bytestr = '0'+bytestr; signaturestr += bytestr; } logger.log(signaturestr); return signaturestr; }
and here's logs contain:
[13-04-25 21:46:55:787 edt] [8, 30, -43, 124, -101, 114, -37, 10, 78, -13, -102, 51, 65, -24, -83, 81] [13-04-25 21:46:55:788 edt] 081ed57c9b72db0a4ef39a3341e8ad51
let's see this on-line md5 hash generator:
i tried few other strings, , consistently matched result on-line generator.