Splited encoding per object type, added public methods for serialization

This commit is contained in:
Jakub Nowakowski committed 2018-06-19 14:49:26 +02:00
1 parent 091f257cd5
commit e3cb617c60
12 files changed
+637 -365

No files matched your search

+79
View File
@@ -0,0 +1,79 @@
// Package bson provides functions for serialization and deserialization between
// paillier objects and BSON
package bson
import (
"fmt"
"github.com/keep-network/paillier"
"gopkg.in/mgo.v2/bson"
)
type PrivateKey paillier.PrivateKey
// Serializes PrivateKey to BSON
func SerializePrivateKey(key *paillier.PrivateKey) ([]byte, error) {
return bson.Marshal(toSerializablePrivateKey(key))
}
// Deserializes BSON to PrivateKey
func DeserializePrivateKey(data []byte) (*paillier.PrivateKey, error) {
serializable := new(PrivateKey)
if err := bson.Unmarshal(data, serializable); err != nil {
return nil, err
}
return toOriginalPrivateKey(serializable), nil
}
// Changes original PrivateKey to serializable PrivateKey
func toSerializablePrivateKey(key *paillier.PrivateKey) *PrivateKey {
serializable := PrivateKey(*key)
return &serializable
}
// Changes serializable PrivateKey to original PrivateKey
func toOriginalPrivateKey(serializable *PrivateKey) *paillier.PrivateKey {
original := paillier.PrivateKey(*serializable)
return &original
}
type dbPrivateKey struct {
N string `bson:",omitempty"`
Lambda string `bson:",omitempty"`
Mu string `bson:",omitempty"`
}
func (privateKey *PrivateKey) GetBSON() (interface{}, error) {
m := make(map[string]string)
if privateKey.N != nil {
m["n"] = fmt.Sprintf("%x", privateKey.N)
}
if privateKey.Lambda != nil {
m["lambda"] = fmt.Sprintf("%x", privateKey.Lambda)
}
return m, nil
}
func (privateKey *PrivateKey) SetBSON(raw bson.Raw) error {
var err error = nil
c := new(dbPrivateKey)
raw.Unmarshal(c)
if c.N != "" {
privateKey.N, err = fromHex(c.N)
if err != nil {
return err
}
}
if c.Lambda != "" {
privateKey.Lambda, err = fromHex(c.Lambda)
if err != nil {
return err
}
}
return err
}