Refactored config system again
All checks were successful
Build (artifact) / build (push) Successful in 27s
All checks were successful
Build (artifact) / build (push) Successful in 27s
This commit is contained in:
117
app/cryptr/main.go
Normal file
117
app/cryptr/main.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.nevets.tech/Steven/certman/app"
|
||||
"git.nevets.tech/Steven/certman/common"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func main() {
|
||||
rootCmd := &cobra.Command{
|
||||
Use: "cryptr",
|
||||
Short: "cryptr is a tool for encrypting and decrypting files",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return cmd.Help()
|
||||
},
|
||||
}
|
||||
rootCmd.AddCommand(&cobra.Command{
|
||||
Use: "key-gen",
|
||||
Short: "Generates encryption key",
|
||||
Run: app.GenKeyCmd,
|
||||
})
|
||||
|
||||
var (
|
||||
keyUTF8 bool
|
||||
keyFile string
|
||||
)
|
||||
encryptCmd := &cobra.Command{
|
||||
Use: "encrypt",
|
||||
Short: "encrypt a file",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
key, err := getKey(keyFile, keyUTF8)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(key)
|
||||
|
||||
inFilePath := args[0]
|
||||
outFilePath := args[1]
|
||||
return common.EncryptFileXChaCha(key, inFilePath, outFilePath, nil)
|
||||
},
|
||||
}
|
||||
encryptCmd.Flags().BoolVar(&keyUTF8, "utf8", false, "Use UTF-8 encoded key")
|
||||
encryptCmd.Flags().StringVar(&keyFile, "key", "", "File containing key")
|
||||
rootCmd.AddCommand(encryptCmd)
|
||||
|
||||
decryptCmd := &cobra.Command{
|
||||
Use: "decrypt",
|
||||
Short: "decrypt a file",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
key, err := getKey(keyFile, keyUTF8)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
inFilePath := args[0]
|
||||
outFilePath := args[1]
|
||||
inFile, err := os.ReadFile(inFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return common.DecryptFileFromBytes(key, inFile, outFilePath, nil)
|
||||
},
|
||||
}
|
||||
decryptCmd.Flags().BoolVar(&keyUTF8, "utf8", false, "Use UTF-8 encoded key")
|
||||
decryptCmd.Flags().StringVar(&keyFile, "key", "", "File containing key")
|
||||
rootCmd.AddCommand(decryptCmd)
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeKey(b []byte) []byte {
|
||||
out := make([]byte, 32)
|
||||
copy(out, b)
|
||||
return out
|
||||
}
|
||||
|
||||
func getKey(keyFile string, keyUTF8 bool) (string, error) {
|
||||
var raw []byte
|
||||
if keyFile != "" {
|
||||
b, err := os.ReadFile(keyFile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
raw = b
|
||||
} else {
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
fmt.Print("Enter Key: ")
|
||||
if !scanner.Scan() {
|
||||
if err := scanner.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
raw = scanner.Bytes()
|
||||
}
|
||||
|
||||
var keyBytes []byte
|
||||
if keyUTF8 {
|
||||
keyBytes = normalizeKey(bytes.TrimSpace(raw))
|
||||
} else {
|
||||
decoded, err := base64.StdEncoding.DecodeString(string(bytes.TrimSpace(raw)))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("key is not valid base64: %w", err)
|
||||
}
|
||||
keyBytes = normalizeKey(decoded)
|
||||
}
|
||||
|
||||
return base64.StdEncoding.EncodeToString(keyBytes), nil
|
||||
}
|
||||
Reference in New Issue
Block a user