-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsa_processor.go
75 lines (68 loc) · 1.6 KB
/
rsa_processor.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
package pasargad
/**
* Thanks to phemmer for the gist:
* https://gist.github.com/phemmer/fea012d087ff65819645
*/
import (
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"encoding/xml"
"fmt"
"io/ioutil"
"math/big"
)
type XMLRSAKey struct {
Modulus string
Exponent string
P string
Q string
DP string
DQ string
InverseQ string
D string
}
func (m *PasargadPaymentAPI)b64d(str string) []byte {
decoded, err := base64.StdEncoding.DecodeString(str)
if err != nil {
fmt.Println(err)
}
return []byte(decoded)
}
func (m *PasargadPaymentAPI)b64bigint(str string) *big.Int {
bint := &big.Int{}
bint.SetBytes(m.b64d(str))
return bint
}
func (m *PasargadPaymentAPI) convertXmlToKey() (block []byte, err error) {
xmlbs, err := ioutil.ReadFile(m.certificationFile)
if err != nil {
fmt.Println(err)
}
if decoded, err := base64.StdEncoding.DecodeString(string(xmlbs)); err == nil {
xmlbs = decoded
}
xrk := XMLRSAKey{}
error := xml.Unmarshal(xmlbs, &xrk)
if error != nil {
fmt.Println(error)
}
key := &rsa.PrivateKey{
PublicKey: rsa.PublicKey{
N: m.b64bigint(xrk.Modulus),
E: int(m.b64bigint(xrk.Exponent).Int64()),
},
D: m.b64bigint(xrk.D),
Primes: []*big.Int{m.b64bigint(xrk.P), m.b64bigint(xrk.Q)},
Precomputed: rsa.PrecomputedValues{
Dp: m.b64bigint(xrk.DP),
Dq: m.b64bigint(xrk.DQ),
Qinv: m.b64bigint(xrk.InverseQ),
CRTValues: ([]rsa.CRTValue)(nil),
},
}
pemkey := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}
block = pem.EncodeToMemory(pemkey)
return block, nil
}