-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
31 lines (27 loc) · 907 Bytes
/
decode.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
package rsax
import (
"crypto/x509"
"encoding/hex"
"encoding/pem"
"github.com/go-leo/gox/cryptox/base64x"
)
func DecodeKeyHex(data []byte) (string, string, error) {
block, _ := pem.Decode(data)
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return "", "", err
}
privateKeyStr := hex.EncodeToString(x509.MarshalPKCS1PrivateKey(privateKey))
publicKeyStr := hex.EncodeToString(x509.MarshalPKCS1PublicKey(&privateKey.PublicKey))
return privateKeyStr, publicKeyStr, nil
}
func DecodeKeyBase64(data []byte) (string, string, error) {
block, _ := pem.Decode(data)
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return "", "", err
}
privateKeyStr := base64x.StdEncode(x509.MarshalPKCS1PrivateKey(privateKey))
publicKeyStr := base64x.StdEncode(x509.MarshalPKCS1PublicKey(&privateKey.PublicKey))
return privateKeyStr, publicKeyStr, nil
}