How to Use Hash Functions in Google Sheets: SHA256, MD5, and Beyond

How to Use Hash Functions in Google Sheets: SHA256, MD5, and Beyond
Photo by SumUp / Unsplash

Hashing is a common technique used in software development to generate unique identifiers, verify integrity, or anonymize data. If you're working inside Google Sheets, you might wonder: can I create a hash like SHA256 or MD5 directly in a spreadsheet? The short answer is: not natively, but with a small script, it becomes very easy.

This guide will walk you through how to implement and use hash functions in Google Sheets, including SHA256, MD5, SHA512, and others. We'll also cover some real-world use cases, and things to watch out for when using hashes in a spreadsheet environment.


🧠 What Is a Hash Function?

A hash function takes some input (a string, number, etc.) and returns a fixed-size string of characters, which usually appears random. For example:

Input: "hello"
SHA256: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

Hash functions are used for:

  • Generating unique keys from input data
  • Anonymizing personal information (e.g., emails)
  • Verifying data integrity (i.e., checking if something changed)
  • Password hashing (note: Google Sheets is not ideal for secure password storage)

⚠️ What Google Sheets Offers by Default

Google Sheets does not offer any built-in formula like =SHA256() or =MD5(). You can't hash data directly using normal spreadsheet functions like =SUM() or =CONCAT().

But Google Sheets is built on top of Google Apps Script, which does allow hashing via the Utilities.computeDigest() function.


🛠️ How to Add Hashing to Google Sheets

Here's how to create your own custom hashing function in less than 1 minute:

Step 1: Open Apps Script

  1. In your Google Sheet, click Extensions → Apps Script.
  2. Remove any existing code and paste the following:
function hash(input, algorithm) {
  algorithm = algorithm || Utilities.DigestAlgorithm.SHA_256;
  const bytes = Utilities.computeDigest(algorithm, input);
  return bytes.map(b => ('0' + (b & 0xFF).toString(16)).slice(-2)).join('');
}

Optional: Add aliases if you want simpler functions:

function SHA256(input) {
  return hash(input, Utilities.DigestAlgorithm.SHA_256);
}

function SHA512(input) {
  return hash(input, Utilities.DigestAlgorithm.SHA_512);
}

function MD5(input) {
  return hash(input, Utilities.DigestAlgorithm.MD5);
}

Step 2: Save the Project

  • Name the project HashFunctions or anything you like.
  • Click File → Save and close the editor.

🧪 How to Use It in Sheets

You can now use your new hash function just like a built-in formula:

=SHA256("Hello world")

Or if you want to hash the value from a cell:

=SHA256(A2)

Or use the generic function with algorithm parameter:

=hash(A2, "SHA_512")
=hash(A2, "MD5")

🔍 Supported Hash Algorithms

Here are the available options via Utilities.DigestAlgorithm:

Algorithm Constant String Notes
SHA-256 "SHA_256" Strong, widely used, ideal for unique keys
SHA-512 "SHA_512" Stronger, longer hash
SHA-384 "SHA_384" Less common, still secure
SHA-1 "SHA_1" Deprecated for security purposes
MD5 "MD5" Fast but insecure, avoid for sensitive data

🎯 Use Case Ideas

Here are some examples of where hashing in Sheets can be useful:

  • Detecting changes to data (hash every row and see if hash changes)

Masking email addresses in shared documents:

=SHA256(LOWER(A2))

Creating a unique row key from multiple columns:

=SHA256(A2 & B2 & C2)

⚠️ Considerations and Limitations

Before using hash functions extensively in Sheets, keep the following in mind:

✅ Pros:

  • Easy to implement and use
  • Works on any input (text, number, combined fields)
  • Makes spreadsheet data cleaner and easier to identify duplicates

❌ Cons:

  • No reverse: hash functions are one-way (you cannot “unhash”)
  • Not secure for storing passwords or secret data
  • Quota limits: Google Apps Script has daily execution quotas
  • Performance: Hashing a large sheet (thousands of rows) may slow down or hit limits
  • Case-sensitive: SHA256("abc")SHA256("ABC"). Use LOWER() if needed.

🧩 Tips & Best Practices

  • Use shorter hashes (e.g., LEFT(SHA256(A2), 8)) if you only need unique keys, not full cryptographic hashes.

Combine multiple cells carefully:

=SHA256(TEXT(A2, "yyyy-mm-dd") & "|" & B2 & "|" & C2)

Hash with salt (add extra text) if needed:

=SHA256(A2 & "-salt")

Use LOWER() or UPPER() for consistency:

=SHA256(LOWER(A2))

✅ Finally

While Google Sheets doesn’t have built-in hash functions like =SHA256(), it’s simple and effective to create your own using Google Apps Script. Whether you’re building a mini database, anonymizing user info, or ensuring data integrity, hashing can be a powerful tool—as long as you understand the limitations.

Support Us