Skip to content

Commit

Permalink
Initial framework and necessary commands
Browse files Browse the repository at this point in the history
Contains the initial code needed for creating a
CLI for encrypting and decrypting files to send
over unsecure connections.
  • Loading branch information
pp-johan committed Feb 2, 2020
1 parent f5e8664 commit 64e6f6e
Show file tree
Hide file tree
Showing 12 changed files with 660 additions and 1 deletion.
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
### Go ###
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

### Go Patch ###
/vendor/
/Godeps/

.vscode/
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
# secure
# secure
[![][languagego img]][languagego]

## Overview

`secure` is a CLI tool that enables sending files securly over an insecure connection (i.e. Slack). It's using public ssh-key for encrypting files and allows decryption using private key.

## Usage

To get help at any time for the tool or any command you can run:

```
secret --help # help pertaining to the tool itself
secret encrypt --help # help pertaining to the encrypt command
secret decrypt --help # help pertaining to the decrypt command
```

### Encrypt

`secure encrypt --file my_secret_file.txt --publicKey recipient_key.pub`

Files to send to recipient.

| Filename | Purpose |
| ------------------ | -------------------------------- |
| secret.txt.enc | Encrypted file containing secret |
| secret.key.enc | Encrypted symmetric key |

### Decrypt

`secure decrypt --file secret.txt.enc --secretKey secret.key.enc`

| Filename | Purpose |
| ------------------ | --------------------------------- |
| secret.txt | Unencrypted file from sender |


## Technical overview

1. Generating 32 bit symmetric key.
2. Encrypt file to transfer with the symmetric key.
3. Encrypt the symmetric key using recipients public ssh-key.

[languagego]:https://golang.org
[languagego img]:https://img.shields.io/badge/language-golang-77CDDD.svg?style=flat
78 changes: 78 additions & 0 deletions cmd/decrypt/decrypt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package decrypt

import (
"io/ioutil"
"os"
"fmt"

"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
"github.com/pypl-johan/secure/dec"
)
type decryption struct {
fileToDecrypt string
privateKey string
secretKey string
}

func NewCmd() *cobra.Command {
decrypt := decryption{}

cmd := &cobra.Command{
Use: "decrypt file using private key",
Short: "decrypt file using private key",
Long: "decrypt file using private key",
Run: func(cmd *cobra.Command, args []string) {
decrypt.run()
},
}

cmd.Flags().
StringVar(
&decrypt.fileToDecrypt,
"file",
"",
"file to decrypt",
)
cmd.Flags().
StringVar(
&decrypt.privateKey,
"privateKey",
os.Getenv("HOME") + "/.ssh/id_rsa",
"private key used to decrypt",
)
cmd.Flags().
StringVar(
&decrypt.secretKey,
"secretKey",
"",
"secret key to decrypt",
)

cmd.MarkFlagRequired("secretKey")
cmd.MarkFlagRequired("file")

return cmd
}

func (e *decryption) run() {
privateKey, _ := ioutil.ReadFile(e.privateKey)
secretKey, _ := ioutil.ReadFile(e.secretKey)
fileToDecrypt, _ := ioutil.ReadFile(e.fileToDecrypt)

fmt.Println("Enter password: ")
pkPassword, _ := terminal.ReadPassword(0)

unecryptedSecret := dec.DecryptUsingPrivateKey(secretKey , privateKey, string(pkPassword))

clearText := dec.DecryptUsingAsymmetricKey(fileToDecrypt, unecryptedSecret)

writeToFile(clearText, "secret.txt")
}

func writeToFile(data []byte, fileName string) {
err := ioutil.WriteFile(fileName, data, 0644)
if err != nil {
fmt.Errorf("%s", err)
}
}
76 changes: 76 additions & 0 deletions cmd/encrypt/encrypt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package encrypt

import (
"encoding/base64"
"fmt"
"io/ioutil"

"github.com/spf13/cobra"
"github.com/pypl-johan/secure/enc"
)

//var publicKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDFf4Ol/RCDrdX2iLkfUcJKqQ0qDtrlLhgszNzqnHK53mvZ+8hkN9gPey97MSo8okT9bF7hkpE4tNKuCA8t/+qoFz8MMIOo4J30r2frsQbmdGhLq4uVjHUDEZCCfwbcWjG/+QkMl43n+mWrbqeCAWD2p10a+mUud4gq5L5a3OG/k0jNkwKh7gbF7xRiw3v3k5WwnOmARPe70UbGo7Db6NXXsZFf54aeE05jVWQHNZPVAN5WXzqVbzEKxI2Eyy/yx3nzCCZTh03l/uFNCCmLrZnnT7YZ0sPABbPgkbWLfraBFvmoI9TTLZSA56gOx35qRdgtA9fE0Kqn1gZ6uVevMo/Gu3ACSkdrHszaNjbxtbSiDfirAoYL7rzTuWgsXi1hHE4yAU5xdtU63mF0Eeus3VCbdp/JDTbiCOJBoL0dIvGRel1Oq3NsgoUEBQ+85PKjElahwE4Ll0vnE83Z+lI8zJjF+27x3vEZDBmtzIbTq4HniaLgS+6Fi6CpQMsAukGp8CD6xMXg7HxXkVna1+Kuy0SzF7w8/AsvFYPQ/ZK3/IuXGyDQug/qh3Vc38xr2XHQek0KEwTxBC61/080/SuSlbjMBBR15DpjszU5jP+Ukx+ddwgwuVFn2TgsvEO50exmAiHIsnXSM1zGi/LKp+9yUuqlc+ERFvHU7X/fULAo6ClcFw== johan.karlgrund@izettle.com"

type encryption struct {
fileToEncrypt string
publicKey string
}

func NewCmd() *cobra.Command {
encrypt := encryption{}

cmd := &cobra.Command{
Use: "encrypt file using public key",
Short: "encrypt file using public key",
Long: "encrypt file using public key",
Run: func(cmd *cobra.Command, args []string) {
encrypt.run()
},
}

cmd.Flags().
StringVar(
&encrypt.fileToEncrypt,
"file",
"",
"file to encrypt",
)
cmd.Flags().
StringVar(
&encrypt.publicKey,
"publicKey",
"",
"public key used to encrypt",
)

cmd.MarkFlagRequired("publicKey")
cmd.MarkFlagRequired("file")

return cmd
}

func (e *encryption) run() {
publicKey, _ := ioutil.ReadFile(e.publicKey)
fileToEncrypt, _ := ioutil.ReadFile(e.fileToEncrypt)

//1
key, secretKey := enc.GenerateSecret(32)

//2
ciphertext := enc.EncryptUsingAsymmetricKey([]byte(fileToEncrypt), secretKey)

//3
encSecret := enc.EncryptUsingPublicKey(key, publicKey)
fmt.Printf("\nBase64 Secret: %s", base64.StdEncoding.EncodeToString(ciphertext))
fmt.Printf("\nBase64 Key: %s\n", base64.StdEncoding.EncodeToString(encSecret))

writeToFile(ciphertext, "secret.txt.enc")
writeToFile(encSecret, "secret.key.enc")
}

func writeToFile(data []byte, fileName string) {
err := ioutil.WriteFile(fileName, data, 0644)
if err != nil {
fmt.Errorf("%s", err)
}
}
97 changes: 97 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"fmt"
"os"
"github.com/spf13/cobra"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"

)


var cfgFile string


// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "secure",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.secure.yaml)")


// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}


// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Search config in home directory with name ".secure" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".secure")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}

32 changes: 32 additions & 0 deletions cmd/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package version

import (
"fmt"
"github.com/spf13/cobra"
)

var (
// BuildTime is a time label from when the binary was built.
BuildTime = "unset"
// Commit is the git hash from when the binary was built.
Commit = "unset"
// Version is the semantic version of the current build.
Version = "unset"
)

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "output version information",
Run: func(cmd *cobra.Command, args []string) {
printVersion()
},
}

return cmd
}

func printVersion() {
output := "version:\t%s\nhash:\t\t%s\nbuild_time:\t%s\n"
fmt.Printf(output, Version, Commit, BuildTime)
}
Loading

0 comments on commit 64e6f6e

Please sign in to comment.