Debian Codegraph @

rsa_using_sha.go

raw symbols SCIP
1 package jose
2
3 import (
4 "crypto/rand"
5 "crypto/rsa"
6 "errors"
7 )
8
9 func init() {
10 RegisterJws(&RsaUsingSha{keySizeBits: 256})
11 RegisterJws(&RsaUsingSha{keySizeBits: 384})
12 RegisterJws(&RsaUsingSha{keySizeBits: 512})
13 }
14
15 // RSA using SHA signature algorithm implementation
16 type RsaUsingSha struct{
17 keySizeBits int
18 }
19
20 func (alg *RsaUsingSha) Name() string {
21 switch alg.keySizeBits {
22 case 256: return RS256
23 case 384: return RS384
24 default: return RS512
25 }
26 }
27
28 func (alg *RsaUsingSha) Verify(securedInput, signature []byte, key interface{}) error {
29
30 if pubKey,ok:=key.(*rsa.PublicKey);ok {
31 return rsa.VerifyPKCS1v15(pubKey, hashFunc(alg.keySizeBits), sha(alg.keySizeBits, securedInput), signature)
32 }
33
34 return errors.New("RsaUsingSha.Verify(): expects key to be '*rsa.PublicKey'")
35 }
36
37 func (alg *RsaUsingSha) Sign(securedInput []byte, key interface{}) (signature []byte, err error) {
38
39 if privKey,ok:=key.(*rsa.PrivateKey);ok {
40 return rsa.SignPKCS1v15(rand.Reader, privKey, hashFunc(alg.keySizeBits), sha(alg.keySizeBits, securedInput))
41 }
42
43 return nil,errors.New("RsaUsingSha.Sign(): expects key to be '*rsa.PrivateKey'")
44 }
45
46 func sha(keySizeBits int, input []byte) (hash []byte) {
47 hasher := hashAlg(keySizeBits)
48 hasher.Write(input)
49 return hasher.Sum(nil)
50 }