Nodejs Keccak256

affaan-m/ECC/skills/nodejs-keccak256

作者 affaan-mef648e01899ba3e8dc6371642deaaf64b4477775无许可证275K 个星标收录于 2026年10月9日更新于 2026年10月9日仓库4天前更新

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.

AI 生成的概览

指导 JavaScript 和 TypeScript 开发者在以太坊哈希中使用 Keccak-256,而非 Node 的 NIST SHA3。

功能
该技能说明以太坊使用 Keccak-256,而 Node 的 crypto.createHash('sha3-256') 实现的是 NIST SHA3 变体,两者对相同输入会产生不同输出且不会有警告。它提供使用 ethers、viem 和 web3.js 计算哈希、函数选择器、事件主题、EIP-712 类型哈希、存储槽和地址的代码示例。它还给出用于审查代码库中错误 sha3 用法的 grep 命令。
适用场景
适用于在 JavaScript 或 TypeScript 中为以太坊数据做哈希时,例如计算函数选择器、事件主题、签名、Merkle 树或存储槽。也适用于审查直接用 Node crypto 哈希以太坊数据的代码,或当选择器、签名、存储槽或派生地址出错时。
运行要求
不包含脚本,仅为说明和代码示例。按示例操作需要 JavaScript 或 TypeScript 项目,并安装支持 Keccak 的库,如 ethers、viem 或 web3.js;可选使用 Node 的 crypto 模块进行对比。

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.

javascript
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

typescript
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

typescript
import { keccak256, toBytes } from 'viem';
const hash = keccak256(toBytes('hello'));

web3.js

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

Common patterns

typescript
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

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

Audit your codebase

bash
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.

来源与署名

来源:affaan-m/ECC位于skills/nodejs-keccak256提交ef648e0

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架