mirror of
https://codeberg.org/scottslowe/learning-tools.git
synced 2026-03-11 09:04:37 +00:00
Add Golang code for generating SSH key
Add Golang code example for generating SSH key along with associated documentation. Signed-off-by: Scott S. Lowe <scott.lowe@scottlowe.org>
This commit is contained in:
parent
3ce33c07ff
commit
50481c8976
4 changed files with 68 additions and 0 deletions
7
golang/README.md
Normal file
7
golang/README.md
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# Learning Tools: Golang
|
||||
|
||||
The contents of this folder are focused on improving knowledge in programming with Golang.
|
||||
|
||||
## Contents
|
||||
|
||||
**generate-ssh-key**: This folder provides some code for generating SSH keys in Golang.
|
||||
5
golang/generate-ssh-key/README.md
Normal file
5
golang/generate-ssh-key/README.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# Generate an SSH Key in Golang
|
||||
|
||||
The code in this directory was heavily derived from [this StackOverflow page](https://stackoverflow.com/questions/21151714/go-generate-an-ssh-public-key).
|
||||
|
||||
This was written and tested with Golang 1.18 on an ARM64-based Mac, but it should run fine on any platform or architecture supported by Go.
|
||||
7
golang/generate-ssh-key/go.mod
Normal file
7
golang/generate-ssh-key/go.mod
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
module go-sshkey
|
||||
|
||||
go 1.18
|
||||
|
||||
require golang.org/x/crypto v0.7.0
|
||||
|
||||
require golang.org/x/sys v0.6.0 // indirect
|
||||
49
golang/generate-ssh-key/main.go
Normal file
49
golang/generate-ssh-key/main.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Generate a 4096-bit RSA private key
|
||||
privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
|
||||
if err != nil {
|
||||
log.Printf("error encountered: %s", err.Error())
|
||||
}
|
||||
privateKeyDer := x509.MarshalPKCS1PrivateKey(privateKey)
|
||||
privateKeyBlock := pem.Block{
|
||||
Type: "RSA PRIVATE KEY",
|
||||
Headers: nil,
|
||||
Bytes: privateKeyDer,
|
||||
}
|
||||
privateKeyPem := string(pem.EncodeToMemory(&privateKeyBlock))
|
||||
fmt.Println(string(privateKeyPem))
|
||||
|
||||
// Generate a matching public key
|
||||
publicKey := privateKey.PublicKey
|
||||
publicKeyDer, err := x509.MarshalPKIXPublicKey(&publicKey)
|
||||
if err != nil {
|
||||
log.Printf("error encountered: %s", err.Error())
|
||||
}
|
||||
publicKeyBlock := pem.Block{
|
||||
Type: "PUBLIC KEY",
|
||||
Headers: nil,
|
||||
Bytes: publicKeyDer,
|
||||
}
|
||||
publicKeyPem := string(pem.EncodeToMemory(&publicKeyBlock))
|
||||
fmt.Println(string(publicKeyPem))
|
||||
|
||||
// Generate the public key in OpenSSH authorized_keys format
|
||||
sshPubKey, err := ssh.NewPublicKey(&publicKey)
|
||||
if err != nil {
|
||||
log.Printf("error encountered: %s", err.Error())
|
||||
}
|
||||
fmt.Println(string(ssh.MarshalAuthorizedKey(sshPubKey)))
|
||||
}
|
||||
Loading…
Reference in a new issue