dtkwt62022 2012-03-14 09:39
浏览 59
已采纳

如何在Go中实现HashCash的算法(类型转换问题)?

I've been trying to implement the HashCash algorithm in Go! For those of you who don't know -

HashCash is a method to stop spam. Basically, a header is constructed of some environment variables known both to the client and server (email, timestamp etc.). A random nonce is appended to the end of the header. The client tries to bruteforce a partial hash collision (e.g. where the first x bits are 0) by changing the nonce.

HashCash works because it's not as expensive to find partial hash collisions. When the server receives this header, they verify the information in it (so it can be used only for one session) and compute the resulting hash. If the first x bits are 0, then a good amount of time has been expended on the client's machine, computing the collision (which wouldn't happen on a spambot)

For me, I'm just wanting to write a program which finds the time it takes for a client to find a partial hash collision of x bits.

I wrote this code which will return true/false if the int64 has a hash collision of x bits.

func partialAllZeroes (zeroCount uint8, val int64) (bool, os.Error) {
    setBitString := "1111111111111111111111111111111111111111111111111111111111111111"
    unsetBitString := "0000000000000000000000000000000000000000000000000000000000000000"
    setBitString = setBitString[0:zeroCount-1]
    unsetBitString = unsetBitString[0:zeroCount-1]

    zeroTest, e := strconv.Btoi64(setBitString, 2) // 64 0bits
    zeroes, e   := strconv.Btoi64(unsetBitString, 2) // 64 1bits

    if e != nil {
        return false, e
    }
    result := val & zeroTest
    switch {
        case result == zeroes:
            return true, nil
        case result != zeroes:
            return false, nil
    }

    return false, os.NewError("")
}

My current problem is I'm having alot of type conversion issues. For example, I am only able to operate on the int64 type, because that's what strconv.Btoi64 returns. Another issue that I'm also looking at is that the hash function returns as a byte array, and I have no idea how to convert that into an int64.

Below is my current hash code -

hasher := sha1.New()
baseCollisionString := "BASE COLLISION STRING"
nonce := "12345"
hasher.Write([]byte(strings.Join(baseCollisionString, nonce)))
testCollision := hasher.Sum()
// Somehow I must convert the first x bits of testCollision into an int64 type, so I can use partialAllZeroes with it
  • 写回答

3条回答 默认 最新

  • dowbwrr3590709 2012-03-14 11:22
    关注

    I would suggest the following code (the function partialAllZeroes should run much faster):

    package main
    
    import "crypto/sha1"
    
    func partialAllZeroes(zeroCount uint8, b []byte) bool {
        i := 0
        for zeroCount >= 8 {
            if b[i] != 0 {
                return false
            }
            i++
            zeroCount -= 8
        }
    
        var mask byte
        switch zeroCount {
        case 0: mask = 0x00
        case 1: mask = 0x01
        case 2: mask = 0x03
        case 3: mask = 0x07
        case 4: mask = 0x0f
        case 5: mask = 0x1f
        case 6: mask = 0x3f
        case 7: mask = 0x7f
        }
    
        return (b[i] & mask) == 0
    }
    
    func main() {
        hasher := sha1.New()
        baseCollisionString := "BASE COLLISION STRING"
        nonce := "12345"
        hasher.Write([]byte(baseCollisionString + nonce))
        testCollision := hasher.Sum()
        partialAllZeroes(100, testCollision)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀