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"
|
||||
)
|
||||
|
||||
type KeyType int
|
||||
|
||||
const (
|
||||
KeyEd25519 KeyType = iota
|
||||
KeyEcDSA
|
||||
)
|
||||
|
||||
type Manager struct {
|
||||
privKey crypto.PrivateKey
|
||||
PubKey crypto.PublicKey
|
||||
|
@ -29,16 +36,29 @@ type Manager struct {
|
|||
Initialized bool
|
||||
}
|
||||
|
||||
func NewManager(private, public string, expireDays int) (mngr Manager, err error) { // {{{
|
||||
mngr.privKey, err = jwt.ParseEdPrivateKeyFromPEM([]byte(private))
|
||||
if err != nil {
|
||||
func NewManager(keyType KeyType, private, public string, expireDays int) (mngr Manager, err error) { // {{{
|
||||
var errPriv, errPub error
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
mngr.PubKey, err = jwt.ParseEdPublicKeyFromPEM([]byte(public))
|
||||
if err != nil {
|
||||
if errPub != nil {
|
||||
err = errPub
|
||||
return
|
||||
}
|
||||
|
||||
mngr.ExpireDays = expireDays
|
||||
mngr.Initialized = true
|
||||
return
|
||||
|
|
Loading…
Add table
Reference in a new issue