-
Notifications
You must be signed in to change notification settings - Fork 10
/
rsa.go
112 lines (101 loc) · 2.95 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2018 The cpchain authors
// This file is part of the cpchain library.
//
// The cpchain library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The cpchain library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the cpchain library. If not, see <http://www.gnu.org/licenses/>.
package rsakey
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"io/ioutil"
"os"
"bitbucket.org/cpchain/chain/commons/log"
)
func generateDerRsaKey(bits int) (*rsa.PublicKey, *rsa.PrivateKey, []byte, []byte, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, nil, nil, nil, err
}
priBytes := x509.MarshalPKCS1PrivateKey(privateKey)
// generate public key
publicKey := &privateKey.PublicKey
pubBytes := x509.MarshalPKCS1PublicKey(publicKey)
return publicKey, privateKey, pubBytes, priBytes, err
}
func generateRsaKey(privateKeyPath string, bits int) error {
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return err
}
priBytes := x509.MarshalPKCS1PrivateKey(privateKey)
block := &pem.Block{
Type: "private key",
Bytes: priBytes,
}
file, err := os.Create(privateKeyPath)
if err != nil {
return err
}
defer file.Close()
err = pem.Encode(file, block)
if err != nil {
return err
}
return err
}
func loadRsaKey(priPath string) (*rsa.PublicKey, *rsa.PrivateKey, []byte, []byte, error) {
priBlock, err := loadKeyFile(priPath)
if err != nil {
return nil, nil, nil, nil, err
}
privateKey, err := bytes2PrivateKey(priBlock.Bytes)
if err != nil {
return nil, nil, nil, nil, err
}
publicKey := &privateKey.PublicKey
pubBytes := x509.MarshalPKCS1PublicKey(publicKey)
return publicKey, privateKey, pubBytes, priBlock.Bytes, nil
}
func bytes2PrivateKey(bs []byte) (*rsa.PrivateKey, error) {
privateKey, err := x509.ParsePKCS1PrivateKey(bs)
return privateKey, err
}
func bytes2PublicKey(bs []byte) (*rsa.PublicKey, error) {
publicKey, err := x509.ParsePKCS1PublicKey(bs)
if err != nil {
return nil, err
}
return publicKey, err
}
func loadKeyFile(path string) (*pem.Block, error) {
keyBytes, pubErr := LoadFile(path)
if pubErr != nil {
return nil, errors.New("load key file [" + path + "] failed")
}
block, _ := pem.Decode(keyBytes)
if block == nil {
return nil, errors.New("decode key error")
}
return block, pubErr
}
func LoadFile(path string) ([]byte, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
log.Info("file ", path, " not found.")
return nil, err
}
return b, nil
}