GetArmorPublicKey 从文件读取GPG证书公钥信息
(gpgKeyFile string, passphrase []byte)
| 205 | |
| 206 | // GetArmorPublicKey 从文件读取GPG证书公钥信息 |
| 207 | func GetArmorPublicKey(gpgKeyFile string, passphrase []byte) (*DigitalSignPEM, error) { |
| 208 | if gpgKeyFile == "" { |
| 209 | return nil, errors.Errorf("please set GPG key file path") |
| 210 | } |
| 211 | // Open key |
| 212 | keyRingReader, err := os.Open(gpgKeyFile) |
| 213 | if err != nil { |
| 214 | return nil, err |
| 215 | } |
| 216 | // Read GPG Keys |
| 217 | elist, err := openpgp.ReadArmoredKeyRing(keyRingReader) |
| 218 | if err != nil { |
| 219 | return nil, err |
| 220 | } |
| 221 | if len(elist) < 1 { |
| 222 | return nil, errors.Errorf("file has no GPG key") |
| 223 | } |
| 224 | gpgKey := elist[0].PrivateKey |
| 225 | if gpgKey.Encrypted { |
| 226 | if len(passphrase) == 0 { |
| 227 | passphrase, err = gopass.GetPasswdPrompt("please input passphrase of key> ", true, os.Stdin, os.Stdout) |
| 228 | if err != nil { |
| 229 | return nil, err |
| 230 | } |
| 231 | } |
| 232 | err = gpgKey.Decrypt(passphrase) |
| 233 | if err != nil { |
| 234 | return nil, err |
| 235 | } |
| 236 | } |
| 237 | publicKeyArmor, err := GetPublicKeyArmorBytes(elist[0]) |
| 238 | if err != nil { |
| 239 | return nil, err |
| 240 | } |
| 241 | return &DigitalSignPEM{ |
| 242 | PrivateKey: gpgKey.PrivateKey.(*rsa.PrivateKey), |
| 243 | PublicKeyRaw: publicKeyArmor, |
| 244 | PublicKey: elist[0].PrimaryKey.PublicKey.(*rsa.PublicKey), |
| 245 | }, nil |
| 246 | } |
no test coverage detected