我目前正在结合 golang 开发 openpgp。我使用以下代码生成新的密钥对并在生成的公钥上创建自签名:
package main
import (
"bytes"
"crypto"
"time"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/openpgp/packet"
"fmt"
)
//Create ASscii Armor from openpgp.Entity
func PubEntToAsciiArmor(pubEnt *openpgp.Entity) (asciiEntity string) {
gotWriter := bytes.NewBuffer(nil)
wr, errEncode := armor.Encode(gotWriter, openpgp.PublicKeyType, nil)
if errEncode != nil {
fmt.Println("Encoding Armor ", errEncode.Error())
return
}
errSerial := pubEnt.Serialize(wr)
if errSerial != nil {
fmt.Println("Serializing PubKey ", errSerial.Error())
}
errClosing := wr.Close()
if errClosing != nil {
fmt.Println("Closing writer ", errClosing.Error())
}
asciiEntity = gotWriter.String()
return
}
func main() {
var entity *openpgp.Entity
entity, err := openpgp.NewEntity("itis", "test", "itis@itis3.com", nil)
if err != nil {
fmt.Println("ERROR")
}
usrIdstring := ""
for _, uIds := range entity.Identities {
usrIdstring = uIds.Name
}
var priKey = entity.PrivateKey
var sig = new(packet.Signature)
//Prepare sign with our configs/////IS IT A MUST ??
sig.Hash = crypto.SHA1
sig.PubKeyAlgo = priKey.PubKeyAlgo
sig.CreationTime = time.Now()
dur := new(uint32)
*dur = uint32(365 * 24 * 60 * 60)
sig.SigLifetimeSecs = dur //a year
issuerUint := new(uint64)
*issuerUint = priKey.KeyId
sig.IssuerKeyId = issuerUint
sig.SigType = packet.SigTypeGenericCert
err = sig.SignKey(entity.PrimaryKey, entity.PrivateKey, nil)
if err != nil {
fmt.Println("ERROR")
}
1.) 当我序列化公钥(以获得它的装甲版本)时,我收到以下错误消息:
序列化 PubKey openpgp:无效参数:签名:需要在序列化之前调用 Sign、SignUserId 或 SignKey
我以为我只是使用了所有可能的方法在该密钥上创建签名?
2.) 我仍然收到问题 1 的输出,当我将密钥上传到密钥服务器时,可用信息不完整。仅列出密钥 ID 和创建日期。缺少所有其他信息,例如自签名、用户 ID 字符串等(例如:https : //pgp.mit.edu/pks/lookup?search=0xbe6ee21e94a73ba5&op=index)。什么地方出了错?它与错误1有关吗?
PS:我刚接触golang,今天开始。
相关分类