Go - Golang openpg - 创建密钥对并创建签名 [英] Go - Golang openpg - Create Key pair and create signature

查看:198
本文介绍了Go - Golang openpg - 创建密钥对并创建签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在与golang结合使用openpgp。我使用下面的代码来生成一个新的密钥对,并在结果公钥上创建一个自签名:

  package main 

导入(
字节
加密
时间
golang.org/x/crypto/openpgp
golang .org / x / crypto / openpgp / armor
golang.org/x/crypto/openpgp/packet
fmt


//从openpgp.Entity创建ASscii Armor
func PubEntToAsciiArmor(pubEnt * openpgp.Entity)(asciiEntity字符串){
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)$ b $ if errSerial!= nil {
fmt.Println(Serializing PubKey,errSerial.Error())
}
errClosing := wr.Close()
if errClosing!= nil {
fmt.Println(Closing wr iter,errClosing.Error())
}
asciiEntity = gotWriter.String()
return
}


func main ){

var entity * openpgp.Entity
entity,err:= openpgp.NewEntity(itis,test,itis@itis3.com,零)
如果err!= nil {
fmt.Println(ERROR)
}

usrIdstring:=
for _,uIds:= range实体。身份{
usrIdstring = uIds.Name

}

var priKey = entity.PrivateKey
var sig = new(packet.Signature)
//用我们的配置准备标志/////是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 //一年
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)
}
err = sig.SignUserId(usrIdstring,entity.PrimaryKey,entity.PrivateKey,nil)
if err != nil {
fmt.Println(ERROR)
}

entity.SignIdentity(usrIdstring,entity,nil)

var copy = entity
var asciiSignedKey = PubEntToAsciiArmor(copy)
fmt.Println(asciiSignedKey)
}



<1>。)当我序列化公钥(获得它的一个装甲版本)时,我得到了以下错误消息:


序列化PubKey openpgp:无效参数:签名:需要在序列化之前调用Sign,SignUserId或SignKey


我以为我只是用一切可能的方式在该密钥上创建签名?



2.)当我将密钥上传到密钥服务器时,我仍然收到来自问题1的输出,但可用信息不完整。仅列出key-id和创建日期。所有其他信息,如自签名,用户id字符串等都缺失(例如: https://pgp.mit.edu/pks/lookup?search=0xbe6ee21e94a73ba5&op=index )。什么地方出了错?是否与错误1有关?



PS:我是golang的新手,今天开始。 方案

也许这会做你想做的事。免责声明:我不是openpgp的专家;我不知道这是否正确。但它可以和gpg --import一起使用。

  package main 

import(
fmt
os

golang.org/x/crypto/openpgp
golang.org/x/crypto/openpgp/armor


func main(){
var e * openpgp.Entity
e,err:= openpgp.NewEntity(itis,test,itis@itis3.com ,nil)
if err!= nil {
fmt.Println(err)
return
}

//如果你在这里添加更多身份希望

//为_,id:=范围签署所有身份
e.Identities {
err:= id.SelfSignature.SignUserId(id.UserId.Id,e .PrimaryKey,e.PrivateKey,nil)
if err!= nil {
fmt.Println(err)
return
}
}

w,err:= armor.Encode(os.Stdout,openpgp.PublicKeyType,nil)
if err != nil {
fmt.Println(err)
return
}
推迟w.Close()

e.Serialize(w)
}


I'm currently working on openpgp in combination with golang. I use the following code to generate a new keypair and create a self-signature on the resulting public key:

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")
    }
    err = sig.SignUserId(usrIdstring, entity.PrimaryKey, entity.PrivateKey, nil)
    if err != nil {
        fmt.Println("ERROR")
    }

    entity.SignIdentity(usrIdstring, entity, nil)

    var copy = entity
    var asciiSignedKey = PubEntToAsciiArmor(copy)
    fmt.Println(asciiSignedKey)
}

1.) When I serialize the public key (to get an armored version of it), I get the following error message:

Serializing PubKey openpgp: invalid argument: Signature: need to call Sign, SignUserId or SignKey before Serialize

I thought I just used every possible way to create a signature on that key?

2.) I still receive an output from problem 1, when I upload the key to a keyserver, than the available information are incomplete. Only the key-id and the creation date are listed. All additional information like, self-signature, user-id-string and so on are missing (example: https://pgp.mit.edu/pks/lookup?search=0xbe6ee21e94a73ba5&op=index). What went wrong? Is it related to error 1?

PS: I am new to golang, started today.

解决方案

Maybe this will do what you want. Disclaimer: I am not an expert in openpgp; I don't know whether this is correct or not. But it does work with gpg --import.

package main

import (
        "fmt"
        "os"

        "golang.org/x/crypto/openpgp"
        "golang.org/x/crypto/openpgp/armor"
)

func main() {
        var e *openpgp.Entity
        e, err := openpgp.NewEntity("itis", "test", "itis@itis3.com", nil)
        if err != nil {
                fmt.Println(err)
                return
        }

        // Add more identities here if you wish

        // Sign all the identities
        for _, id := range e.Identities {
                err := id.SelfSignature.SignUserId(id.UserId.Id, e.PrimaryKey, e.PrivateKey, nil)
                if err != nil {
                        fmt.Println(err)
                        return
                }
        }

        w, err := armor.Encode(os.Stdout, openpgp.PublicKeyType, nil)
        if err != nil {
                fmt.Println(err)
                return
        }
        defer w.Close()

        e.Serialize(w)
}

这篇关于Go - Golang openpg - 创建密钥对并创建签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆