Skip to content
Snippets Groups Projects
Commit 5c6b6dfd authored by David Stainton's avatar David Stainton
Browse files

Bytes use 32 bit int and add explanatory docstring

parent 257b7f27
No related branches found
No related tags found
2 merge requests!23Channels Implementation,!21Add PublicKey's Bytes and FromBytes + tests
...@@ -131,18 +131,22 @@ func (p *PrivateKey) GetE() int { ...@@ -131,18 +131,22 @@ func (p *PrivateKey) GetE() int {
} }
// Bytes returns the PublicKey as a byte slice. // Bytes returns the PublicKey as a byte slice.
// The first 4 bytes are the exponent (E) as a 4 byte big
// endian integer, followed by the modulus (N) as a big.Int
// in Bytes format. We should the 32 bit integer for E
// because it should be big enough.
func (p *PublicKey) Bytes() []byte { func (p *PublicKey) Bytes() []byte {
buf := make([]byte, 8) buf := make([]byte, 4)
binary.BigEndian.PutUint64(buf, uint64(p.GetE())) binary.BigEndian.PutUint32(buf, uint32(p.GetE()))
return append(buf, p.PublicKey.N.Bytes()...) return append(buf, p.PublicKey.N.Bytes()...)
} }
// FromBytes loads the given byte slice into the PublicKey. // FromBytes loads the given byte slice into the PublicKey.
func (p *PublicKey) FromBytes(b []byte) error { func (p *PublicKey) FromBytes(b []byte) error {
e := binary.BigEndian.Uint64(b[:8]) e := binary.BigEndian.Uint32(b[:4])
p.E = int(e) p.E = int(e)
p.N = new(big.Int) p.N = new(big.Int)
p.N.SetBytes(b[8:]) p.N.SetBytes(b[4:])
return nil return nil
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment