diff options
Diffstat (limited to 'init.go')
| -rw-r--r-- | init.go | 41 |
1 files changed, 41 insertions, 0 deletions
@@ -0,0 +1,41 @@ +package main + +import ( + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "encoding/pem" + "fmt" +) + +func init_genkey() (*pem.Block, *pem.Block) { + privateKey, err := rsa.GenerateKey(rand.Reader, 4096) + if err != nil { + panic(fmt.Errorf("不能生成密钥: %v", err)) + } + + privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey) + privateKeyBlock := &pem.Block{ + Type: "RSA PRIVATE KEY", + Bytes: privateKeyBytes, + } + + publicKeyBytes, err := x509.MarshalPKIXPublicKey(&privateKey.PublicKey) + if err != nil { + panic(fmt.Errorf("不能解析公钥: %v", err)) + } + + publicKeyBlock := &pem.Block{ + Type: "PUBLIC KEY", + Bytes: publicKeyBytes, + } + + return privateKeyBlock, publicKeyBlock +} + +var privatePEM []byte + +func init() { + privateKeyBlock, _ := init_genkey() + privatePEM = pem.EncodeToMemory(privateKeyBlock) +} |
