-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathrsa_using_sha.go
50 lines (39 loc) · 1.25 KB
/
rsa_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
47
48
49
50
package jose
import (
"crypto/rand"
"crypto/rsa"
"errors"
)
func init() {
RegisterJws(&RsaUsingSha{keySizeBits: 256})
RegisterJws(&RsaUsingSha{keySizeBits: 384})
RegisterJws(&RsaUsingSha{keySizeBits: 512})
}
// RSA using SHA signature algorithm implementation
type RsaUsingSha struct{
keySizeBits int
}
func (alg *RsaUsingSha) Name() string {
switch alg.keySizeBits {
case 256: return RS256
case 384: return RS384
default: return RS512
}
}
func (alg *RsaUsingSha) Verify(securedInput, signature []byte, key interface{}) error {
if pubKey,ok:=key.(*rsa.PublicKey);ok {
return rsa.VerifyPKCS1v15(pubKey, hashFunc(alg.keySizeBits), sha(alg.keySizeBits, securedInput), signature)
}
return errors.New("RsaUsingSha.Verify(): expects key to be '*rsa.PublicKey'")
}
func (alg *RsaUsingSha) Sign(securedInput []byte, key interface{}) (signature []byte, err error) {
if privKey,ok:=key.(*rsa.PrivateKey);ok {
return rsa.SignPKCS1v15(rand.Reader, privKey, hashFunc(alg.keySizeBits), sha(alg.keySizeBits, securedInput))
}
return nil,errors.New("RsaUsingSha.Sign(): expects key to be '*rsa.PrivateKey'")
}
func sha(keySizeBits int, input []byte) (hash []byte) {
hasher := hashAlg(keySizeBits)
hasher.Write(input)
return hasher.Sum(nil)
}