| 1 | // Copyright 2025 The age Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package main |
| 6 | |
| 7 | import ( |
| 8 | "encoding/json" |
| 9 | "flag" |
| 10 | "fmt" |
| 11 | "log" |
| 12 | "os" |
| 13 | "runtime/debug" |
| 14 | |
| 15 | "filippo.io/age/internal/inspect" |
| 16 | ) |
| 17 | |
| 18 | const usage = `Usage: |
| 19 | age-inspect [--json] [INPUT] |
| 20 | |
| 21 | Options: |
| 22 | --json Output machine-readable JSON. |
| 23 | |
| 24 | INPUT defaults to standard input. "-" may be used as INPUT to explicitly |
| 25 | read from standard input.` |
| 26 | |
| 27 | // Version can be set at link time to override debug.BuildInfo.Main.Version when |
| 28 | // building manually without git history. It should look like "v1.2.3". |
| 29 | var Version string |
| 30 | |
| 31 | func main() { |
| 32 | flag.Usage = func() { fmt.Fprintf(os.Stderr, "%s\n", usage) } |
| 33 | |
| 34 | var ( |
| 35 | versionFlag bool |
| 36 | jsonFlag bool |
| 37 | ) |
| 38 | |
| 39 | flag.BoolVar(&versionFlag, "version", false, "print the version") |
| 40 | flag.BoolVar(&jsonFlag, "json", false, "output machine-readable JSON") |
| 41 | flag.Parse() |
| 42 | |
| 43 | if versionFlag { |
| 44 | if buildInfo, ok := debug.ReadBuildInfo(); ok && Version == "" { |
| 45 | Version = buildInfo.Main.Version |
| 46 | } |
| 47 | fmt.Println(Version) |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | if flag.NArg() > 1 { |
| 52 | flag.Usage() |
| 53 | os.Exit(1) |
| 54 | } |
| 55 | |
| 56 | in := os.Stdin |
| 57 | var fileSize int64 = -1 |
| 58 | if name := flag.Arg(0); name != "" && name != "-" { |
| 59 | f, err := os.Open(name) |
| 60 | if err != nil { |
| 61 | errorf("failed to open input file %q: %v", name, err) |
| 62 | } |
| 63 | defer f.Close() |
| 64 | in = f |
| 65 | if stat, err := f.Stat(); err == nil && stat.Mode().IsRegular() { |
| 66 | fileSize = stat.Size() |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | data, err := inspect.Inspect(in, fileSize) |
| 71 | if err != nil { |
| 72 | errorf("inspection failed: %v", err) |
| 73 | } |
| 74 | |
| 75 | if jsonFlag { |
| 76 | enc := json.NewEncoder(os.Stdout) |
| 77 | enc.SetIndent("", " ") |
| 78 | if err := enc.Encode(data); err != nil { |
| 79 | errorf("failed to encode JSON output: %v", err) |
| 80 | } |
| 81 | } else { |
| 82 | name := flag.Arg(0) |
| 83 | if name == "" { |
| 84 | name = "<stdin>" |
| 85 | } |
| 86 | fmt.Printf("%s is an age file, version %q.\n", name, data.Version) |
| 87 | fmt.Printf("\n") |
| 88 | if data.Armor { |
| 89 | fmt.Printf("This file is ASCII-armored.\n") |
| 90 | fmt.Printf("\n") |
| 91 | } |
| 92 | fmt.Printf("This file is encrypted to the following recipient types:\n") |
| 93 | for _, t := range data.StanzaTypes { |
| 94 | fmt.Printf(" - %q\n", t) |
| 95 | } |
| 96 | fmt.Printf("\n") |
| 97 | switch data.Postquantum { |
| 98 | case "yes": |
| 99 | fmt.Printf("This file uses post-quantum encryption.\n") |
| 100 | fmt.Printf("\n") |
| 101 | case "no": |
| 102 | fmt.Printf("This file does NOT use post-quantum encryption.\n") |
| 103 | fmt.Printf("\n") |
| 104 | case "unknown": |
| 105 | fmt.Printf("It is unknown whether this file uses post-quantum encryption.\n") |
| 106 | fmt.Printf("\n") |
| 107 | } |
| 108 | fmt.Printf("Size breakdown (assuming it decrypts successfully):\n") |
| 109 | fmt.Printf("\n") |
| 110 | fmt.Printf(" Header % 12d bytes\n", data.Sizes.Header) |
| 111 | if data.Armor { |
| 112 | fmt.Printf(" Armor overhead % 12d bytes\n", data.Sizes.Armor) |
| 113 | } |
| 114 | fmt.Printf(" Encryption overhead % 12d bytes\n", data.Sizes.Overhead) |
| 115 | fmt.Printf(" Payload % 12d bytes\n", data.Sizes.MinPayload) |
| 116 | fmt.Printf(" -------------------\n") |
| 117 | total := data.Sizes.Header + data.Sizes.Overhead + data.Sizes.MinPayload + data.Sizes.Armor |
| 118 | fmt.Printf(" Total % 12d bytes\n", total) |
| 119 | fmt.Printf("\n") |
| 120 | fmt.Printf("Tip: for machine-readable output, use --json.\n") |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // l is a logger with no prefixes. |
| 125 | var l = log.New(os.Stderr, "", 0) |
| 126 | |
| 127 | func errorf(format string, v ...any) { |
| 128 | l.Printf("age-inspect: error: "+format, v...) |
| 129 | l.Printf("age-inspect: report unexpected or unhelpful errors at https://filippo.io/age/report") |
| 130 | os.Exit(1) |
| 131 | } |