iT邦幫忙

2024 iThome 鐵人賽

DAY 21
1
佛心分享-IT 人自學之術

合約開發特訓班系列 第 21

Keccak256 雜湊函數 hash input 與 output 範例

  • 分享至 

  • xImage
  •  

有位台灣人小哥(算是朋友的前同事),幾年前蓋了一個小工具叫 「Online Tools」(https://emn178.github.io/online-tools/)。今天就利用這個網站,簡單操作幾個 hash input 與 output 的範例,一起觀察 hash input 和 output 的變化。

這邊觀察的重點是,input 即使只有改動一小點,output 他的 hash value 就會有很大的變化。以太坊雜湊函數 hashing function 使用 keccak256,因此我們介面可選擇 keccak256 來了解。

第一個範例 "Why Georgia" 歌詞開頭當 input

https://ithelp.ithome.com.tw/upload/images/20241001/20169297syHOUENdKe.png

input 的地方,範例打的這一小段 text 是一首流行歌歌詞的開頭。文字編排主要照著歌手唱的方式做斷句。這首流行歌想想其實挺適合開車的時候聽,尤其開在美國的州際高速公路(類似台灣的國道一號嗎?)。像歌詞這裡寫的 85,是指美國喬治亞州亞特蘭大一帶,經常會需要路過的 I-85 高速公路(85 號州際高速公路) (https://en.wikipedia.org/wiki/Interstate_85_in_Georgia)。有點古老的演唱會 Why Georgia (Live) 影片連結:(https://youtu.be/uhYn7ryaWNE?si=j-mvRnQEaVRG-uF7)

input:
"I am driving up
85 in the
Kind of morning that
Lasts all afternoon
I'm just stuck inside the gloom"

接著,來看他的 output。
output 是這一長串 hash value:
bbdc357147880b7ddd3bd72f384130e4a09cc8a05705a5067d634c919c869b0b

第二個範例 "Why Georgia" 歌詞開頭 但多加一個空格 space 當 input

https://ithelp.ithome.com.tw/upload/images/20241001/20169297FVMpYezK0n.png

第二個範例,在 input 的地方做微小的改動。同一段歌詞開頭,但在最後一個字 gloom 的後面加一個空格 space。

"I am driving up
85 in the
Kind of morning that
Lasts all afternoon
I'm just stuck inside the gloom "

他的 output 變成是長得非常不一樣的一長串 hash value:
92b097d51d5df3e313d625585aad749cb29cfb6823a1742fa250d55205017708

回想 hash function 雜湊函式的 collision resistance

這幾個 hash input 和 output 範例操作,想傳達的重點是,output 對 input 非常敏感(sensitive)。output 的長相長什麼樣子對 input 的改動很敏感。即使 input 只有非常、非常小的改動,一丁點的改動,只差一個字,或甚至只差一個空白,通過了雜湊函式(範例用 keccak256),整個 hash output 已經面目全非。

表面上來看,hash value 出來,仍然一大長串的模樣。肉眼乍看之下,看起來相當亂的,隨機的 random hex。但這對電腦來說,可說是判若兩人。二個 hash values 截然不同,很好分辨。這是雜湊函數(hash function)就像是一種資料的「指紋」(fingerprint)的概念。

範例同時能表現,密碼學中雜湊函式的重要性質 collision resistance 抗碰撞性。攻擊者如果要找到碰撞的 output,想找到「兩個不一樣的訊息有同樣的 output」,需要花費大量的精力和金錢用電腦暴力搜索,大幅增加「發生 collision」的難度。範例中,兩個 output 不會一樣,也就不會發生 collision 碰撞。

維基百科:
https://en.wikipedia.org/wiki/Hash_function
https://en.wikipedia.org/wiki/Collision_resistance
https://en.wikipedia.org/wiki/Fingerprint_(computing)


上一篇
雜湊函數 hash functions 與資料完整性 data integrity
下一篇
雜湊樹 Merkle tree 與雜湊碰撞 collision
系列文
合約開發特訓班30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言