-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmessage_types_test.go
50 lines (36 loc) · 1.11 KB
/
message_types_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
package proton_test
import (
"os"
"testing"
"github.com/ProtonMail/go-proton-api"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/stretchr/testify/require"
)
func TestDecrypt(t *testing.T) {
body, err := os.ReadFile("testdata/body.pgp")
require.NoError(t, err)
pubKR := loadKeyRing(t, "testdata/pub.asc", nil)
prvKR := loadKeyRing(t, "testdata/prv.asc", []byte("password"))
msg := proton.Message{Body: string(body)}
sigs, err := proton.ExtractSignatures(prvKR, msg.Body)
require.NoError(t, err)
enc, err := crypto.NewPGPMessageFromArmored(msg.Body)
require.NoError(t, err)
dec, err := prvKR.Decrypt(enc, nil, crypto.GetUnixTime())
require.NoError(t, err)
require.NoError(t, pubKR.VerifyDetached(dec, sigs[0].Data, crypto.GetUnixTime()))
}
func loadKeyRing(t *testing.T, file string, pass []byte) *crypto.KeyRing {
f, err := os.Open(file)
require.NoError(t, err)
defer f.Close()
key, err := crypto.NewKeyFromArmoredReader(f)
require.NoError(t, err)
if pass != nil {
key, err = key.Unlock(pass)
require.NoError(t, err)
}
kr, err := crypto.NewKeyRing(key)
require.NoError(t, err)
return kr
}