diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2025-04-14 17:13:30 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2025-04-14 17:13:30 -0400 |
| commit | 9d3164de326e24348fa5e86d19b3f4e6e961f2ce (patch) | |
| tree | 80bfc5197601a83f5b66a567d676c7b6b6fa5c9b | |
| parent | 1373e3fb7eb9e504689cf629c15a713c157aae8d (diff) | |
| download | hose-9d3164de326e24348fa5e86d19b3f4e6e961f2ce.zip | |
load keypair from disc
| -rw-r--r-- | key/key.go | 35 |
1 files changed, 27 insertions, 8 deletions
@@ -7,28 +7,47 @@ import ( "os" ) +// LoadKeypair reads the public and private keys from disc, +// or generates a new keypair if it does not already exist. +func LoadKeypair() (public, private [32]byte, err error) { + // Generate a keypair if it doesn't already exist. + err = generateIfNoExist() + if err != nil { + return + } + + public, err = loadKey(pubKeyFile) + if err != nil { + return + } + + private, err = loadKey(privKeyFile) + + return +} + // LoadPublicKey reads the public key from disc, or generates a new keypair // if it does not already exist. func LoadPublicKey() ([32]byte, error) { - // Generate a keypair if it doesn't already exist. - if err := generateIfNoExist(); err != nil { - return [32]byte{}, err - } + return loadKey(pubKeyFile) +} - // Open key file. - f, err := os.Open(pubKeyFile) +// loadKey reads a key (public or private) from the specified file. +func loadKey(filename string) ([32]byte, error) { + // Open file. + f, err := os.Open(filename) if err != nil { return [32]byte{}, err } defer f.Close() - // Read key. + // Read key from file. buf, err := io.ReadAll(f) if err != nil { return [32]byte{}, err } - // Decode. + // Decode key. var key [32]byte if hex.DecodedLen(len(buf)) != len(key) { return [32]byte{}, fmt.Errorf("malformed key: expected %d bytes; got %d", |