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
952 B

8 years ago
  1. package crypto
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. . "github.com/tendermint/tmlibs/common"
  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. PanicSanity("Error encoding ascii armor: " + err.Error())
  13. }
  14. _, err = w.Write(data)
  15. if err != nil {
  16. PanicSanity("Error encoding ascii armor: " + err.Error())
  17. }
  18. err = w.Close()
  19. if err != nil {
  20. PanicSanity("Error encoding ascii armor: " + err.Error())
  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. }