-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyinfo.go
58 lines (52 loc) · 1.11 KB
/
keyinfo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package certutil
import (
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"github.com/pkg/errors"
"gopkg.in/square/go-jose.v2"
)
// KeyInfo provides information about the key
type KeyInfo struct {
KeySize int
Type string
IsPrivate bool
Key interface{}
}
// NewKeyInfo returns *SignerInfo
func NewKeyInfo(k interface{}) (*KeyInfo, error) {
ki := &KeyInfo{Key: k}
var pubKey crypto.PublicKey
// find the Public
switch typ := k.(type) {
case *rsa.PrivateKey:
ki.KeySize = typ.N.BitLen()
ki.IsPrivate = true
ki.Type = "RSA"
return ki, nil
case *ecdsa.PrivateKey:
ki.Type = "ECDSA"
ki.IsPrivate = true
ki.KeySize = typ.Curve.Params().BitSize
return ki, nil
case crypto.Signer:
pubKey = typ.Public()
case crypto.Decrypter:
pubKey = typ.Public()
case *jose.JSONWebKey:
return NewKeyInfo(typ.Key)
default:
pubKey = k
}
switch typ := pubKey.(type) {
case *rsa.PublicKey:
ki.KeySize = typ.N.BitLen()
ki.Type = "RSA"
case *ecdsa.PublicKey:
ki.Type = "ECDSA"
ki.KeySize = typ.Curve.Params().BitSize
default:
return nil, errors.Errorf("key not supported: %T", typ)
}
return ki, nil
}