aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGibheer <gibheer+git@zero-knowledge.org>2017-05-12 22:19:25 +0200
committerGibheer <gibheer+git@zero-knowledge.org>2017-05-12 22:19:25 +0200
commite95929ed2641bf6548aada92d9d17a3441f19e2b (patch)
treec97488fbb3f89d313d452d74de635665f092be0e
parentb6c44317f540dac8763e720767b0e73940a0b6c5 (diff)
Add ToPem() to public keys
This was missing before from all public keys.
-rw-r--r--ecdsa.go16
-rw-r--r--ed25519.go10
-rw-r--r--rsa.go12
-rw-r--r--types.go1
4 files changed, 32 insertions, 7 deletions
diff --git a/ecdsa.go b/ecdsa.go
index 8bf432d..4202bd6 100644
--- a/ecdsa.go
+++ b/ecdsa.go
@@ -108,15 +108,23 @@ func LoadPublicKeyEcdsa(raw []byte) (*EcdsaPublicKey, error) {
return &EcdsaPublicKey{pub}, nil
}
+// ToPem returns the pem block of the public key.
+func (pu *EcdsaPublicKey) ToPem() (pem.Block, error) {
+ asn1, err := x509.MarshalPKIXPublicKey(pu.public_key)
+ if err != nil {
+ return pem.Block{}, err
+ }
+ return pem.Block{Type: PemLabelPublic, Bytes: asn1}, nil
+}
+
// This function implements the Pemmer interface to marshal the public key into
// a pem block.
func (pu *EcdsaPublicKey) MarshalPem() (io.WriterTo, error) {
- asn1, err := x509.MarshalPKIXPublicKey(pu.public_key)
- if err != nil {
+ if block, err := pu.ToPem(); err != nil {
return nil, err
+ } else {
+ return marshalledPemBlock(pem.EncodeToMemory(&block)), nil
}
- pem_block := pem.Block{Type: PemLabelPublic, Bytes: asn1}
- return marshalledPemBlock(pem.EncodeToMemory(&pem_block)), nil
}
// This function verifies a message using the public key, signature and hash
diff --git a/ed25519.go b/ed25519.go
index f7a0e12..cb56a70 100644
--- a/ed25519.go
+++ b/ed25519.go
@@ -92,9 +92,17 @@ func LoadPublicKeyEd25519(raw []byte) (*Ed25519PublicKey, error) {
return &Ed25519PublicKey{pu_loaded}, nil
}
+// ToPem returns the pem encoded public key.
+func (pu Ed25519PublicKey) ToPem() (pem.Block, error) {
+ return pem.Block{Type: PemLabelPublic, Bytes: pu.public_key[:]}, nil
+}
+
// Export the public key into the pem format.
func (pu Ed25519PublicKey) MarshalPem() (io.WriterTo, error) {
- pem_block := pem.Block{Type: PemLabelPublic, Bytes: pu.public_key[:]}
+ pem_block, err := pu.ToPem()
+ if err != nil {
+ return nil, err
+ }
return marshalledPemBlock(pem.EncodeToMemory(&pem_block)), nil
}
diff --git a/rsa.go b/rsa.go
index 7d575cb..b4024ac 100644
--- a/rsa.go
+++ b/rsa.go
@@ -86,13 +86,21 @@ func LoadPublicKeyRsa(raw []byte) (*RsaPublicKey, error) {
return pub, nil
}
+// ToPem returns the pem encoded public key.
+func (pu *RsaPublicKey) ToPem() (pem.Block, error) {
+ asn1, err := x509.MarshalPKIXPublicKey(pu.public_key)
+ if err != nil {
+ return pem.Block{}, err
+ }
+ return pem.Block{Type: PemLabelPublic, Bytes: asn1}, nil
+}
+
// marshal a rsa public key into pem format
func (pu *RsaPublicKey) MarshalPem() (io.WriterTo, error) {
- asn1, err := x509.MarshalPKIXPublicKey(pu.public_key)
+ pem_block, err := pu.ToPem()
if err != nil {
return nil, err
}
- pem_block := pem.Block{Type: PemLabelPublic, Bytes: asn1}
return marshalledPemBlock(pem.EncodeToMemory(&pem_block)), nil
}
diff --git a/types.go b/types.go
index 53db1a9..f2c6902 100644
--- a/types.go
+++ b/types.go
@@ -45,6 +45,7 @@ type (
// same functionality like verifying a message against a signature.
PublicKey interface {
Pemmer
+ PemOutput
// This function can be used to verify a message against a provided signature
// using the given hash function.
Verify(message []byte, signature []byte, hash crypto.Hash) (bool, error)