-
Notifications
You must be signed in to change notification settings - Fork 29
/
hmac_using_sha.go
46 lines (35 loc) · 1.01 KB
/
hmac_using_sha.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
package jose
import (
"crypto/hmac"
"errors"
)
func init() {
RegisterJws(&HmacUsingSha{keySizeBits: 256})
RegisterJws(&HmacUsingSha{keySizeBits: 384})
RegisterJws(&HmacUsingSha{keySizeBits: 512})
}
// HMAC with SHA signing algorithm implementation
type HmacUsingSha struct{
keySizeBits int
}
func (alg *HmacUsingSha) Name() string {
switch alg.keySizeBits {
case 256: return HS256
case 384: return HS384
default: return HS512
}
}
func (alg *HmacUsingSha) Verify(securedInput, signature []byte, key interface{}) error {
actualSig,_ := alg.Sign(securedInput, key)
if !hmac.Equal(signature, actualSig) {
return errors.New("HmacUsingSha.Verify(): Signature is invalid")
}
return nil
}
func (alg *HmacUsingSha) Sign(securedInput []byte, key interface{}) (signature []byte, err error) {
//TODO: assert min key size
if pubKey,ok:=key.([]byte); ok {
return calculateHmac(alg.keySizeBits, securedInput, pubKey),nil
}
return nil,errors.New("HmacUsingSha.Sign(): expects key to be '[]byte' array")
}