wrapper for mobile
This commit is contained in:
commit
8d300e4782
17 changed files with 1763 additions and 0 deletions
61
armor.go
Normal file
61
armor.go
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
package pm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/openpgp/armor"
|
||||
)
|
||||
|
||||
// AromrType ...
|
||||
type AromrType string
|
||||
|
||||
func (at AromrType) string() string {
|
||||
return string(at)
|
||||
}
|
||||
|
||||
const (
|
||||
pgpMessageType AromrType = "PGP MESSAGE"
|
||||
pgpPublicBlockType AromrType = "PGP PUBLIC KEY BLOCK"
|
||||
pgpPrivateBlockType AromrType = "PGP PRIVATE KEY BLOCK"
|
||||
)
|
||||
|
||||
// ArmorKey make bytes input key to armor format
|
||||
func ArmorKey(input []byte) (string, error) {
|
||||
return ArmorWithType(input, pgpPublicBlockType.string())
|
||||
}
|
||||
|
||||
// ArmorWithType make bytes input to armor format
|
||||
func ArmorWithType(input []byte, armorType string) (string, error) {
|
||||
var b bytes.Buffer
|
||||
w, err := armor.Encode(&b, armorType, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
_, err = w.Write(input)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
w.Close()
|
||||
return b.String(), nil
|
||||
}
|
||||
|
||||
// UnArmor an armored key to bytes key
|
||||
func UnArmor(input string) ([]byte, error) {
|
||||
b, err := unArmor(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ioutil.ReadAll(b.Body)
|
||||
}
|
||||
|
||||
func unArmor(input string) (*armor.Block, error) {
|
||||
io := strings.NewReader(input)
|
||||
b, err := armor.Decode(io)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b, nil
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue