Go中的OpenPGP错误:“加密:请求的哈希函数不可用”

时间:2013-08-01 06:34:29

标签: hash cryptography go gnupg openpgp

我正在玩Go的openpgp包,通过制作一个加密短消息和ASCII装甲的简单程序。我目前的代码是:

package main

import (
    "code.google.com/p/go.crypto/openpgp"
    "code.google.com/p/go.crypto/openpgp/armor"
    "fmt"
    "log"
    "os"
)

func main() {
    to, err := openpgp.NewEntity("John Smith", "comment", "john@example.com", nil)
    if err != nil {
        log.Fatal(err)
    }

    from, err := openpgp.NewEntity("John Smith", "comment", "john@example.com", nil)
    if err != nil {
        log.Fatal(err)
    }

    recipients := []*openpgp.Entity{to}

    armorer, err := armor.Encode(os.Stdout, "PGP MESSAGE", nil)
    if err != nil {
        log.Fatal(err)
    }

    defer armorer.Close()

    encrypter, err := openpgp.Encrypt(armorer, recipients, from, nil, nil)
    if err != nil {
        log.Fatal(err)
    }

    defer encrypter.Close()

    fmt.Fprintf(encrypter, "Hello! How are you?")
}

当我运行时,我得到输出:

-----BEGIN PGP MESSAGE-----

wcBMA5iJWayscZQHAQgAvHQtyGxzyHJNYQlK7bcYZnWRm1gHsRRVZIgPTxtqnklp
KgCYOuv5dtgqUXinJWZnk3DozR7xbMj/c9oSlifJ9FiPnTaLLOIVhv2Jf7w9ydh3
oig3vpL8rI/ha9bOJsNdznt3/z1ZLVghPsrRGHO7LWUTWQUn/cgQZXr/z5CpGQP0
TFlMcbBuuwE2ffplAGkBSQlXNRx1ZsY6T+MjajWwcik/CjJ/NnzRpURpXFIWc85x
HQbUexQzEghyh4t5NEQhx9UvmLtnKPmwnhxX3s43Zcd1VtzSXc77giE08fSdIDdH
eDClMno6jsoWhmubXZVXOHqf+z5euEcxvYyk16N/jdLgAeMBTSifCFR8YuGBmOFA
DONO/4/M1ye76+KyhsbF4KTgK+CW4Rjg4NLi0ubP1A==
=YD2p
-----END PGP MESSAGE-----panic: crypto: requested hash function is unavailable

goroutine 1 [running]:
crypto.Hash.New(0x9, 0x21027c4b0, 0x1d3300)
        /usr/local/Cellar/go/1.1.1/src/pkg/crypto/crypto.go:62 +0xab
code.google.com/p/go.crypto/openpgp.Encrypt(0x210299e70, 0x2102e76e0, 0x2210284f38, 0x1, 0x1, ...)
        /Users/cb/workspace/go/src/code.google.com/p/go.crypto/openpgp/write.go:289 +0x1141
main.main()
        /Users/cb/workspace/go/src/github.com/xoebus/gpg/go-gpg.go:31 +0x425
exit status 2

看起来它正在输出装甲密文(似乎无效)然后崩溃,因为它无法找到链接到二进制文件的已注册哈希函数。据我所知,默认情况下它应该使用标准库中的一个SHA哈希值,它肯定会被链接?

我做错了什么?

1 个答案:

答案 0 :(得分:5)

哈希函数必须由import _ "whatever"“注册”。它们没有自动链接到您的二进制文件。实际上,没有任何明确(或传递)导入的是。但是,如果直接使用某个非导入实体,编译器/链接器会抱怨。散列函数OTOH以及stdlib的其他一些部分通过Hash interface使用一种后期绑定。

(09:25) jnml@fsc-r550:~/src/tmp/SO/17986762$ cat main.go 
package main

import (
        "fmt"
        "log"
        "os"

        "code.google.com/p/go.crypto/openpgp"
        "code.google.com/p/go.crypto/openpgp/armor"
        _ "code.google.com/p/go.crypto/ripemd160"
)

func main() {
        to, err := openpgp.NewEntity("John Smith", "comment", "john@example.com", nil)
        if err != nil {
                log.Fatal(err)
        }

        from, err := openpgp.NewEntity("John Smith", "comment", "john@example.com", nil)
        if err != nil {
                log.Fatal(err)
        }

        recipients := []*openpgp.Entity{to}

        armorer, err := armor.Encode(os.Stdout, "PGP MESSAGE", nil)
        if err != nil {
                log.Fatal(err)
        }

        defer armorer.Close()

        encrypter, err := openpgp.Encrypt(armorer, recipients, from, nil, nil)
        if err != nil {
                log.Fatal(err)
        }

        defer encrypter.Close()

        fmt.Fprintf(encrypter, "Hello! How are you?")
}
(09:25) jnml@fsc-r550:~/src/tmp/SO/17986762$ go run main.go 
-----BEGIN PGP MESSAGE-----

wcBMA6art6sLA/RNAQgAyScgc5RZErJ59rFreeOiotvMcSDgIRgdoxyKjo7qHxhz
jwhxYv/fqLgpYq5AbOCXVUK4Ubt+vmr+ET/txhW5tCx96cTZXVmyuke89+yQ6SQ7
ESlZxq9X973lpOq/I1rXY6T8bgfz4TY3w9Ur0XevnB9uJKv7nMaWCG46f//uT1P8
wD7eqv8hupJK+FnyzU5SzDyeO20ZzyN0e6M2GobhmmOmik3iB491+NskC5gBL2oB
kZBJROOrBtp9z27YXZTFjewgxzkUNzw+IVlsQD5h6XiyzOIfD2UtxvzxJs6qe3Kf
iAlqPfsETCBi6qxpnaBlUVQycfMCClbWzoI54dq3m9LgAeOJhljWnwSGnOGdLOGW
G+MDigPW5TYnD+JX4dRg4G3g/+Dm4VyV4DbihPWbZ+CA5IOlWefWOHpkJSOQi6JU
H1DgluHA9eAb4FjgYuGBaeCz5N8ur0undv+IiD08IJXlzgjiP74GiuGc2uFFgeFz
YOH3K+jHj0qPYhceyDp8mtmCgy7E/523HYsLgzu9nXbKajafo/6HRa2KLM1d0VGM
qZsEqF6Kr/1m1qBaV94oWju5z0f+bBtNCgXjbP2ibFAUlmuFvKsRHZVsdOo8BLkd
ed5Q9sw1fMXSzA6cxYClpeN6BmJ3KlLEkn6wxmbpUxR6VI17I2iZNA4rhzU2fYRR
egESqUF2tfZP5MDNUx8QzQdP6ch3okdKBg7hga59YA66QqbV2KczixWaFssWyqI+
AtInYYD/bHJQYHGdHgST3aLUO6wEALgpcK55AWHHjJAwoZUoVkEFN8KN6pAEutur
jRjTcfnkbCz4l/CxuN+jzLTevogM5NO+RU7ZKMkKVhzRoGM95lviTSwkj+E5mgA=
=wWmw
-----END PGP MESSAGE-----(09:26) jnml@fsc-r550:~/src/tmp/SO/17986762$