-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathdiffiehellmankeyexchange_test.go
41 lines (34 loc) · 1.4 KB
/
diffiehellmankeyexchange_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
package diffiehellman
import (
"crypto/rand"
"crypto/rsa"
"testing"
)
func TestDiffieHellmanKeyExchange(t *testing.T) {
t.Run("Test 1: modularExponentiation", func(t *testing.T) {
var want int64 = 9 // (3^5)mod13 = 243mod13 = 9
var prvKey int64 = 5
var generator int64 = 3
var primeNumber int64 = 13
got := modularExponentiation(generator, prvKey, primeNumber)
if got != want {
t.Errorf(`with privateKey=%d, generator=%d and primeNumber=%d
modularExponentiation should result=%d, but resulted=%d`, prvKey, generator, primeNumber, want, got)
}
})
t.Run("Test 2: Key Exchange", func(t *testing.T) {
// generating a small sized rsa_cipher key for testing
alicePrvKey, _ := rsa.GenerateKey(rand.Reader, 31)
bobPrvKey, _ := rsa.GenerateKey(rand.Reader, 31)
// alice and bob generates their respective share key with their privateKey
shareKeyByAlice := GenerateShareKey(alicePrvKey.D.Int64())
shareKeyByBob := GenerateShareKey(bobPrvKey.D.Int64())
// generated share key now can be exchanged even via insecure channel
// mutualKey can be computed using shared key
mutualKeyComputedByAlice := GenerateMutualKey(alicePrvKey.D.Int64(), shareKeyByBob)
mutualKeyComputedByBob := GenerateMutualKey(bobPrvKey.D.Int64(), shareKeyByAlice)
if mutualKeyComputedByAlice != mutualKeyComputedByBob {
t.Errorf("mutual key computed by alice and bob should be same, but got un-equal")
}
})
}