dongwen2896 2019-01-10 05:05
浏览 118
已采纳

如何在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)

md5(md5(md5(md5(....(md5(password))))))

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.

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

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失