mirror of
https://github.com/FiloSottile/age.git
synced 2026-01-08 13:01:09 +00:00
cmd/age: add a prototype of the command line tool
This commit is contained in:
106
cmd/age/age.go
Normal file
106
cmd/age/age.go
Normal file
@@ -0,0 +1,106 @@
|
||||
// Copyright 2019 Google LLC
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file or at
|
||||
// https://developers.google.com/open-source/licenses/bsd
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/FiloSottile/age/internal/age"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.SetFlags(0)
|
||||
|
||||
generateFlag := flag.Bool("generate", false, "generate a new age key pair")
|
||||
decryptFlag := flag.Bool("d", false, "decrypt the input")
|
||||
flag.Parse()
|
||||
|
||||
switch {
|
||||
case *generateFlag:
|
||||
if *decryptFlag {
|
||||
log.Fatalf("Invalid flag combination")
|
||||
}
|
||||
generate()
|
||||
case *decryptFlag:
|
||||
if *generateFlag {
|
||||
log.Fatalf("Invalid flag combination")
|
||||
}
|
||||
decrypt()
|
||||
default:
|
||||
encrypt()
|
||||
}
|
||||
}
|
||||
|
||||
func generate() {
|
||||
if len(flag.Args()) != 0 {
|
||||
log.Fatalf("-generate takes no arguments")
|
||||
}
|
||||
|
||||
key := make([]byte, 32)
|
||||
if _, err := rand.Read(key); err != nil {
|
||||
log.Fatalf("Internal error: %v", err)
|
||||
}
|
||||
k, err := age.NewX25519Identity(key)
|
||||
if err != nil {
|
||||
log.Fatalf("Internal error: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("# created: %s\n", time.Now().Format(time.RFC3339))
|
||||
fmt.Printf("# %s\n", k.Recipient())
|
||||
fmt.Printf("%s\n", k)
|
||||
}
|
||||
|
||||
func encrypt() {
|
||||
var recipients []age.Recipient
|
||||
for _, arg := range flag.Args() {
|
||||
r, err := parseRecipient(arg)
|
||||
if err != nil {
|
||||
log.Fatalf("Error: %v", err)
|
||||
}
|
||||
recipients = append(recipients, r)
|
||||
}
|
||||
if len(recipients) == 0 {
|
||||
log.Fatalf("Missing recipients!")
|
||||
}
|
||||
|
||||
w, err := age.Encrypt(os.Stdout, recipients...)
|
||||
if err != nil {
|
||||
log.Fatalf("Error initializing encryption: %v", err)
|
||||
}
|
||||
if _, err := io.Copy(w, os.Stdin); err != nil {
|
||||
log.Fatalf("Error encrypting the input: %v", err)
|
||||
}
|
||||
if err := w.Close(); err != nil {
|
||||
log.Fatalf("Error finalizing encryption: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func decrypt() {
|
||||
var identities []age.Identity
|
||||
// TODO: use the default location if no arguments are provided.
|
||||
for _, name := range flag.Args() {
|
||||
ids, err := parseIdentitiesFile(name)
|
||||
if err != nil {
|
||||
log.Fatalf("Error: %v", err)
|
||||
}
|
||||
identities = append(identities, ids...)
|
||||
}
|
||||
|
||||
r, err := age.Decrypt(os.Stdin, identities...)
|
||||
if err != nil {
|
||||
log.Fatalf("Error initializing decryption: %v", err)
|
||||
}
|
||||
if _, err := io.Copy(os.Stdout, r); err != nil {
|
||||
log.Fatalf("Error decrypting the input: %v", err)
|
||||
}
|
||||
}
|
||||
52
cmd/age/parse.go
Normal file
52
cmd/age/parse.go
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright 2019 Google LLC
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file or at
|
||||
// https://developers.google.com/open-source/licenses/bsd
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/FiloSottile/age/internal/age"
|
||||
)
|
||||
|
||||
func parseRecipient(arg string) (age.Recipient, error) {
|
||||
if strings.HasPrefix(arg, "pubkey:") {
|
||||
return age.ParseX25519Recipient(arg)
|
||||
}
|
||||
return nil, fmt.Errorf("unknown recipient type: %s", arg)
|
||||
}
|
||||
|
||||
func parseIdentitiesFile(name string) ([]age.Identity, error) {
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open file: %v", err)
|
||||
}
|
||||
|
||||
var ids []age.Identity
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.HasPrefix(line, "#") || line == "" {
|
||||
continue
|
||||
}
|
||||
i, err := age.ParseX25519Identity(line)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("malformed secret keys file %q: %v", name, err)
|
||||
}
|
||||
ids = append(ids, i)
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, fmt.Errorf("failed to read %q: %v", name, err)
|
||||
}
|
||||
|
||||
if len(ids) == 0 {
|
||||
return nil, fmt.Errorf("no secret keys found in %q", name)
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
Reference in New Issue
Block a user