-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharmor.go
109 lines (89 loc) · 2.34 KB
/
armor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"os"
"golang.org/x/crypto/openpgp/armor"
)
// crlfWriter wraps an io.Writer and ensures CRLF line endings
type crlfWriter struct {
w io.Writer
}
func (cw *crlfWriter) Write(p []byte) (n int, err error) {
// Replace LF with CRLF
p = bytes.ReplaceAll(p, []byte{'\n'}, []byte{'\r', '\n'})
return cw.w.Write(p)
}
func encode(input io.Reader, output io.Writer) error {
// Create a buffer to capture the output
var buf bytes.Buffer
// Wrap the buffer with our crlfWriter
crlfOutput := &crlfWriter{w: &buf}
w, err := armor.Encode(crlfOutput, "PGP MESSAGE", nil)
if err != nil {
return err
}
_, err = io.Copy(w, input)
if err != nil {
return err
}
// Close the armor writer
if err := w.Close(); err != nil {
return err
}
// Trim any trailing newlines and add a single CRLF
trimmedOutput := bytes.TrimRight(buf.Bytes(), "\r\n")
trimmedOutput = append(trimmedOutput, '\r', '\n')
// Write the final output
_, err = output.Write(trimmedOutput)
return err
}
func decode(input io.Reader, output io.Writer) error {
dec, err := armor.Decode(input)
if err != nil {
return err
}
_, err = io.Copy(output, dec.Body)
return err
}
func printUsage() {
fmt.Fprintf(os.Stderr, "Usage: %s [-d] [file]\n", os.Args[0])
fmt.Fprintf(os.Stderr, " -d Decode instead of encode\n")
fmt.Fprintf(os.Stderr, " file Input file (optional, stdin used if not provided)\n")
fmt.Fprintf(os.Stderr, "\nIf no file is specified, the program will read from stdin.\n")
fmt.Fprintf(os.Stderr, "To finish input from stdin, press Ctrl+D (Unix) or Ctrl+Z (Windows).\n")
}
func main() {
decodeFlag := flag.Bool("d", false, "Decode instead of encode")
flag.Usage = printUsage
flag.Parse()
var input io.Reader
var output io.Writer = os.Stdout
if flag.NArg() > 1 {
fmt.Fprintf(os.Stderr, "Error: Too many arguments\n\n")
printUsage()
os.Exit(1)
}
if flag.NArg() == 0 {
input = os.Stdin
} else {
file, err := os.Open(flag.Arg(0))
if err != nil {
log.Fatalf("Error opening file: %v", err)
}
defer file.Close()
input = file
}
var err error
if *decodeFlag {
err = decode(input, output)
} else {
err = encode(input, output)
}
if err != nil {
log.Fatalf("Error: %v", err)
}
}