or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

bip39.mded25519.mdhashing.mdindex.mdkey-derivation.mdsecp256k1.mdsr25519.mdvrf.md

hashing.mddocs/

0

# Cryptographic Hashing Functions

1

2

Various cryptographic hash functions including SHA variants, BLAKE2b, Keccak, HMAC, and the Two-X hash function optimized for Polkadot applications.

3

4

## Capabilities

5

6

### BLAKE2b Hash

7

8

BLAKE2b cryptographic hash function with support for keyed hashing and configurable output size.

9

10

```typescript { .api }

11

/**

12

* Computes BLAKE2b hash

13

* @param data - Data to hash as Uint8Array

14

* @param key - Optional key for keyed hashing as Uint8Array (use empty array for keyless)

15

* @param size - Output size in bytes (1-64)

16

* @returns Hash as Uint8Array of specified size

17

*/

18

function blake2b(data: Uint8Array, key: Uint8Array, size: number): Uint8Array;

19

```

20

21

**Usage Example:**

22

23

```typescript

24

import { waitReady, blake2b } from "@polkadot/wasm-crypto";

25

26

await waitReady();

27

28

const data = new TextEncoder().encode("Hello, BLAKE2b!");

29

30

// Keyless hashing with different output sizes

31

const hash32 = blake2b(data, new Uint8Array(0), 32);

32

const hash64 = blake2b(data, new Uint8Array(0), 64);

33

34

// Keyed hashing

35

const key = new Uint8Array(32).fill(1);

36

const keyedHash = blake2b(data, key, 32);

37

38

console.log("32-byte hash length:", hash32.length); // 32

39

console.log("64-byte hash length:", hash64.length); // 64

40

console.log("Keyed hash length:", keyedHash.length); // 32

41

```

42

43

### SHA-256 Hash

44

45

Standard SHA-256 cryptographic hash function.

46

47

```typescript { .api }

48

/**

49

* Computes SHA-256 hash

50

* @param data - Data to hash as Uint8Array

51

* @returns 32-byte hash as Uint8Array

52

*/

53

function sha256(data: Uint8Array): Uint8Array;

54

```

55

56

**Usage Example:**

57

58

```typescript

59

import { waitReady, sha256 } from "@polkadot/wasm-crypto";

60

61

await waitReady();

62

63

const data = new TextEncoder().encode("Hello, SHA-256!");

64

const hash = sha256(data);

65

66

console.log("SHA-256 hash length:", hash.length); // 32

67

```

68

69

### SHA-512 Hash

70

71

Standard SHA-512 cryptographic hash function.

72

73

```typescript { .api }

74

/**

75

* Computes SHA-512 hash

76

* @param data - Data to hash as Uint8Array

77

* @returns 64-byte hash as Uint8Array

78

*/

79

function sha512(data: Uint8Array): Uint8Array;

80

```

81

82

**Usage Example:**

83

84

```typescript

85

import { waitReady, sha512 } from "@polkadot/wasm-crypto";

86

87

await waitReady();

88

89

const data = new TextEncoder().encode("Hello, SHA-512!");

90

const hash = sha512(data);

91

92

console.log("SHA-512 hash length:", hash.length); // 64

93

```

94

95

### Keccak-256 Hash

96

97

Keccak-256 hash function (different from SHA3-256).

98

99

```typescript { .api }

100

/**

101

* Computes Keccak-256 hash

102

* @param data - Data to hash as Uint8Array

103

* @returns 32-byte hash as Uint8Array

104

*/

105

function keccak256(data: Uint8Array): Uint8Array;

106

```

107

108

**Usage Example:**

109

110

```typescript

111

import { waitReady, keccak256 } from "@polkadot/wasm-crypto";

112

113

await waitReady();

114

115

const data = new TextEncoder().encode("Hello, Keccak-256!");

116

const hash = keccak256(data);

117

118

console.log("Keccak-256 hash length:", hash.length); // 32

119

```

120

121

### Keccak-512 Hash

122

123

Keccak-512 hash function.

124

125

```typescript { .api }

126

/**

127

* Computes Keccak-512 hash

128

* @param data - Data to hash as Uint8Array

129

* @returns 64-byte hash as Uint8Array

130

*/

131

function keccak512(data: Uint8Array): Uint8Array;

132

```

133

134

**Usage Example:**

135

136

```typescript

137

import { waitReady, keccak512 } from "@polkadot/wasm-crypto";

138

139

await waitReady();

140

141

const data = new TextEncoder().encode("Hello, Keccak-512!");

142

const hash = keccak512(data);

143

144

console.log("Keccak-512 hash length:", hash.length); // 64

145

```

146

147

### HMAC-SHA256

148

149

Hash-based Message Authentication Code using SHA-256.

150

151

```typescript { .api }

152

/**

153

* Computes HMAC-SHA256

154

* @param key - Secret key as Uint8Array

155

* @param data - Data to authenticate as Uint8Array

156

* @returns 32-byte HMAC as Uint8Array

157

*/

158

function hmacSha256(key: Uint8Array, data: Uint8Array): Uint8Array;

159

```

160

161

**Usage Example:**

162

163

```typescript

164

import { waitReady, hmacSha256 } from "@polkadot/wasm-crypto";

165

166

await waitReady();

167

168

const key = new TextEncoder().encode("secret-key");

169

const data = new TextEncoder().encode("Hello, HMAC-SHA256!");

170

const hmac = hmacSha256(key, data);

171

172

console.log("HMAC-SHA256 length:", hmac.length); // 32

173

```

174

175

### HMAC-SHA512

176

177

Hash-based Message Authentication Code using SHA-512.

178

179

```typescript { .api }

180

/**

181

* Computes HMAC-SHA512

182

* @param key - Secret key as Uint8Array

183

* @param data - Data to authenticate as Uint8Array

184

* @returns 64-byte HMAC as Uint8Array

185

*/

186

function hmacSha512(key: Uint8Array, data: Uint8Array): Uint8Array;

187

```

188

189

**Usage Example:**

190

191

```typescript

192

import { waitReady, hmacSha512 } from "@polkadot/wasm-crypto";

193

194

await waitReady();

195

196

const key = new TextEncoder().encode("secret-key");

197

const data = new TextEncoder().encode("Hello, HMAC-SHA512!");

198

const hmac = hmacSha512(key, data);

199

200

console.log("HMAC-SHA512 length:", hmac.length); // 64

201

```

202

203

### Two-X Hash

204

205

Two-X hash function optimized for Polkadot applications with configurable rounds.

206

207

```typescript { .api }

208

/**

209

* Computes Two-X hash

210

* @param data - Data to hash as Uint8Array

211

* @param rounds - Number of rounds (typically 2, 4, or 8)

212

* @returns Hash as Uint8Array (length depends on rounds: rounds * 8 bytes)

213

*/

214

function twox(data: Uint8Array, rounds: number): Uint8Array;

215

```

216

217

**Usage Example:**

218

219

```typescript

220

import { waitReady, twox } from "@polkadot/wasm-crypto";

221

222

await waitReady();

223

224

const data = new TextEncoder().encode("Hello, Two-X!");

225

226

// Different round configurations

227

const twox64 = twox(data, 1); // 8 bytes

228

const twox128 = twox(data, 2); // 16 bytes

229

const twox256 = twox(data, 4); // 32 bytes

230

231

console.log("Two-X 64 length:", twox64.length); // 8

232

console.log("Two-X 128 length:", twox128.length); // 16

233

console.log("Two-X 256 length:", twox256.length); // 32

234

```

235

236

## Hash Function Comparison Example

237

238

```typescript

239

import {

240

waitReady,

241

sha256,

242

sha512,

243

blake2b,

244

keccak256,

245

keccak512,

246

twox

247

} from "@polkadot/wasm-crypto";

248

249

async function compareHashFunctions() {

250

await waitReady();

251

252

const data = new TextEncoder().encode("Compare hash functions");

253

254

// Compute various hashes

255

const sha256Hash = sha256(data);

256

const sha512Hash = sha512(data);

257

const blake2bHash = blake2b(data, new Uint8Array(0), 32);

258

const keccak256Hash = keccak256(data);

259

const keccak512Hash = keccak512(data);

260

const twoxHash = twox(data, 4); // 32 bytes

261

262

console.log("Hash comparison for same input:");

263

console.log("SHA-256: ", Array.from(sha256Hash.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join(''));

264

console.log("SHA-512: ", Array.from(sha512Hash.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join(''));

265

console.log("BLAKE2b: ", Array.from(blake2bHash.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join(''));

266

console.log("Keccak-256:", Array.from(keccak256Hash.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join(''));

267

console.log("Keccak-512:", Array.from(keccak512Hash.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join(''));

268

console.log("Two-X: ", Array.from(twoxHash.slice(0, 8)).map(b => b.toString(16).padStart(2, '0')).join(''));

269

270

return {

271

sha256Hash,

272

sha512Hash,

273

blake2bHash,

274

keccak256Hash,

275

keccak512Hash,

276

twoxHash

277

};

278

}

279

280

compareHashFunctions();

281

```