aboutsummaryrefslogtreecommitdiff
path: root/scripts/signer.go
blob: aff95e9fb25aa454da82a10b47a7c26b9db3e190 (plain)
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
package main

import (
	"crypto"
	"encoding/base64"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"

	"golang.org/x/crypto/ed25519"
)

func main() {
	var doGenerate bool
	flag.BoolVar(&doGenerate, "g", false, "Generate a keypair")

	var doPrintPub bool
	flag.BoolVar(&doPrintPub, "p", false, "Print the pub key")

	var doSign bool
	flag.BoolVar(&doSign, "s", false, "Sign the release tar")

	var publicKeyString string
	flag.StringVar(&publicKeyString, "k", "", "Public key to verify against (runs verifier if set)")

	flag.Parse()

	if doGenerate {
		generateKeypair()
		os.Exit(0)
	}

	if doPrintPub {
		pub, _ := loadKeys()
		fmt.Printf("Public key: %s\n", base64.StdEncoding.EncodeToString(pub))
		os.Exit(0)
	}

	if doSign {
		_, priv := loadKeys()

		file, err := ioutil.ReadFile("bin/NoiseTorch_x64.tgz")
		if err != nil {
			panic(err)
		}

		sig, err := priv.Sign(nil, file, crypto.Hash(0))
		if err != nil {
			panic(err)
		}
		if err := ioutil.WriteFile("bin/NoiseTorch_x64.tgz.sig", sig, 0644); err != nil {
			panic(err)
		}
		os.Exit(0)
	}

	if publicKeyString != "" {
		pub, err := base64.StdEncoding.DecodeString(publicKeyString)
		if err != nil {
			panic(err)
		}

		file, err := ioutil.ReadFile("bin/NoiseTorch_x64.tgz")
		if err != nil {
			panic(err)
		}

		sig, err := ioutil.ReadFile("bin/NoiseTorch_x64.tgz.sig")
		if err != nil {
			panic(err)
		}

		verified := ed25519.Verify(pub, file, sig)

		fmt.Printf("Verified %t\n", verified)
	}
}

func loadKeys() (ed25519.PublicKey, ed25519.PrivateKey) {
	seed, err := ioutil.ReadFile(filepath.Join(os.Getenv("HOME"), ".config/noisetorch/private.key"))
	if err != nil {
		panic(err)
	}

	priv := ed25519.NewKeyFromSeed(seed)
	pub := priv.Public().(ed25519.PublicKey)

	return pub, priv
}

func generateKeypair() {
	pub, priv, err := ed25519.GenerateKey(nil)
	if err != nil {
		panic(err)
		os.Exit(1)
	}
	if err := ioutil.WriteFile(filepath.Join(os.Getenv("HOME"), ".config/noisetorch/private.key"), priv.Seed(), 0600); err != nil {
		panic(err)
		os.Exit(2)
	}

	fmt.Printf("Private key generated and saved.\nPublic key: %s\n", base64.StdEncoding.EncodeToString(pub))
}