From 59c5a6c4950d340d248431b84452d86329024d76 Mon Sep 17 00:00:00 2001 From: runoneall Date: Thu, 11 Sep 2025 17:19:12 +0800 Subject: add ssh server --- init.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 init.go (limited to 'init.go') diff --git a/init.go b/init.go new file mode 100644 index 0000000..37ed53a --- /dev/null +++ b/init.go @@ -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) +} -- cgit v1.2.3