forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdh_test.go
82 lines (68 loc) · 2.28 KB
/
dh_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
package codec
import (
"math/big"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDiffieHellman(t *testing.T) {
key1, err := GenerateKey()
assert.Nil(t, err)
key2, err := GenerateKey()
assert.Nil(t, err)
pubKey1, err := ComputeKey(key1.PubKey, key2.PriKey)
assert.Nil(t, err)
pubKey2, err := ComputeKey(key2.PubKey, key1.PriKey)
assert.Nil(t, err)
assert.Equal(t, pubKey1, pubKey2)
}
func TestDiffieHellman1024(t *testing.T) {
old := p
p, _ = new(big.Int).SetString("F488FD584E49DBCD20B49DE49107366B336C380D451D0F7C88B31C7C5B2D8EF6F3C923C043F0A55B188D8EBB558CB85D38D334FD7C175743A31D186CDE33212CB52AFF3CE1B1294018118D7C84A70A72D686C40319C807297ACA950CD9969FABD00A509B0246D3083D66A45D419F9C7CBD894B221926BAABA25EC355E92F78C7", 16)
defer func() {
p = old
}()
key1, err := GenerateKey()
assert.Nil(t, err)
key2, err := GenerateKey()
assert.Nil(t, err)
pubKey1, err := ComputeKey(key1.PubKey, key2.PriKey)
assert.Nil(t, err)
pubKey2, err := ComputeKey(key2.PubKey, key1.PriKey)
assert.Nil(t, err)
assert.Equal(t, pubKey1, pubKey2)
}
func TestDiffieHellmanMiddleManAttack(t *testing.T) {
key1, err := GenerateKey()
assert.Nil(t, err)
keyMiddle, err := GenerateKey()
assert.Nil(t, err)
key2, err := GenerateKey()
assert.Nil(t, err)
const aesByteLen = 32
pubKey1, err := ComputeKey(keyMiddle.PubKey, key1.PriKey)
assert.Nil(t, err)
src := []byte(`hello, world!`)
encryptedSrc, err := EcbEncrypt(pubKey1.Bytes()[:aesByteLen], src)
assert.Nil(t, err)
pubKeyMiddle, err := ComputeKey(key1.PubKey, keyMiddle.PriKey)
assert.Nil(t, err)
decryptedSrc, err := EcbDecrypt(pubKeyMiddle.Bytes()[:aesByteLen], encryptedSrc)
assert.Nil(t, err)
assert.Equal(t, string(src), string(decryptedSrc))
pubKeyMiddle, err = ComputeKey(key2.PubKey, keyMiddle.PriKey)
assert.Nil(t, err)
encryptedSrc, err = EcbEncrypt(pubKeyMiddle.Bytes()[:aesByteLen], decryptedSrc)
assert.Nil(t, err)
pubKey2, err := ComputeKey(keyMiddle.PubKey, key2.PriKey)
assert.Nil(t, err)
decryptedSrc, err = EcbDecrypt(pubKey2.Bytes()[:aesByteLen], encryptedSrc)
assert.Nil(t, err)
assert.Equal(t, string(src), string(decryptedSrc))
}
func TestKeyBytes(t *testing.T) {
var empty DhKey
assert.Equal(t, 0, len(empty.Bytes()))
key, err := GenerateKey()
assert.Nil(t, err)
assert.True(t, len(key.Bytes()) > 0)
}