summaryrefslogtreecommitdiff
path: root/init.go
diff options
context:
space:
mode:
authorrunoneall <runoneall@serv00.net>2025-09-11 11:19:12 +0200
committerrunoneall <runoneall@serv00.net>2025-09-11 11:19:12 +0200
commit59c5a6c4950d340d248431b84452d86329024d76 (patch)
tree71d5e243090a319f35c6b35046e81ddf7271dc35 /init.go
parent1ac4476d0a226c319200720a6ff63a5471bdc104 (diff)
add ssh server
Diffstat (limited to 'init.go')
-rw-r--r--init.go41
1 files changed, 41 insertions, 0 deletions
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)
+}