Skip to content

Commit 626345f

Browse files
committed
add runnable example
1 parent 02f255e commit 626345f

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

example_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package delphi_test
22

33
import (
44
"crypto/rand"
5+
"fmt"
6+
"slices"
57
"testing"
68

79
"github.com/sean9999/go-delphi"
@@ -42,3 +44,40 @@ func TestExample(t *testing.T) {
4244
assert.Equal(t, "bar", foo)
4345

4446
}
47+
48+
func Example() {
49+
50+
// some plain text
51+
sentence := []byte("hello world")
52+
53+
// create two principals
54+
alice := delphi.NewPrincipal(rand.Reader)
55+
bob := delphi.NewPrincipal(rand.Reader)
56+
57+
// create a message for bob, from alice
58+
msg := delphi.NewMessage(rand.Reader, delphi.PlainMessage, sentence)
59+
msg.SenderKey = alice.PublicKey()
60+
61+
// add some metadata (this becomes AAD)
62+
msg.Headers["foo"] = "bar"
63+
msg.Headers["bing"] = "bat"
64+
65+
// encrypt message
66+
err := msg.Encrypt(rand.Reader, alice, bob.PublicKey(), nil)
67+
fmt.Println("should be nil", err)
68+
69+
// decrpyt message
70+
err = bob.Decrypt(msg, nil)
71+
fmt.Println("should be nil", err)
72+
73+
// is decrypted text same as plain text?
74+
diff := slices.Compare(sentence, msg.PlainText)
75+
fmt.Println("should be 0", diff)
76+
77+
// has the metadata survived?
78+
foo, ok := msg.Headers["foo"]
79+
80+
fmt.Println("should be true", ok)
81+
fmt.Println("should be bar", foo)
82+
83+
}

0 commit comments

Comments
 (0)