Hash of a cell text in Google Spreadsheet

HDB picture HDB · Nov 3, 2011 · Viewed 45.3k times · Source

How can I compute a MD5 or SHA1 hash of text in a specific cell and set it to another cell in Google Spreadsheet?

Is there a formula like =ComputeMD5(A1) or =ComputeSHA1(A1)?

Or is it possible to write custom formula for this? How?

Answer

gabhubert picture gabhubert · Aug 8, 2012

Open Tools > Script Editor then paste the following code:

function MD5 (input) {
  var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, input);
  var txtHash = '';
  for (i = 0; i < rawHash.length; i++) {
    var hashVal = rawHash[i];
    if (hashVal < 0) {
      hashVal += 256;
    }
    if (hashVal.toString(16).length == 1) {
      txtHash += '0';
    }
    txtHash += hashVal.toString(16);
  }
  return txtHash;
}

Save the script after that and then use the MD5() function in your spreadsheet while referencing a cell.

This script is based on Utilities.computeDigest() function.