-
Notifications
You must be signed in to change notification settings - Fork 15
/
minisign_test.go
62 lines (52 loc) · 1.7 KB
/
minisign_test.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
// Copyright (c) 2021 Andreas Auernhammer. All rights reserved.
// Use of this source code is governed by a license that can be
// found in the LICENSE file.
package minisign
import (
"io"
"os"
"testing"
)
func TestRoundtrip(t *testing.T) {
const Password = "correct horse battery staple"
privateKey, err := PrivateKeyFromFile(Password, "./internal/testdata/minisign.key")
if err != nil {
t.Fatalf("Failed to load private key: %v", err)
}
message, err := os.ReadFile("./internal/testdata/message.txt")
if err != nil {
t.Fatalf("Failed to load message: %v", err)
}
signature := Sign(privateKey, message)
publicKey, err := PublicKeyFromFile("./internal/testdata/minisign.pub")
if err != nil {
t.Fatalf("Failed to load public key: %v", err)
}
if !Verify(publicKey, message, signature) {
t.Fatalf("Verification failed: signature %q - public key %q", signature, publicKey)
}
}
func TestReaderRoundtrip(t *testing.T) {
const Password = "correct horse battery staple"
privateKey, err := PrivateKeyFromFile(Password, "./internal/testdata/minisign.key")
if err != nil {
t.Fatalf("Failed to load private key: %v", err)
}
file, err := os.Open("./internal/testdata/message.txt")
if err != nil {
t.Fatalf("Failed to open message: %v", err)
}
defer file.Close()
reader := NewReader(file)
if _, err = io.Copy(io.Discard, reader); err != nil {
t.Fatalf("Failed to read message: %v", err)
}
signature := reader.Sign(privateKey)
publicKey, err := PublicKeyFromFile("./internal/testdata/minisign.pub")
if err != nil {
t.Fatalf("Failed to load public key: %v", err)
}
if !reader.Verify(publicKey, signature) {
t.Fatalf("Verification failed: signature %q - public key %q", signature, publicKey)
}
}