dtkwt62022 2012-03-14 01: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 03: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部