58 lines
1.2 KiB
Markdown
58 lines
1.2 KiB
Markdown
---
|
||
description: Calculates the Sha256 hash of a byte array.
|
||
---
|
||
|
||
# sha256
|
||
|
||
Calculates the [Sha256](https://en.wikipedia.org/wiki/SHA-256) hash of a byte array or hex value.
|
||
|
||
This function is a re-export of `sha256` from [`@noble/hashes`](https://github.com/paulmillr/noble-hashes) – an audited & minimal JS hashing library.
|
||
|
||
## Install
|
||
|
||
```ts
|
||
import { sha256 } from 'viem'
|
||
```
|
||
|
||
## Usage
|
||
|
||
```ts
|
||
import { sha256 } from 'viem'
|
||
|
||
sha256(new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
|
||
// 0x7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069
|
||
|
||
sha256('0xdeadbeef')
|
||
// 0x5f78c33274e43fa9de5659265c1d917e25c03722dcb0b8d27db8d5feaa813953
|
||
```
|
||
|
||
## Returns
|
||
|
||
`Hex | ByteArray`
|
||
|
||
The hashed value.
|
||
|
||
## Parameters
|
||
|
||
### value
|
||
|
||
- **Type:** `Hex | ByteArray`
|
||
|
||
The hex value or byte array to hash.
|
||
|
||
### to
|
||
|
||
- **Type:** `"bytes" | "hex"`
|
||
- **Default:** `"hex"`
|
||
|
||
The output type.
|
||
|
||
```ts
|
||
import { sha256 } from 'viem'
|
||
|
||
sha256(
|
||
new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33],
|
||
'bytes' // [!code focus]
|
||
)
|
||
// Uint8Array [95, 120, 195, 50, 116, 228, 63, 169, 222, 86, 89, 38, 92, 29, 145, 126, 37, 192, 55, 34, 220, 176, 184, 210, 125, 184, 213, 254, 170, 129, 57, 83] // [!code focus]
|
||
``` |