Skip to content

Commit

Permalink
Merge pull request #5 from theorlandog/content
Browse files Browse the repository at this point in the history
keygen config
  • Loading branch information
theorlandog authored Feb 8, 2023
2 parents d463548 + c8fa892 commit 0aeb6cb
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 5 deletions.
7 changes: 3 additions & 4 deletions cmd/easy_gpg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
// stdlib imports

"fmt"
"os"
"runtime"
Expand Down Expand Up @@ -63,7 +62,7 @@ func main() {
fmt.Println("easy_gpg only works on Windows, MacOS, and Linux")
}

// name, keylength, email = steps.CollectKeyInfo()
_, _, _ = steps.CollectKeyInfo()

name, keyType, keyLength, email, password, expireDays := steps.CollectKeyInfo()
configString := steps.GenerateKeyGenConfigString(name, keyType, keyLength, email, password, expireDays)
print(configString)
}
39 changes: 39 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
package config

const VERSION = "0.1"

const GENPARAMSTEMPLATE = `%echo Generating a OpenPGP key
{{if eq .Password ""}}
%no-protection
{{else}}
Passphrase: {{.Password}}
{{end}}
Key-Type: {{.KeyType}}
Key-Length: {{.KeyLength}}
# Key generated is a master key ("certificate")
Key-Usage: cert
# Parameters to generate subkeys
Subkey-Type: ELG-E
Subkey-Length: {{.KeyLength}}
Subkey-Usage: encrypt
Subkey-Type: ELG-S
Subkey-Length: {{.KeyLength}}
Subkey-Usage: sign
Subkey-Type: ELG-A
Subkey-Length: {{.KeyLength}}
Subkey-Usage: auth
# select a name and email address - neither has to be valid nor existing
Name-Real: {{.Name}}
Name-Email: {{.Email}}
# Set the key to expiration (0 is never)
Expire-Date: {{.ExpireDays}}
# Do a commit here, so that we can later print "done" :-)
%commit
%echo done
`
67 changes: 66 additions & 1 deletion pkg/steps/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ package steps
import (
// stdlib imports
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"os/user"
"strconv"
"strings"

"text/template"

// Local imports
"github.com/theorlandog/easy_gpg/pkg/config"
"github.com/theorlandog/easy_gpg/pkg/logging"
)

Expand Down Expand Up @@ -63,8 +68,10 @@ func WindowsCheckDependencies() int {
return 0
}

func CollectKeyInfo() (name string, keylength string, email string) {
func CollectKeyInfo() (name string, keyType string, keylength string, email string, password string, expireDays int) {
scanner := bufio.NewScanner(os.Stdin)

// Get current user for default name
currentUser, err := user.Current()
if err != nil {
log.Fatalf(err.Error())
Expand All @@ -76,15 +83,73 @@ func CollectKeyInfo() (name string, keylength string, email string) {
// Set the default to the OS username
name = currentUser.Name
}

fmt.Println("Enter your keytype. [RSA]:")
scanner.Scan()
keyType = strings.ToUpper(strings.TrimSpace(scanner.Text()))
if keyType == "" {
// Set default keylength
keyType = "RSA"
}

fmt.Println("Enter your keylength. [4096]:")
scanner.Scan()
keylength = strings.TrimSpace(scanner.Text())
if keylength == "" {
// Set default keylength
keylength = "4096"
}

fmt.Println("Enter your email. []:")
scanner.Scan()
email = strings.TrimSpace(scanner.Text())

fmt.Println("Enter a password for you key. []:")
scanner.Scan()
password = strings.TrimSpace(scanner.Text())

fmt.Println("How many days until the master key should expire? (0 is never). [0]:")
scanner.Scan()
expireDaysText := strings.TrimSpace(scanner.Text())
if expireDaysText == "" {
expireDays = 0
} else {
expireDays, err = strconv.Atoi(expireDaysText)
if err != nil {
// ... handle error
log.Fatalf(err.Error())
}
}

return
}

type KeyGenConfigParams struct {
Name string
Email string
KeyType string
KeyLength string
Password string
ExpireDays int
}

func GenerateKeyGenConfigString(name string, keyType string, keyLength string, email string, password string, expireDays int) (configstring string) {
var buf bytes.Buffer
keyGenConfigParams := KeyGenConfigParams{
Name: name,
Email: email,
KeyType: keyType,
KeyLength: keyLength,
Password: password,
ExpireDays: expireDays,
}
configTemplate := template.New("configTemplate")
configTemplate = template.Must(configTemplate.Parse(config.GENPARAMSTEMPLATE))

// template.Execute writes to an io.writer object
configTemplate.Execute(&buf, keyGenConfigParams)

configstring = buf.String()

return
}
1 change: 1 addition & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
// Local imports

"github.com/theorlandog/easy_gpg/pkg/logging"
)

Expand Down

0 comments on commit 0aeb6cb

Please sign in to comment.