I'm developing an API using Golang, and I have a JSON file keys.json
as follows:
{
"publicKeys": {
"Flex": "<valid pgp public key>",
"Flex2": "<valid pgp public key>"
},
"privateKey": "<valid pgp private key>"
}
To unmarshal this, I have the following model
type PGPKeys struct {
PublicKeys map[string]string `json:"publicKeys"`
PrivateKey string `json:"privateKey"`
}
and I unmarshal the code using
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
}
Later, when I use openpgp
to get the public key packet, I am met with EOF
error which armor.Decode
returns when it's unable to find any blocks -- but I'm not sure why it's happening
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
}
NOTE: When I call the function, I do type conversion using something like
publicKeyPacket, err := pgp.GetPublicKeyPacket([]byte(h.PGPKeys.PublicKeys[h.Config.PGPIdentifier]))
Finally, I have tried moving the keys into individual TXT files and that works but for some reason having them in JSON does not