/
rsa.go
68 lines (56 loc) · 1.36 KB
/
rsa.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
// Copyright 2021 The msgsig Authors. All rights reserved.
// Use of this source code is governed by the Apache License,
// Version 2.0, that can be found in the LICENSE file.
package msgsig
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"fmt"
"hash"
)
type rsaPssSigningAlgorithm struct {
keyId string
privKey *rsa.PrivateKey
hashOpt crypto.Hash
hash hash.Hash
}
func (s *rsaPssSigningAlgorithm) KeyId() string {
return s.keyId
}
func (s *rsaPssSigningAlgorithm) AlgName() AlgorithmName {
return AlgorithmRsaPssSha512
}
func (s *rsaPssSigningAlgorithm) Sign(in []byte) ([]byte, error) {
fmt.Printf("signing: `%s`\n", string(in))
defer s.hash.Reset()
s.hash.Write(in)
digest := s.hash.Sum(nil)
sig, err := rsa.SignPSS(rand.Reader, s.privKey, s.hashOpt, digest, &rsa.PSSOptions{
SaltLength: 64,
Hash: s.hashOpt,
})
if err != nil {
return nil, err
}
return sig, nil
}
type rsaV15SigningAlgorithm struct {
algName AlgorithmName
keyId string
privKey crypto.Signer
hashOpt crypto.Hash
hash hash.Hash
}
func (s *rsaV15SigningAlgorithm) KeyId() string {
return s.keyId
}
func (s *rsaV15SigningAlgorithm) AlgName() AlgorithmName {
return s.algName
}
func (s *rsaV15SigningAlgorithm) Sign(in []byte) ([]byte, error) {
defer s.hash.Reset()
s.hash.Write(in)
digest := s.hash.Sum(nil)
return s.privKey.Sign(rand.Reader, digest, s.hashOpt)
}