forked from cristalhq/jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo.go
81 lines (63 loc) · 1.6 KB
/
algo.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package jwt
import (
"crypto"
_ "crypto/sha256" // to register a hash
_ "crypto/sha512" // to register a hash
)
// Signer is used to sign tokens.
type Signer interface {
Algorithm() Algorithm
SignSize() int
Sign(payload []byte) ([]byte, error)
}
// Verifier is used to verify tokens.
type Verifier interface {
Algorithm() Algorithm
Verify(payload, signature []byte) error
}
// Algorithm for signing and verifying.
type Algorithm string
func (a Algorithm) String() string { return string(a) }
// keySize of the algorithm's key (if exist). Is similar to Signer.SignSize.
func (a Algorithm) keySize() int { return algsKeySize[a] }
var algsKeySize = map[Algorithm]int{
// for EdDSA private and public key have different sizes, so 0
// for HS there is no limits for key size, so 0
RS256: 256,
RS384: 384,
RS512: 512,
ES256: 64,
ES384: 96,
ES512: 132,
PS256: 256,
PS384: 384,
PS512: 512,
}
// Algorithm names for signing and verifying.
const (
EdDSA Algorithm = "EdDSA"
HS256 Algorithm = "HS256"
HS384 Algorithm = "HS384"
HS512 Algorithm = "HS512"
RS256 Algorithm = "RS256"
RS384 Algorithm = "RS384"
RS512 Algorithm = "RS512"
ES256 Algorithm = "ES256"
ES384 Algorithm = "ES384"
ES512 Algorithm = "ES512"
PS256 Algorithm = "PS256"
PS384 Algorithm = "PS384"
PS512 Algorithm = "PS512"
)
func hashPayload(hash crypto.Hash, payload []byte) ([]byte, error) {
hasher := hash.New()
_, err := hasher.Write(payload)
if err != nil {
return nil, err
}
signed := hasher.Sum(nil)
return signed, nil
}
func constTimeAlgEqual(a, b Algorithm) bool {
return constTimeEqual(a.String(), b.String())
}