You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
675 B

package dh
import (
"crypto/rand"
"math/big"
"testing"
"github.com/stretchr/testify/assert"
)
const (
bits = 2048
)
func TestDiffieHellman(t *testing.T) {
p, err := rand.Prime(rand.Reader, bits/2)
assert.Nil(t, err)
g, err := rand.Prime(rand.Reader, bits/2)
assert.Nil(t, err)
max, err := rand.Prime(rand.Reader, bits/2)
assert.Nil(t, err)
a, err := rand.Int(rand.Reader, max)
assert.Nil(t, err)
b, err := rand.Int(rand.Reader, max)
assert.Nil(t, err)
A := new(big.Int).Exp(g, a, p)
B := new(big.Int).Exp(g, b, p)
sA := new(big.Int).Exp(B, a, p)
sB := new(big.Int).Exp(A, b, p)
if sA.Int64() != sB.Int64() {
t.Errorf("secret not equal")
}
}