encryptWithOpenPGP attempts to encrypt the data key using OpenPGP with the PGP key that belongs to Fingerprint. It sets EncryptedDataKey, or returns an error.
(dataKey []byte)
| 299 | // PGP key that belongs to Fingerprint. It sets EncryptedDataKey, or returns |
| 300 | // an error. |
| 301 | func (key *MasterKey) encryptWithOpenPGP(dataKey []byte) error { |
| 302 | entity, err := key.retrievePubKey() |
| 303 | if err != nil { |
| 304 | return err |
| 305 | } |
| 306 | |
| 307 | encBuf := new(bytes.Buffer) |
| 308 | armorBuf, err := armor.Encode(encBuf, "PGP MESSAGE", nil) |
| 309 | if err != nil { |
| 310 | return err |
| 311 | } |
| 312 | plainBuf, err := openpgp.Encrypt(armorBuf, []*openpgp.Entity{&entity}, nil, &openpgp.FileHints{IsBinary: true}, nil) |
| 313 | if err != nil { |
| 314 | return err |
| 315 | } |
| 316 | _, err = plainBuf.Write(dataKey) |
| 317 | if err != nil { |
| 318 | return err |
| 319 | } |
| 320 | err = plainBuf.Close() |
| 321 | if err != nil { |
| 322 | return err |
| 323 | } |
| 324 | err = armorBuf.Close() |
| 325 | if err != nil { |
| 326 | return err |
| 327 | } |
| 328 | |
| 329 | b, err := io.ReadAll(encBuf) |
| 330 | if err != nil { |
| 331 | return err |
| 332 | } |
| 333 | |
| 334 | key.SetEncryptedDataKey(b) |
| 335 | return nil |
| 336 | } |
| 337 | |
| 338 | // encryptWithOpenPGP attempts to encrypt the data key using GnuPG with the |
| 339 | // PGP key that belongs to Fingerprint. It sets EncryptedDataKey, or returns |