> For the complete documentation index, see [llms.txt](https://www.0x1.academy/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.0x1.academy/qu-kuai-lian-ru-he-da-cheng-gong-shi/wa/ha-xi-zhi.md).

# 哈希值

### 哈希值（Hash Value）是透過雜湊函數將任意資料轉換成的固定長度數值。它就像資料的「數位指紋」——每份資料都有獨一無二的哈希值，即使只改動一個字，哈希值也會完全不同。

***

## 什麼是哈希值？

想像你把一本 500 頁的小說丟進一台神奇的機器，它吐出一串 64 個字的「密碼」。這串密碼就是哈希值。

| 輸入      | SHA-256 輸出                          |
| ------- | ----------------------------------- |
| "Hello" | 185f8db32271fe25f561a6fc938b2e26... |
| "hello" | 2cf24dba5fb0a30e26e83b2ac5b9e29e... |
| 整本小說    | 還是 64 個字                            |

{% hint style="info" %}
**神奇的特性**

1. **固定長度**：不管輸入多長，輸出永遠一樣長
2. **單向性**：無法從哈希值反推原始資料
3. **雪崩效應**：改一個字，輸出完全改變
4. **唯一性**：不同輸入幾乎不可能產生相同輸出
   {% endhint %}

***

## 哈希值在區塊鏈中的應用

### 1. 區塊連結

每個區塊都包含前一個區塊的哈希值。如果有人竄改歷史區塊，哈希值就會改變，導致整條鏈「斷裂」。

```
區塊 1 ──→ 區塊 2 ──→ 區塊 3
 Hash A    (包含 Hash A)    (包含 Hash B)
```

### 2. 挖礦

礦工要找到一個特殊的輸入，讓產生的哈希值符合條件（例如開頭有一定數量的零）。

### 3. 驗證交易

交易的哈希值可以快速驗證交易內容是否被竄改。

### 4. 錢包地址

錢包地址就是公鑰經過哈希運算後的結果。

***

## 常見的哈希算法

| 算法         | 輸出長度    | 使用場景  |
| ---------- | ------- | ----- |
| SHA-256    | 256 bit | 比特幣   |
| Keccak-256 | 256 bit | 以太坊   |
| RIPEMD-160 | 160 bit | 比特幣地址 |

***

## 延伸閱讀

* [算力](/qu-kuai-lian-ru-he-da-cheng-gong-shi/wa/suan-li.md) — 每秒產生多少哈希值
* [哈希力](/qu-kuai-lian-ru-he-da-cheng-gong-shi/wa/ha-xi-li.md) — 算力的另一種說法
* [哈希算法](/gei-kai-fa-zhe-de-zhi-nan/cryptography/hash.md) — 密碼學基礎
* [SHA-256](/gei-kai-fa-zhe-de-zhi-nan/cryptography/sha256.md) — 比特幣使用的算法

***

#### 參考資料

* [Cryptographic Hash Function - Wikipedia](https://en.wikipedia.org/wiki/Cryptographic_hash_function)
