0
# Cryptographic Hashing
1
2
Keccak-256 hashing functions and Solidity-style packed encoding for data integrity and smart contract interaction. These functions provide cryptographic hashing capabilities essential for Ethereum development.
3
4
## Capabilities
5
6
### Keccak-256 Hashing
7
8
#### Basic Keccak-256
9
10
```typescript { .api }
11
/**
12
* Wrapper for keccak256 hashing
13
* @param data - Data to hash (Bytes, Numbers, string, or ReadonlyArray<number>)
14
* @returns Keccak-256 hash as hex string
15
*/
16
function keccak256Wrapper(data: Bytes | Numbers | string | ReadonlyArray<number>): string;
17
18
/**
19
* Alias for keccak256Wrapper
20
*/
21
const keccak256 = keccak256Wrapper;
22
```
23
24
#### SHA3 Functions
25
26
```typescript { .api }
27
/**
28
* Computes Keccak-256, returns undefined for empty data
29
* @param data - Bytes data to hash
30
* @returns Keccak-256 hash or undefined for empty input
31
*/
32
function sha3(data: Bytes): string | undefined;
33
34
/**
35
* Like sha3 but returns hash even for empty data
36
* @param data - Bytes data to hash
37
* @returns Keccak-256 hash string
38
*/
39
function sha3Raw(data: Bytes): string;
40
```
41
42
### Solidity-Style Hashing
43
44
#### Packed Encoding
45
46
```typescript { .api }
47
/**
48
* Processes arguments for Solidity-style packing
49
* @param arg - Input argument to process
50
* @returns Processed hex string
51
*/
52
function processSolidityEncodePackedArgs(arg: Sha3Input): string;
53
54
/**
55
* Encodes packed arguments to hexstring
56
* @param values - Arguments to encode and pack
57
* @returns Packed hex string
58
*/
59
function encodePacked(...values: Sha3Input[]): string;
60
```
61
62
#### Solidity SHA3
63
64
```typescript { .api }
65
/**
66
* Solidity-style hash with tight packing
67
* @param values - Values to hash with Solidity-style packing
68
* @returns Keccak-256 hash or undefined for empty input
69
*/
70
function soliditySha3(...values: Sha3Input[]): string | undefined;
71
72
/**
73
* Like soliditySha3 but returns hash even for empty data
74
* @param values - Typed objects to hash
75
* @returns Keccak-256 hash string
76
*/
77
function soliditySha3Raw(...values: TypedObject[] | TypedObjectAbbreviated[]): string;
78
```
79
80
### Storage Utilities
81
82
```typescript { .api }
83
/**
84
* Gets storage slot number for long strings
85
* @param mainSlotNumber - Main slot number (number or string)
86
* @returns Storage slot string or undefined
87
*/
88
function getStorageSlotNumForLongString(mainSlotNumber: number | string): string | undefined;
89
```
90
91
## Usage Examples
92
93
```typescript
94
import {
95
keccak256, sha3, soliditySha3, encodePacked
96
} from "web3-utils";
97
98
// Basic Keccak-256 hashing
99
const hash1 = keccak256("hello world");
100
// "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"
101
102
const hash2 = keccak256(new Uint8Array([1, 2, 3, 4]));
103
// Hash of byte array
104
105
// SHA3 with empty handling
106
const hash3 = sha3("data"); // Returns hash
107
const hash4 = sha3(""); // Returns undefined
108
const hash5 = sha3Raw(""); // Returns hash even for empty
109
110
// Solidity-style packed hashing
111
const packedHash = soliditySha3(
112
{ type: "uint256", value: "1234567890" },
113
{ type: "string", value: "Hello" },
114
{ type: "address", value: "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAeD" }
115
);
116
117
// Encoding packed arguments
118
const packed = encodePacked(
119
{ type: "uint256", value: "123" },
120
{ type: "string", value: "test" }
121
);
122
// Returns hex string of packed data
123
124
// Hash the packed data
125
const finalHash = keccak256(packed);
126
```
127
128
## Types
129
130
```typescript { .api }
131
// Input types for Solidity-style hashing
132
type Sha3Input =
133
| string
134
| number
135
| bigint
136
| boolean
137
| TypedObject
138
| TypedObjectAbbreviated;
139
140
interface TypedObject {
141
type: string;
142
value: string | number | boolean | bigint;
143
}
144
145
interface TypedObjectAbbreviated {
146
t: string; // Abbreviated form of 'type'
147
v: string | number | boolean | bigint; // Abbreviated form of 'value'
148
}
149
```
150
151
## Common Use Cases
152
153
### Smart Contract Event Signatures
154
155
```typescript
156
// Calculate event signature hash
157
const eventSignature = "Transfer(address,address,uint256)";
158
const eventHash = keccak256(eventSignature);
159
// Used for filtering events
160
161
// Function selector (first 4 bytes)
162
const functionSignature = "transfer(address,uint256)";
163
const selector = keccak256(functionSignature).slice(0, 10); // "0x" + 8 chars
164
```
165
166
### Merkle Tree Construction
167
168
```typescript
169
// Hash leaf nodes
170
const leaf1 = keccak256("data1");
171
const leaf2 = keccak256("data2");
172
173
// Hash internal nodes
174
const parent = keccak256(encodePacked(
175
{ type: "bytes32", value: leaf1 },
176
{ type: "bytes32", value: leaf2 }
177
));
178
```
179
180
### Storage Slot Calculation
181
182
```typescript
183
// Calculate storage slot for mapping
184
const key = "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAeD";
185
const slot = 0;
186
const storageSlot = keccak256(encodePacked(
187
{ type: "address", value: key },
188
{ type: "uint256", value: slot }
189
));
190
```