doucai1901 2018-09-12 09:18
浏览 115

等价于gpg-登录golang吗?

I currently have a Perl script that performs a PGP sign on an email address to produce an obfuscated token. The Perl script uses the command line utility GPG version 2.2.8 (on MacOS). I would like to convert this functionality to Go.

The relevant line in Perl:

my $token = `printf $emailaddress | gpg --sign -u myprivatekeyname | base64`;

My attempt in Go:

import (
    "golang.org/x/crypto/openpgp"
    "log"
    "fmt"
    "strings"
    "bytes"
    )


const privateKey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
<the same private key as is used by the GPG tool>
-----END PGP PRIVATE KEY BLOCK-----`

func main() {
    email := "bob@foo.com"
    token := makeToken(email)
    fmt.Println(token)
}

func getPrivateKey() *openpgp.Entity {
    pp := "mypassphrase"
    ppb := []byte(pp)

    entitylist, err := openpgp.ReadArmoredKeyRing(strings.NewReader(privateKey))
    if err != nil {
        log.Fatal(err)
    }

    entity := entitylist[0]

    if entity.PrivateKey != nil && entity.PrivateKey.Encrypted {
        err := entity.PrivateKey.Decrypt(ppb)
        if err != nil {
            fmt.Println("Failed to decrypt key")
        }
    }

    for _, subkey := range entity.Subkeys {
        if subkey.PrivateKey != nil && subkey.PrivateKey.Encrypted {
            err := subkey.PrivateKey.Decrypt(ppb)
            if err != nil {
                fmt.Println("Failed to decrypt subkey")
            }
        }
    }
    return entity
}

func makeToken(email string) string {
    pk := getPrivateKey()
    out := new(bytes.Buffer)
    in, err := openpgp.Sign(out, pk, nil, nil)
    if err != nil {
        log.Fatal(err)
    }
    in.Write([]byte(email))
    in.Close()
    return base64.StdEncoding.EncodeToString(out.Bytes())
}

Unfortunately the results for a given email addresss (like 'bob@foo.com') are not identical.

I am using the same private key: I exported it from GPG and pasted it into my go code (obviously this is just for testing, I won't leave it like that).

Any idea what Iam doing wrong?

  • 写回答

1条回答 默认 最新

  • dongnai8013 2018-09-12 09:35
    关注

    You cannot compare signatures based on a binary diff; they include timestamps. Furthermore, you might apply different compression and signature algorithms. Compare the output of gpg --list-packets or pgpdump instead.

    Looking at a rather arbitrary signature:

    $ echo foo | gpg --sign | gpg --list-packets
    

    Here, you might be using different compression algorithms (or none at all, here: 1):

    # off=0 ctb=a3 tag=8 hlen=1 plen=0 indeterminate
    :compressed packet: algo=1
    

    The signature packet might use different digest algorithms (here: 10):

    # off=2 ctb=90 tag=4 hlen=2 plen=13
    :onepass_sig packet: keyid 8E78E44DFB1B55E9
        version 3, sigclass 0x00, digest 10, pubkey 1, last=1
    

    Here we have a timestamp:

    # off=17 ctb=cb tag=11 hlen=2 plen=10 new-ctb
    :literal data packet:
        mode b (62), created 1536744536, name="",
        raw data: 4 bytes
    

    And another timestamp (the signing algorithm should be the same for the same key):

    # off=29 ctb=89 tag=2 hlen=3 plen=563
    :signature packet: algo 1, keyid 8E78E44DFB1B55E9
        version 4, created 1536744536, md5len 0, sigclass 0x00
        digest algo 10, begin of digest f4 03
        hashed subpkt 33 len 21 (issuer fpr v4 292F04A99AA52EC6F3088C608E78E44DFB1B55E9)
        hashed subpkt 2 len 4 (sig created 2018-09-12)
        subpkt 16 len 8 (issuer key ID 8E78E44DFB1B55E9)
        data: [4096 bits]
    

    By diffing the output of gpg --list-packets, you should be able to understand whether there is any noteworthy difference in the output. If you're using the same algorithms and fake the time, you should be able to get the same output.

    The constants/magic numbers mentioned above are also defined in RFC 4880, OpenPGP.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大