Browse Source

Add ascii armor support

pull/1782/head
Jae Kwon 9 years ago
parent
commit
185547efc1
4 changed files with 66 additions and 1 deletions
  1. +39
    -0
      armor.go
  2. +25
    -0
      armor_test.go
  3. +0
    -0
      symmetric.go
  4. +2
    -1
      symmetric_test.go

+ 39
- 0
armor.go View File

@ -0,0 +1,39 @@
package crypto
import (
"bytes"
"io/ioutil"
. "github.com/tendermint/go-common"
"golang.org/x/crypto/openpgp/armor"
)
func EncodeArmor(blockType string, headers map[string]string, data []byte) string {
buf := new(bytes.Buffer)
w, err := armor.Encode(buf, blockType, headers)
if err != nil {
PanicSanity("Error encoding ascii armor: " + err.Error())
}
_, err = w.Write(data)
if err != nil {
PanicSanity("Error encoding ascii armor: " + err.Error())
}
err = w.Close()
if err != nil {
PanicSanity("Error encoding ascii armor: " + err.Error())
}
return string(buf.Bytes())
}
func DecodeArmor(armorStr string) (blockType string, headers map[string]string, data []byte, err error) {
buf := bytes.NewBufferString(armorStr)
block, err := armor.Decode(buf)
if err != nil {
return "", nil, nil, err
}
data, err = ioutil.ReadAll(block.Body)
if err != nil {
return "", nil, nil, err
}
return block.Type, block.Header, data, nil
}

+ 25
- 0
armor_test.go View File

@ -0,0 +1,25 @@
package crypto
import (
"bytes"
"testing"
)
func TestSimpleArmor(t *testing.T) {
blockType := "MINT TEST"
data := []byte("somedata")
armorStr := EncodeArmor(blockType, nil, data)
t.Log("Got armor: ", armorStr)
// Decode armorStr and test for equivalence.
blockType2, _, data2, err := DecodeArmor(armorStr)
if err != nil {
t.Error(err)
}
if blockType != blockType2 {
t.Errorf("Expected block type %v but got %v", blockType, blockType2)
}
if !bytes.Equal(data, data2) {
t.Errorf("Expected data %X but got %X", data2, data)
}
}

encrypt.go → symmetric.go View File


encrypt_test.go → symmetric_test.go View File


Loading…
Cancel
Save