【发布时间】:2019-03-20 05:13:04
【问题描述】:
我正在使用 Golang 开发一个 API,我有一个 JSON 文件 keys.json 如下:
{
"publicKeys": {
"Flex": "<valid pgp public key>",
"Flex2": "<valid pgp public key>"
},
"privateKey": "<valid pgp private key>"
}
为了解组这个,我有以下模型
type PGPKeys struct {
PublicKeys map[string]string `json:"publicKeys"`
PrivateKey string `json:"privateKey"`
}
我使用
解组代码keysJSONFile, err := os.Open(keysPath)
if keysJSONFile != nil {
defer keysJSONFile.Close()
}
if err != nil {
return nil, err
}
keysJSONBytes, err := ioutil.ReadAll(keysJSONFile)
if err != nil {
return nil, err
}
var pgpKeys PGPKeys
err = json.Unmarshal(keysJSONBytes, &pgpKeys)
if err != nil {
return nil, err
}
稍后,当我使用openpgp 获取公钥数据包时,遇到EOF 错误,armor.Decode 在找不到任何块时返回该错误-但我不确定为什么会这样
func GetPublicKeyPacket(publicKey []byte) (*packet.PublicKey, error) {
publicKeyReader := bytes.NewReader(publicKey)
block, err := armor.Decode(publicKeyReader)
if err != nil {
return nil, err
}
if block.Type != openpgp.PublicKeyType {
return nil, errors.New("Invalid public key data")
}
packetReader := packet.NewReader(block.Body)
pkt, err := packetReader.Next()
if err != nil {
return nil, err
}
key, ok := pkt.(*packet.PublicKey)
if !ok {
return nil, err
}
return key, nil
}
注意:当我调用函数时,我会使用类似的东西进行类型转换
publicKeyPacket, err := pgp.GetPublicKeyPacket([]byte(h.PGPKeys.PublicKeys[h.Config.PGPIdentifier]))
最后,我尝试将密钥移动到单独的 TXT 文件中,并且可行,但由于某种原因,将它们放在 JSON 中不起作用
【问题讨论】: