diff options
| author | runoneall <runoneall@serv00.net> | 2025-09-11 11:19:12 +0200 |
|---|---|---|
| committer | runoneall <runoneall@serv00.net> | 2025-09-11 11:19:12 +0200 |
| commit | 59c5a6c4950d340d248431b84452d86329024d76 (patch) | |
| tree | 71d5e243090a319f35c6b35046e81ddf7271dc35 /init.go | |
| parent | 1ac4476d0a226c319200720a6ff63a5471bdc104 (diff) | |
add ssh server
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) +} |
