All Recipes
StarVerified Developer Solution • 100% Offline
Hash Generator (MD5, SHA256, SHA512)

Fix "TypeError: Data must be a string or a buffer"

Resolve hashing crashes when passing objects or numbers into crypto update functions.

The Problem (Error Root Cause)Exception

When calling `hash.update(data)`, you receive a TypeError. This occurs because hashing algorithms only operate on raw bytes (strings or buffers), but you passed an object, array, or number.

Identified via runtime validation & stack traces
The Solution (Step-by-Step Fix)Verified

Stringify the object or cast the number to a string before hashing it.

Deterministic, non-destructive resolution

Code Standard: Bad Pattern vs Verified Fix

Live Syntax
Anti-Pattern vs Verified Fix
1// ❌ Bad: Passing an object
2const data = { id: 1 };
3crypto.createHash('sha256').update(data);
4
5// ✅ Good: Stringify first
6const data = { id: 1 };
7crypto.createHash('sha256').update(JSON.stringify(data));

Test and resolve this using Hash Generator (MD5, SHA256, SHA512)

Execute directly in your browser memory. Zero API keys, zero network tracking, completely client-side.

Launch Hash Generator (MD5, SHA256, SHA512)

Frequently Asked Questions

Q:Will JSON.stringify produce the same hash every time?

Only if the key order is strictly identical. For deterministic hashing of objects, use a library like json-stable-stringify.

Q:Where can I quickly test hashes?

Our Hash Generator allows you to paste raw text and get instant cryptographic outputs.

Related Troubleshooting Guides

View Directory