implement key structure

This commit is contained in:
mykola2312 2024-12-29 13:27:06 +02:00
parent ee24948b73
commit 3a7f25020a
4 changed files with 58 additions and 5 deletions

View file

@ -3,19 +3,17 @@ package crypto
import (
"crypto/rand"
"lux/proto"
"github.com/google/uuid"
)
type LuxKey struct {
Type proto.LuxType
Id uuid.UUID
Id proto.LuxID
Key []byte
}
func (key *LuxKey) NewLuxKey(keyType proto.LuxType) error {
key.Type = keyType
key.Id = uuid.New()
key.Id = proto.NewLuxID()
key.Key = make([]byte, 32)
if _, err := rand.Read(key.Key); err != nil {
@ -24,3 +22,26 @@ func (key *LuxKey) NewLuxKey(keyType proto.LuxType) error {
return nil
}
}
func (key *LuxKey) Read(rd *proto.LuxBuffer) error {
if err := key.Type.Read(rd); err != nil {
return err
}
if err := key.Id.Read(rd); err != nil {
return err
}
_key, err := rd.ReadNext(32)
if err != nil {
return err
}
key.Key = _key
return nil
}
func (key *LuxKey) Write(wd *proto.LuxBuffer) {
key.Type.Write(wd)
key.Id.Write(wd)
wd.WriteBytes(key.Key)
}

View file

@ -2,5 +2,5 @@ package proto
type LuxData interface {
Read(rd *LuxBuffer) error
Write(wd *LuxBuffer) error
Write(wd *LuxBuffer)
}

28
proto/lux_id.go Normal file
View file

@ -0,0 +1,28 @@
package proto
import "github.com/google/uuid"
type LuxID struct{ uuid.UUID }
func NewLuxID() LuxID {
return LuxID{uuid.New()}
}
func (id *LuxID) Read(rd *LuxBuffer) error {
bytes, err := rd.ReadNext(16)
if err != nil {
return err
}
_id, err := uuid.FromBytes(bytes)
if err != nil {
return err
}
id.UUID = _id
return nil
}
func (id *LuxID) Write(wd *LuxBuffer) {
wd.WriteBytes(id.UUID[:])
}

View file

@ -15,3 +15,7 @@ func (luxType *LuxType) Read(rd *LuxBuffer) error {
return nil
}
}
func (luxType *LuxType) Write(wd *LuxBuffer) {
wd.WriteUint16(uint16(*luxType))
}