-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexample_test.go
43 lines (36 loc) · 1011 Bytes
/
example_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
package stream_test
import (
"crypto/rand"
"net"
stream "github.com/nknorg/encrypted-stream"
)
func Example() {
// We use a net.Conn as an example.
conn, err := net.Dial("tcp", "golang.org:80")
if err != nil {
panic(err)
}
// In this example we treat key as a prior knowledge. In actual usage you can
// do handshake here using the original stream and compute shared key before
// creating encrypted stream from it.
var key [32]byte
_, err = rand.Read(key[:])
if err != nil {
panic(err)
}
config := &stream.Config{
Cipher: stream.NewXSalsa20Poly1305Cipher(&key),
SequentialNonce: true, // only when key is unique for every stream
Initiator: true, // only on the dialer side
}
// Create an encrypted stream from a conn.
encryptedConn, err := stream.NewEncryptedStream(conn, config)
if err != nil {
panic(err)
}
// Now you can use encryptedConn just like a regular conn
_, err = encryptedConn.Write([]byte("hello world"))
if err != nil {
panic(err)
}
}