add string methods where they needed, add method to get list of keys from key store

This commit is contained in:
mykola2312 2024-12-31 18:24:48 +02:00
parent 3066fbf83f
commit 3172def440
4 changed files with 36 additions and 6 deletions

View file

@ -2,6 +2,7 @@ package crypto
import (
"crypto/rand"
"fmt"
"lux/proto"
"os"
)
@ -66,6 +67,10 @@ func (key *LuxKey) Write(wd *proto.LuxBuffer) {
wd.WriteBytes(key.IV)
}
func (key *LuxKey) String() string {
return fmt.Sprintf("%s %s", key.Type.String(), key.Id.String())
}
const LUX_KEYSTORE_FILEMODE = os.FileMode(int(0600))
type LuxKeyStore struct {
@ -111,12 +116,9 @@ func (ks *LuxKeyStore) Save() error {
return os.WriteFile(ks.filePath, wd.AllBytes(), LUX_KEYSTORE_FILEMODE)
}
func (ks *LuxKeyStore) Get(id proto.LuxID) *LuxKey {
if key, ok := ks.keys[id]; ok {
return &key
} else {
return nil
}
func (ks *LuxKeyStore) Get(id proto.LuxID) (LuxKey, bool) {
key, ok := ks.keys[id]
return key, ok
}
func (ks *LuxKeyStore) Put(key LuxKey) error {
@ -127,3 +129,14 @@ func (ks *LuxKeyStore) Put(key LuxKey) error {
return nil
}
func (ks *LuxKeyStore) Keys() []LuxKey {
values := make([]LuxKey, len(ks.keys))
i := 0
for _, value := range ks.keys {
values[i] = value
}
return values
}

View file

@ -45,5 +45,7 @@ func LuxHostEntry(configPath string) error {
log.Fatal(err)
}
fmt.Println(key.String())
return nil
}

View file

@ -28,3 +28,7 @@ func (id *LuxID) Read(rd *LuxBuffer) error {
func (id *LuxID) Write(wd *LuxBuffer) {
wd.WriteBytes(id.UUID[:])
}
func (id *LuxID) String() string {
return id.UUID.String()
}

View file

@ -21,3 +21,14 @@ func (luxType *LuxType) Read(rd *LuxBuffer) error {
func (luxType *LuxType) Write(wd *LuxBuffer) {
wd.WriteUint16(uint16(*luxType))
}
func (luxType *LuxType) String() string {
switch *luxType {
case LuxTypeHost:
return "host"
case LuxTypeNode:
return "node"
default:
return "unknown"
}
}