forked from wumansgy/goEncrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getecckey.go
64 lines (58 loc) · 1.06 KB
/
getecckey.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
package goEncrypt
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"os"
"log"
)
/*
@Time : 2018/11/4 16:22
@Author : wuman
@File : GetECCKey
@Software: GoLand
*/
func init(){
log.SetFlags(log.Ldate|log.Lshortfile)
}
func GetEccKey()error{
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err!=nil{
return err
}
x509PrivateKey, err := x509.MarshalECPrivateKey(privateKey)
if err!=nil{
return err
}
block := pem.Block{
Type: eccPrivateKeyPrefix,
Bytes: x509PrivateKey,
}
file, err := os.Create(eccPrivateFileName)
if err!=nil{
return err
}
defer file.Close()
if err=pem.Encode(file, &block);err!=nil{
return err
}
x509PublicKey, err := x509.MarshalPKIXPublicKey(&privateKey.PublicKey)
if err!=nil {
return err
}
publicBlock := pem.Block{
Type: eccPublicKeyPrefix,
Bytes: x509PublicKey,
}
publicFile, err := os.Create(eccPublishFileName)
if err!=nil {
return err
}
defer publicFile.Close()
if err=pem.Encode(publicFile,&publicBlock);err!=nil{
return err
}
return nil
}