/
main.go
60 lines (50 loc) · 1.21 KB
/
main.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
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
)
func run() error {
input, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
index := bytes.IndexByte(input, '.')
header := make([]byte, base64.RawStdEncoding.DecodedLen(len(input[:index])))
if _, err := base64.RawStdEncoding.Decode(header, input[:index]); err != nil {
return fmt.Errorf("decoding header: %w", err)
}
fmt.Println("=== HEADER ===")
var b bytes.Buffer
if json.Indent(&b, header, "", " ") == nil {
_, _ = io.Copy(os.Stdout, &b)
} else {
_, _ = os.Stdout.Write(header)
}
fmt.Println("\n=== BODY ===")
input = input[index+1:]
index = bytes.IndexByte(input, '.')
body := make([]byte, base64.RawStdEncoding.DecodedLen(len(input[:index])))
if _, err := base64.RawStdEncoding.Decode(body, input[:index]); err != nil {
return fmt.Errorf("decoding body: %w", err)
}
b.Reset()
if json.Indent(&b, body, "", " ") == nil {
_, _ = io.Copy(os.Stdout, &b)
} else {
_, _ = os.Stdout.Write(body)
}
fmt.Println("\n=== SIGNATURE ===")
input = input[index+1:]
_, _ = os.Stdout.Write(input)
return nil
}
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}