A
A
Ayan Bai2015-02-19 22:04:22
JavaScript
Ayan Bai, 2015-02-19 22:04:22

How to generate md5 string with Google Script?

I need to assemble a string using Google Script tools by concatenating the parameters with the addition of the md5, sha1 or sha256 secret key at the end.
I do the following:
var md5String = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, name+'3350'+service+bookitem+id+date+'byc2iycvbdn1fvbtghxqm9');
And I get the following result:
Logger.log(md5String ) // -51, -106, 88, -121, -103, 78, -8, -65, 76, -51, 124, -91, -76, -60 , 99, -118
Why is this happening?
In theory, I should have received a string like this gf32h03j927t3rgb2rcgxhwx207fcw
and instead I get an array of numbers.
I make my hash using the service ( www.md5.cz ) it works, but it doesn't work through Google tools.
Tell me pliz

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Ivanov, 2015-02-20
@wolf47

Hello.
It seems to me that you have an incomplete understanding of Google Apps Script methods, hashing algorithms and number systems.
First, computeDigest returns a Byte[] array, which is what it should do.
Secondly, you just need to "convert it to a hexadecimal hash string" as mentioned above.
Try doing this:

function test(){
  Logger.log(toMD5('56'));
}
const toMD5 = function(charset, toByte) {
  charset = charset || Utilities.Charset.UTF_8;
  var digest = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, this, charset);
  if(toByte) return digest;
  var __ = '';
  for (i = 0; i < digest.length; i++) {
    var byte = digest[i];
    if (byte < 0) byte += 256;
    var bStr = byte.toString(16);
    if (bStr.length == 1) bStr = '0' + bStr;
    __ += bStr;
  }
  return __;
}

K
KorsaR-ZN, 2015-02-19
@KorsaR-ZN

Because it returns a byte array.
It is necessary to convert it into a hexadecimal hash string in a cycle.
MD5 is a sequence of hexadecimal digits, so there may be characters 0-9a-f , not what you wrote in the example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question