Added configurable key type
This commit is contained in:
parent
5856e9af69
commit
4426c5f9e8
1 changed files with 25 additions and 5 deletions
30
pkg.go
30
pkg.go
|
@ -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
|
||||||
|
|
Loading…
Add table
Reference in a new issue