Hash String SHA-256

Cache keys and dedupe fingerprints should not log raw emails or IDs. hashStringSha256(text) runs Utilities.computeDigest(SHA_256, …, UTF_8) and returns a lowercase hex string you can safely stash in Properties, CacheService, or a Log tab.

Null/empty choice: null, undefined, and '' all hash as the empty string — so you get one consistent hex for “no input,” not three different shapes.

What you'll need

  • Apps Script with Utilities.computeDigest
  • A string you want to fingerprint (email, external id, payload slice)
  • A place to store the hex (cache key, column, Script Property)

How to use this snippet

/** * SHA-256 hex digest of text (UTF-8). null/undefined/'' → hash of empty string. * @param {string} text * @return {string} 64-char lowercase hex */ function hashStringSha256(text) { var input = text == null ? '' : String(text); var bytes = Utilities.computeDigest( Utilities.DigestAlgorithm.SHA_256, input, Utilities.Charset.UTF_8 ); return bytes .map(function (b) { var v = b < 0 ? b + 256 : b; var hex = v.toString(16); return hex.length === 1 ? '0' + hex : hex; }) .join(''); }

Example

function cacheKeyForLead_(email) { return 'lead:' + hashStringSha256(String(email || '').trim().toLowerCase()); } function alreadyProcessed_(externalId) { var key = 'seen:' + hashStringSha256(externalId); var cache = CacheService.getScriptCache(); if (cache.get(key)) return true; cache.put(key, '1', 21600); // 6 hours return false; }

Tips:

  • Normalize before hashing (trim, lower-case email) or you’ll mistreat Ada@x.com vs ada@x.com as different keys.
  • Hex is fine for keys; don’t treat SHA-256 as encryption — it’s a one-way fingerprint.
  • Prefer hashing PII over writing it into shared Log sheets.
  • Empty input is intentional: callers can detect text == null before hashing if “missing” should skip work entirely.

Tip: NitroGAS Co-Pilot can wire hashStringSha256 into your CacheService / dedupe helpers so logs stay useful without dumping customer emails.

Happy Coding!

NitroGAS Chrome Extension

Want this snippet handy inside the Apps Script editor? NitroGAS gives you free themes and snippets — plus optional Co-Pilot when you want a boost (1-week, 1-month, or yearly passes — no subscription). Happy Coding!

Get the Extension