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 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多