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.

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

报告相同问题?

悬赏问题

  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用