mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-15 22:31:21 +00:00
Add ascii armor support
This commit is contained in:
39
armor.go
Normal file
39
armor.go
Normal 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
armor_test.go
Normal file
25
armor_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
@ -2,8 +2,9 @@ package crypto
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSimple(t *testing.T) {
|
func TestSimple(t *testing.T) {
|
Reference in New Issue
Block a user