Skip to content
Skillpedia
General coding280k · #242

nodejs-keccak256

Makes your agent avoid Ethereum hashing errors when using JavaScript. It's important for ensuring security calculations are correct.

What it does

This tool ensures you are using the correct 'hashing' method (a function that turns data into a unique character sequence) for Ethereum in your JavaScript and TypeScript code. Using the wrong function can lead to errors in transactions and other critical operations.

When to use it

  • ✓When writing code to interact with Ethereum.
  • ✓When you need to generate unique identifiers for functions or events.
  • ✓If you suspect your Ethereum code is experiencing unexpected errors.

Example of what to ask your AI

“How can I ensure I'm correctly calculating a message's 'hash' for Ethereum using JavaScript?”

Want more? Open the sections below.

How to use

The easiest way: copy the message below and paste it into Claude, ChatGPT, Cursor or any AI. It reads the skill and follows the instructions.

Skill link

https://github.com/affaan-m/ECC/blob/main/skills/nodejs-keccak256/SKILL.md

Ready-to-paste message

Use this skill to help me: https://github.com/affaan-m/ECC/blob/main/skills/nodejs-keccak256/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
How it works

It works by replacing Node.js's default hashing function, which differs from Ethereum's, with one that follows the correct Keccak-256 standard. This prevents your code from producing incorrect results without warning.

Installation

Easiest way: copy the skill link and paste it into Claude, ChatGPT or Cursor asking it to follow the instructions.

In the terminal (Claude Code, Codex, Cursor), in your project folder:

npx skills add affaan-m/ECC --skill nodejs-keccak256

Or download just the file:

curl -L https://raw.githubusercontent.com/affaan-m/ECC/main/skills/nodejs-keccak256/SKILL.md -o SKILL.md
Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

Node.js Keccak-256

Ethereum uses Keccak-256, not the NIST-standardized SHA3 variant exposed by Node's crypto.createHash('sha3-256').

When to Use

  • Computing Ethereum function selectors or event topics
  • Building EIP-712, signature, Merkle, or storage-slot helpers in JS/TS
  • Reviewing any code that hashes Ethereum data with Node crypto directly

How It Works

The two algorithms produce different outputs for the same input, and Node will not warn you.

import crypto from 'crypto';
import { keccak256, toUtf8Bytes } from 'ethers';

const data = 'hello';
const nistSha3 = crypto.createHash('sha3-256').update(data).digest('hex');
const keccak = keccak256(toUtf8Bytes(data)).slice(2);

console.log(nistSha3 === keccak); // false

Examples

ethers v6

import { keccak256, toUtf8Bytes, solidityPackedKeccak256, id } from 'ethers';

const hash = keccak256(new Uint8Array([0x01, 0x02]));
const hash2 = keccak256(toUtf8Bytes('hello'));
const topic = id('Transfer(address,address,uint256)');
const packed = solidityPackedKeccak256(
  ['address', 'uint256'],
  ['0x742d35Cc6634C0532925a3b8D4C9B569890FaC1c', 100n],
);

viem

import { keccak256, toBytes } from 'viem';

const hash = keccak256(toBytes('hello'));

web3.js

const hash = web3.utils.keccak256('hello');
const packed = web3.utils.soliditySha3(
  { type: 'address', value: '0x742d35Cc6634C0532925a3b8D4C9B569890FaC1c' },
  { type: 'uint256', value: '100' },
);

Common patterns

import { id, keccak256, AbiCoder } from 'ethers';

const selector = id('transfer(address,uint256)').slice(0, 10);
const typeHash = keccak256(toUtf8Bytes('Transfer(address from,address to,uint256 value)'));

function getMappingSlot(key: string, mappingSlot: number): string {
  return keccak256(
    AbiCoder.defaultAbiCoder().encode(['address', 'uint256'], [key, mappingSlot]),
  );
}

Address from public key

import { keccak256 } from 'ethers';

function pubkeyToAddress(pubkeyBytes: Uint8Array): string {
  const hash = keccak256(pubkeyBytes.slice(1));
  return '0x' + hash.slice(-40);
}

Audit your codebase

grep -rn "createHash.*sha3" --include="*.ts" --include="*.js" --exclude-dir=node_modules .
grep -rn "keccak256" --include="*.ts" --include="*.js" . | grep -v node_modules

Rule

For Ethereum contexts, never use crypto.createHash('sha3-256'). Use Keccak-aware helpers from ethers, viem, web3, or another explicit Keccak implementation.

FAQ

Why is Node.js's hash different from Ethereum's Keccak-256?

Node.js uses a standard called SHA3, while Ethereum uses a slightly different standard called Keccak-256. Although they look similar, they produce different outputs for the same data.

What happens if I use the wrong hash?

Your code might fail when interacting with the Ethereum network, such as sending transactions, signing messages, or verifying data. This can lead to hard-to-trace errors.

Do I need to understand how Keccak-256 works internally?

No, you don't need to understand the technical details. This tool handles that for you, allowing you to use the correct function without worrying about the complexity.

Details

Original description by the author

Prevent Ethereum hashing bugs in JavaScript and TypeScript. Node's sha3-256 is NIST SHA3, not Ethereum Keccak-256, and silently breaks selectors, signatures, storage slots, and address derivation. Use when hashing for Ethereum in JavaScript or TypeScript, or when a selector, signature, storage slot, or derived address is wrong.

repo: affaan-m/ECC

Popularity

GitHub stars
280k
Ranking position
#242
License
MIT
Updated
10/15/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills