dongwen2896 2019-01-10 05:05

# 如何在Golang中计算嵌套/迭代的MD5哈希？

I'm in the process of building a program to bruteforce passwords using golang. The format of the password hashes are a md5 hash applied 1000x to the initial password and then that being used. (The code I show is only applying this 5x)

``````func hash(pw string) string {
hasher := md5.New()

data := []byte(pw)
fmt.Printf("Initial data: %s
", pw)

for i := 0; i < 5; i++ {
hasher.Reset()
hasher.Write(data)
sum := hasher.Sum(nil)
data = sum[:]
fmt.Printf("Iteration %x has the hash: %x
", i+1, data)
}
return hex.EncodeToString(data)
}
``````

The result from this differs from what using the command line utility md5sum gives. My other attempt was to use, because this was stateless but I still start to deviate on the second round of hashing

``````sum := md5.Sum([]byte(data))
``````

What is a good/successful way of achieving calculating this iterated hash?

• 写回答

#### 1条回答默认 最新

• douyou2048 2019-01-10 05:22
关注

Maybe I'm misunderstanding your question, but is this what you're looking for?

``````package main

import (
"crypto/md5"
"fmt"
"io"
)

func main() {
fmt.Printf("
result: %s", md5plus("123", 2))
}

func md5plus(text string, cost int) string {
for i := 0; i < cost; i++ {
fmt.Printf("Loop %d: %s", i+1, text)
hash := md5.New()
io.WriteString(hash, text)
text = fmt.Sprintf("%x", hash.Sum(nil))
fmt.Printf(" => %s
", text)
}

return text
}
``````

https://play.golang.org/p/ri-5m3RZ_8v

I realize you're trying to reuse the hasher in your version, but to my understanding, that's not how the library is intended to be use. You write to it to compute single hashes, not rounds.

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

#### 悬赏问题

• ¥50 swiftui @query 报错
• ¥50 怎么解决刷卡或扫码后，点击软件输入框，win10屏幕键盘不会自动弹出的问题
• ¥15 如何使用arcgispro的训练深度模型，发现water和nowater精度为0？（相关搜索：深度学习）
• ¥20 matlab作业不太懂呀有问题能给个代码吗
• ¥15 自制电路图为何无法驱动ESP01S？
• ¥15 前端加access数据库
• ¥15 ARCGIS 多值提取到点 ERROR 999999
• ¥15 mysql异常断电， [MY-011971] [InnoDB]
• ¥15 uni.onBluetoothDeviceFound熄屏不运行
• ¥15 求PHDA糖尿病并发症数据集，有偿