0
0
Fork 0

fix go evt problem

Go vet reported a problem with the interface of WriteTo returning an
int.

  Line 13: method WriteTo(stream io.Writer) (int, error)
        should have signature WriteTo(io.Writer) (int64, error)

To fix that problem, the interface was changed to return io.WriterTo,
which uses int64 for return values.
This commit is contained in:
Gibheer 2015-03-25 20:12:41 +01:00
parent a11b67a64e
commit 11423e25b5
4 changed files with 15 additions and 11 deletions

View File

@ -9,6 +9,7 @@ import (
"encoding/asn1" "encoding/asn1"
"encoding/pem" "encoding/pem"
"errors" "errors"
"io"
"math/big" "math/big"
) )
@ -72,11 +73,11 @@ func (pr EcdsaPrivateKey) PrivateKey() crypto.PrivateKey {
// This function implements the Pemmer interface to marshal the private key // This function implements the Pemmer interface to marshal the private key
// into a pem block. // into a pem block.
func (pr EcdsaPrivateKey) MarshalPem() (marshalledPemBlock, error) { func (pr EcdsaPrivateKey) MarshalPem() (io.WriterTo, error) {
asn1, err := x509.MarshalECPrivateKey(pr.private_key) asn1, err := x509.MarshalECPrivateKey(pr.private_key)
if err != nil { return nil, err } if err != nil { return nil, err }
pem_block := pem.Block{Type: PemLabelEcdsa, Bytes: asn1} pem_block := pem.Block{Type: PemLabelEcdsa, Bytes: asn1}
return pem.EncodeToMemory(&pem_block), nil return marshalledPemBlock(pem.EncodeToMemory(&pem_block)), nil
} }
// This functoin loads an ecdsa public key from the asn.1 representation. // This functoin loads an ecdsa public key from the asn.1 representation.
@ -91,11 +92,11 @@ func LoadPublicKeyEcdsa(raw []byte) (*EcdsaPublicKey, error) {
// This function implements the Pemmer interface to marshal the public key into // This function implements the Pemmer interface to marshal the public key into
// a pem block. // a pem block.
func (pu *EcdsaPublicKey) MarshalPem() (marshalledPemBlock, error) { func (pu *EcdsaPublicKey) MarshalPem() (io.WriterTo, error) {
asn1, err := x509.MarshalPKIXPublicKey(pu.public_key) asn1, err := x509.MarshalPKIXPublicKey(pu.public_key)
if err != nil { return nil, err } if err != nil { return nil, err }
pem_block := pem.Block{Type: PemLabelPublic, Bytes: asn1} pem_block := pem.Block{Type: PemLabelPublic, Bytes: asn1}
return pem.EncodeToMemory(&pem_block), nil return marshalledPemBlock(pem.EncodeToMemory(&pem_block)), nil
} }
// This function verifies a message using the public key, signature and hash // This function verifies a message using the public key, signature and hash

View File

@ -10,6 +10,7 @@ type (
// This function writes the marshalled pem block to a writer and returns the // This function writes the marshalled pem block to a writer and returns the
// number of written bytes and eventual errors. // number of written bytes and eventual errors.
func (b marshalledPemBlock) WriteTo(stream io.Writer) (int, error) { func (b marshalledPemBlock) WriteTo(stream io.Writer) (int64, error) {
return stream.Write(b) numBytes, err := stream.Write(b)
return int64(numBytes), err
} }

9
rsa.go
View File

@ -7,6 +7,7 @@ import (
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"errors" "errors"
"io"
) )
const ( const (
@ -50,10 +51,10 @@ func (pr RsaPrivateKey) PrivateKey() crypto.PrivateKey {
return pr.private_key return pr.private_key
} }
func (pr RsaPrivateKey) MarshalPem() (marshalledPemBlock, error) { func (pr RsaPrivateKey) MarshalPem() (io.WriterTo, error) {
asn1 := x509.MarshalPKCS1PrivateKey(pr.private_key) asn1 := x509.MarshalPKCS1PrivateKey(pr.private_key)
pem_block := pem.Block{Type: PemLabelRsa, Bytes: asn1} pem_block := pem.Block{Type: PemLabelRsa, Bytes: asn1}
return pem.EncodeToMemory(&pem_block), nil return marshalledPemBlock(pem.EncodeToMemory(&pem_block)), nil
} }
// restore a rsa public key // restore a rsa public key
@ -62,11 +63,11 @@ func LoadPublicKeyRsa(raw []byte) (*RsaPublicKey, error) {
} }
// marshal a rsa public key into pem format // marshal a rsa public key into pem format
func (pu *RsaPublicKey) MarshalPem() (marshalledPemBlock, error) { func (pu *RsaPublicKey) MarshalPem() (io.WriterTo, error) {
asn1, err := x509.MarshalPKIXPublicKey(pu.public_key) asn1, err := x509.MarshalPKIXPublicKey(pu.public_key)
if err != nil { return nil, err } if err != nil { return nil, err }
pem_block := pem.Block{Type: PemLabelPublic, Bytes: asn1} pem_block := pem.Block{Type: PemLabelPublic, Bytes: asn1}
return pem.EncodeToMemory(&pem_block), nil return marshalledPemBlock(pem.EncodeToMemory(&pem_block)), nil
} }
// verify a message with a signature using the public key // verify a message with a signature using the public key

View File

@ -16,6 +16,7 @@ package pki
import ( import (
"crypto" "crypto"
"io"
) )
// This label is used as the type in the pem encoding of public keys. // This label is used as the type in the pem encoding of public keys.
@ -47,6 +48,6 @@ type (
// in the pem format. The result can then be written to any structure // in the pem format. The result can then be written to any structure
// implementing the io.Writer interface. // implementing the io.Writer interface.
Pemmer interface { Pemmer interface {
MarshalPem() (marshalledPemBlock, error) MarshalPem() (io.WriterTo, error)
} }
) )