You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
920 B

  1. package armor
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "golang.org/x/crypto/openpgp/armor"
  7. )
  8. func EncodeArmor(blockType string, headers map[string]string, data []byte) string {
  9. buf := new(bytes.Buffer)
  10. w, err := armor.Encode(buf, blockType, headers)
  11. if err != nil {
  12. panic(fmt.Errorf("could not encode ascii armor: %s", err))
  13. }
  14. _, err = w.Write(data)
  15. if err != nil {
  16. panic(fmt.Errorf("could not encode ascii armor: %s", err))
  17. }
  18. err = w.Close()
  19. if err != nil {
  20. panic(fmt.Errorf("could not encode ascii armor: %s", err))
  21. }
  22. return buf.String()
  23. }
  24. func DecodeArmor(armorStr string) (blockType string, headers map[string]string, data []byte, err error) {
  25. buf := bytes.NewBufferString(armorStr)
  26. block, err := armor.Decode(buf)
  27. if err != nil {
  28. return "", nil, nil, err
  29. }
  30. data, err = ioutil.ReadAll(block.Body)
  31. if err != nil {
  32. return "", nil, nil, err
  33. }
  34. return block.Type, block.Header, data, nil
  35. }