|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "encoding/base64" |
| 6 | + "fmt" |
| 7 | + "github.com/spf13/cobra" |
| 8 | + "golang.org/x/crypto/nacl/box" |
| 9 | + "io/ioutil" |
| 10 | + "os" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + versionString string |
| 15 | +) |
| 16 | + |
| 17 | +func main() { |
| 18 | + rootCmd := &cobra.Command{ |
| 19 | + Use: "crypta2", |
| 20 | + Short: "Just another cryptographic tool", |
| 21 | + SilenceErrors: true, |
| 22 | + Version: versionString, |
| 23 | + } |
| 24 | + rootCmd.AddCommand(makeGenKeyCmd()) |
| 25 | + rootCmd.AddCommand(makeEncryptCmd()) |
| 26 | + rootCmd.AddCommand(makeDecryptCmd()) |
| 27 | + if err := rootCmd.Execute(); err != nil { |
| 28 | + _, _ = fmt.Fprintln(os.Stderr, err) |
| 29 | + os.Exit(1) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func makeGenKeyCmd() *cobra.Command { |
| 34 | + cmdGenKey := &cobra.Command{ |
| 35 | + Use: "genkey filename", |
| 36 | + Short: "Generates a key pair", |
| 37 | + Long: "Generates a key pair and save as filename.pub and filename.pvt to working directory", |
| 38 | + Args: cobra.ExactArgs(1), |
| 39 | + SilenceUsage: true, |
| 40 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 41 | + fileNamePub := fmt.Sprintf("%s.pub", args[0]) |
| 42 | + fileNamePvt := fmt.Sprintf("%s.pvt", args[0]) |
| 43 | + |
| 44 | + if _, err := os.Stat(fileNamePvt); err == nil { |
| 45 | + return fmt.Errorf("private key file %q already exists", fileNamePvt) |
| 46 | + } |
| 47 | + |
| 48 | + if _, err := os.Stat(fileNamePub); err == nil { |
| 49 | + return fmt.Errorf("public key file %q already exists", fileNamePub) |
| 50 | + } |
| 51 | + |
| 52 | + pubKey, pvtKey, err := box.GenerateKey(rand.Reader) |
| 53 | + if err != nil { |
| 54 | + return fmt.Errorf("cannot generate key pair: %v", err) |
| 55 | + } |
| 56 | + pubEncoded := []byte(base64.StdEncoding.EncodeToString(pubKey[:])) |
| 57 | + pvtEncoded := []byte(base64.StdEncoding.EncodeToString(pvtKey[:])) |
| 58 | + fmt.Printf("Writing public key %q\n", fileNamePub) |
| 59 | + |
| 60 | + err = ioutil.WriteFile(fileNamePub, pubEncoded, 0644) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("cannot write public key %q: %v", fileNamePub, err) |
| 63 | + } |
| 64 | + |
| 65 | + fmt.Printf("Writing private key %q\n", fileNamePvt) |
| 66 | + err = ioutil.WriteFile(fileNamePvt, pvtEncoded, 0600) |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("cannot write private key %q: %v", fileNamePvt, err) |
| 69 | + } |
| 70 | + return nil |
| 71 | + }, |
| 72 | + } |
| 73 | + return cmdGenKey |
| 74 | +} |
| 75 | + |
| 76 | +func makeEncryptCmd() *cobra.Command { |
| 77 | + var input string |
| 78 | + var pubKeyFile string |
| 79 | + cmdEncrypt := &cobra.Command{ |
| 80 | + Use: "encrypt [(-f|--file=) input] (-p|--public-key=) public-key file", |
| 81 | + Short: "Encrypt input with the given public key", |
| 82 | + Long: "Encrypt input file or stdin with the given public key", |
| 83 | + Args: cobra.ExactArgs(0), |
| 84 | + SilenceUsage: true, |
| 85 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 86 | + inBytes, err := readInput(input) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("cannot read input: %v", err) |
| 89 | + } |
| 90 | + pubEnc, err := ioutil.ReadFile(pubKeyFile) |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("cannot read public key file %q: %v", pubKeyFile, err) |
| 93 | + } |
| 94 | + pubBytes, err := base64.StdEncoding.DecodeString(string(pubEnc)) |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("cannot decode public key: %v", err) |
| 97 | + } |
| 98 | + |
| 99 | + var pubKey32 [32]byte |
| 100 | + copy(pubKey32[:], pubBytes) |
| 101 | + |
| 102 | + value, err := box.SealAnonymous(nil, inBytes, &pubKey32, nil) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("cannot encrypt the input with provided public key") |
| 105 | + } |
| 106 | + fmt.Println(base64.StdEncoding.EncodeToString(value)) |
| 107 | + return nil |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + cmdEncrypt.Flags().StringVarP(&input, "file", "f", "", "Input file path. Uses standard input if not provided") |
| 112 | + cmdEncrypt.Flags().StringVarP(&pubKeyFile, "public-key", "p", "", "Public key file path for decrypting") |
| 113 | + _ = cmdEncrypt.MarkFlagRequired("public-key") |
| 114 | + return cmdEncrypt |
| 115 | +} |
| 116 | + |
| 117 | +func makeDecryptCmd() *cobra.Command { |
| 118 | + var input string |
| 119 | + var pvtKeyFile string |
| 120 | + var pubKeyFile string |
| 121 | + cmdDecrypt := &cobra.Command{ |
| 122 | + Use: "decrypt [(-f|--file=) input] (-k|--private-key=) private-key file (-p|--public-key=) public-key file", |
| 123 | + Short: "Decrypt input with the given private and public keys", |
| 124 | + Long: "Decrypt input file or stdin with the given private and public keys", |
| 125 | + Args: cobra.ExactArgs(0), |
| 126 | + SilenceUsage: true, |
| 127 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 128 | + inEncodedBytes, err := readInput(input) |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("cannot read input: %v", err) |
| 131 | + } |
| 132 | + inBytes, err := base64.StdEncoding.DecodeString(string(inEncodedBytes)) |
| 133 | + if err != nil { |
| 134 | + return fmt.Errorf("cannot decode input: %v", err) |
| 135 | + } |
| 136 | + pubEnc, err := ioutil.ReadFile(pubKeyFile) |
| 137 | + if err != nil { |
| 138 | + return fmt.Errorf("cannot read public key file %q: %v", pubKeyFile, err) |
| 139 | + } |
| 140 | + pubBytes, err := base64.StdEncoding.DecodeString(string(pubEnc)) |
| 141 | + if err != nil { |
| 142 | + return fmt.Errorf("cannot decode public key: %v", err) |
| 143 | + } |
| 144 | + pvtEnc, err := ioutil.ReadFile(pvtKeyFile) |
| 145 | + if err != nil { |
| 146 | + return fmt.Errorf("cannot read private key file %q: %v", pvtKeyFile, err) |
| 147 | + } |
| 148 | + pvtBytes, err := base64.StdEncoding.DecodeString(string(pvtEnc)) |
| 149 | + if err != nil { |
| 150 | + return fmt.Errorf("cannot decode private key: %v", err) |
| 151 | + } |
| 152 | + var pubKey32 [32]byte |
| 153 | + var pvtKey32 [32]byte |
| 154 | + copy(pubKey32[:], pubBytes) |
| 155 | + copy(pvtKey32[:], pvtBytes) |
| 156 | + |
| 157 | + value, ok := box.OpenAnonymous(nil, inBytes, &pubKey32, &pvtKey32) |
| 158 | + if !ok { |
| 159 | + return fmt.Errorf("cannot decrypt the input with provided key pair") |
| 160 | + } |
| 161 | + fmt.Println(string(value)) |
| 162 | + return nil |
| 163 | + }, |
| 164 | + } |
| 165 | + |
| 166 | + cmdDecrypt.Flags().StringVarP(&input, "file", "f", "", "Base64 input file path. Uses standard input if not provided") |
| 167 | + cmdDecrypt.Flags().StringVarP(&pubKeyFile, "public-key", "p", "", "Public key file path for decrypting") |
| 168 | + cmdDecrypt.Flags().StringVarP(&pvtKeyFile, "private-key", "k", "", "Private key file path for decrypting") |
| 169 | + _ = cmdDecrypt.MarkFlagRequired("public-key") |
| 170 | + _ = cmdDecrypt.MarkFlagRequired("private-key") |
| 171 | + return cmdDecrypt |
| 172 | +} |
| 173 | + |
| 174 | +func readInput(file string) ([]byte, error) { |
| 175 | + if len(file) > 0 { |
| 176 | + return ioutil.ReadFile(file) |
| 177 | + } else { |
| 178 | + return ioutil.ReadAll(os.Stdin) |
| 179 | + } |
| 180 | +} |
0 commit comments