// MarshalPrivateKey converts a key object into its protobuf serialized form. func MarshalPrivateKey(k PrivKey) ([]byte, error) { b := MarshalRsaPrivateKey(k.(*RsaPrivateKey)) pmes := new(pb.PrivateKey) typ := pb.KeyType_RSA // for now only type. pmes.Type = &typ pmes.Data = b return proto.Marshal(pmes) }
func (sk *RsaPrivateKey) Bytes() ([]byte, error) { b := x509.MarshalPKCS1PrivateKey(sk.sk) pbmes := new(pb.PrivateKey) typ := pb.KeyType_RSA pbmes.Type = &typ pbmes.Data = b return proto.Marshal(pbmes) }
// UnmarshalPrivateKey converts a protobuf serialized private key into its // representative object func UnmarshalPrivateKey(data []byte) (PrivKey, error) { pmes := new(pb.PrivateKey) err := proto.Unmarshal(data, pmes) if err != nil { return nil, err } switch pmes.GetType() { case pb.KeyType_RSA: return UnmarshalRsaPrivateKey(pmes.GetData()) default: return nil, ErrBadKeyType } }
//Encode into a protobuf func MarshalAccount(acc Account) ([]byte, error) { accpb := new(pb.Account) // make proto account accpb.RegistrationDate = &acc.RegistrationDate //Gather up Private key3 cPrivData := crypto.MarshalRsaPrivateKey(acc.PrivKey.(*crypto.RsaPrivateKey)) cPrivK := new(keypb.PrivateKey) typ := keypb.KeyType_RSA cPrivK.Type = &typ cPrivK.Data = cPrivData accpb.PrivKey = cPrivK // add proto private key //Gather up Public key cPubData, err2 := crypto.MarshalRsaPublicKey(acc.PubKey.(*crypto.RsaPublicKey)) if err2 != nil { return nil, err2 } cPubK := new(keypb.PublicKey) cPubK.Type = &typ cPubK.Data = cPubData accpb.PubKey = cPubK // add proto public key //Do the damn thing return proto.Marshal(accpb) //Explosions }