Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
543 views
in Technique[技术] by (71.8m points)

javascript - get back a string representation from computeDigest(algorithm, value) byte[]

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If we put Logger.log(signature); right after the call to computeDigest(), we get:

[8, 30, -43, 124, -101, 114, -37, 10, 78, -13, -102, 51, 65, -24, -83, 81]

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:

hexString = yourNumber.toString(16);

Putting that together, here's your sign() function, which is also available as a gist:

function sign(message){     
  message = message || "thisisteststring";
  var signature = Utilities.computeDigest(
                       Utilities.DigestAlgorithm.MD5,
                       message,
                       Utilities.Charset.US_ASCII);
  Logger.log(signature);
  var signatureStr = '';
    for (i = 0; i < signature.length; i++) {
      var byte = signature[i];
      if (byte < 0)
        byte += 256;
      var byteStr = byte.toString(16);
      // Ensure we have 2 chars in our byte, pad with 0
      if (byteStr.length == 1) byteStr = '0'+byteStr;
      signatureStr += byteStr;
    }   
  Logger.log(signatureStr);
  return signatureStr;
}

And here's what the 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 what we get from this on-line MD5 Hash Generator:

081ed57c9b72db0a4ef39a3341e8ad51

I tried it with a few other strings, and they consistently matched the result from the on-line generator.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...