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 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable