github.com/vipernet-xyz/tm@v0.34.24/crypto/armor/armor.go (about)

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