Added configurable key type

This commit is contained in:
Magnus Åhall 2025-06-01 08:58:12 +02:00
parent 5856e9af69
commit 4426c5f9e8

30
pkg.go
View file

@ -22,6 +22,13 @@ import (
"time" "time"
) )
type KeyType int
const (
KeyEd25519 KeyType = iota
KeyEcDSA
)
type Manager struct { type Manager struct {
privKey crypto.PrivateKey privKey crypto.PrivateKey
PubKey crypto.PublicKey PubKey crypto.PublicKey
@ -29,16 +36,29 @@ type Manager struct {
Initialized bool Initialized bool
} }
func NewManager(private, public string, expireDays int) (mngr Manager, err error) { // {{{ func NewManager(keyType KeyType, private, public string, expireDays int) (mngr Manager, err error) { // {{{
mngr.privKey, err = jwt.ParseEdPrivateKeyFromPEM([]byte(private)) var errPriv, errPub error
if err != nil {
switch keyType {
case KeyEcDSA:
mngr.privKey, errPriv = jwt.ParseECPrivateKeyFromPEM([]byte(private))
mngr.PubKey, errPub = jwt.ParseECPublicKeyFromPEM([]byte(public))
case KeyEd25519:
mngr.privKey, errPriv = jwt.ParseEdPrivateKeyFromPEM([]byte(private))
mngr.PubKey, errPub = jwt.ParseEdPublicKeyFromPEM([]byte(public))
}
if errPriv != nil {
err = errPriv
return return
} }
mngr.PubKey, err = jwt.ParseEdPublicKeyFromPEM([]byte(public)) if errPub != nil {
if err != nil { err = errPub
return return
} }
mngr.ExpireDays = expireDays mngr.ExpireDays = expireDays
mngr.Initialized = true mngr.Initialized = true
return return