dtz63853 2013-08-01 06:34
浏览 111
已采纳

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

I'm playing around with the openpgp package for Go by making a simple program that encrypts a short message and ASCII armors it. The code I currently have is:

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?")
}

When I run this I get the output:

-----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

It looks like it is outputting the armored cipher-text (which seems to be invalid) and then crashing because it can't find the registered hash function linked to the binary. From what I can tell it should be using one of the SHA hashes in the standard library by default which should definitely be linked?

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dongzhuifeng1843 2013-08-01 07:27
    关注

    Hash functions must be "registered" by import _ "whatever". The're not linked into your binary automagically. Actually, nothing not explicitly (or transitively) imported is. However, if you use some non imported entity directly, the compiler/linker will complain. Hash functions, OTOH, as well as some other parts of the stdlib, use a kind of late binding via the 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$ 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档