-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsrp_test.go
90 lines (74 loc) · 2.54 KB
/
srp_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
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
package srp_test
import (
"bytes"
"encoding/base64"
"fmt"
"testing"
"gitlab.com/voynic/srp"
)
func TestFullHandshake(t *testing.T) {
// First, register a client.
// I is defined.
identifier := []byte("testuser")
passphrase := []byte("Password123!")
s, v, err := srp.NewClient(identifier, passphrase)
if err != nil {
t.Errorf("Error in NewClient()")
}
// *************************************************************************
// Client sends I, s, v to the server.
// *************************************************************************
// The client now initiates a handshake to create a session
A, a, err := srp.InitiateHandshake()
if err != nil {
t.Errorf("Error in InitiateHandshake()")
}
// *************************************************************************
// Client sends I, A to the server...
// *************************************************************************
// Lookup "v" and "s" from "I"
B, S, serverK, err := srp.Handshake(A, v)
if err != nil {
t.Errorf("Error in Handshake()")
}
if testing.Verbose() {
fmt.Println("Server K: " + formatBytes(serverK))
}
// *************************************************************************
// Server sends B, s to the client...
// *************************************************************************
clientK, err := srp.CompleteHandshake(A, a, identifier, passphrase, s, B)
if err != nil {
t.Errorf("Error in CompleteHandshake()")
}
if testing.Verbose() {
fmt.Println("Client K: " + formatBytes(clientK))
}
// *************************************************************************
// Client and server MIGHT have a shared K.
// *************************************************************************
// These proofs will almost certainly not fail, but we invoke the functions
// just to be diligent.
clientProof := srp.ClientProof(A, B, S)
srp.ServerProof(A, clientProof, serverK)
// *************************************************************************
// Client and server SHOULD have a shared K.
// *************************************************************************
if !bytes.Equal(serverK, clientK) {
t.Errorf("Server K does not match Client K. Proofs failed!")
}
}
//
// Helper method that takes a byte slice, and returns a pretty-printable
// base64 string.
//
func formatBytes(x []byte) string {
// Convert bytes to a base64 string
str := base64.StdEncoding.EncodeToString(x)
// Return up to 40 characters of the base64 string
if len(str) > 40 {
return fmt.Sprintf("%v...", str[:40])
} else {
return str
}
}