pgp.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // This file is part of the go-ethereum library.
  2. //
  3. // The go-ethereum library is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Lesser General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // The go-ethereum library is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU Lesser General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU Lesser General Public License
  14. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  15. // signFile reads the contents of an input file and signs it (in armored format)
  16. // with the key provided, placing the signature into the output file.
  17. package build
  18. import (
  19. "bytes"
  20. "fmt"
  21. "os"
  22. "golang.org/x/crypto/openpgp"
  23. )
  24. // PGPSignFile parses a PGP private key from the specified string and creates a
  25. // signature file into the output parameter of the input file.
  26. //
  27. // Note, this method assumes a single key will be container in the pgpkey arg,
  28. // furthermore that it is in armored format.
  29. func PGPSignFile(input string, output string, pgpkey string) error {
  30. // Parse the keyring and make sure we only have a single private key in it
  31. keys, err := openpgp.ReadArmoredKeyRing(bytes.NewBufferString(pgpkey))
  32. if err != nil {
  33. return err
  34. }
  35. if len(keys) != 1 {
  36. return fmt.Errorf("key count mismatch: have %d, want %d", len(keys), 1)
  37. }
  38. // Create the input and output streams for signing
  39. in, err := os.Open(input)
  40. if err != nil {
  41. return err
  42. }
  43. defer in.Close()
  44. out, err := os.Create(output)
  45. if err != nil {
  46. return err
  47. }
  48. defer out.Close()
  49. // Generate the signature and return
  50. return openpgp.ArmoredDetachSign(out, keys[0], in, nil)
  51. }