Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 966
Saqib Ali Avatar asked Apr 25 '13 14:04

Saqib Ali


1 Answers

Just in case this is helpful to anyone else, I've put together a more succinct version of Mogsdad's solution:

function md5(str) {
  return Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, str).reduce(function(str,chr){
    chr = (chr < 0 ? chr + 256 : chr).toString(16);
    return str + (chr.length==1?'0':'') + chr;
  },'');
}
like image 95
mikegreiling Avatar answered Sep 28 '22 08:09

mikegreiling