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

  1. package dh
  2. import (
  3. "crypto/rand"
  4. "math/big"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. const (
  9. bits = 2048
  10. )
  11. func TestDiffieHellman(t *testing.T) {
  12. p, err := rand.Prime(rand.Reader, bits/2)
  13. assert.Nil(t, err)
  14. g, err := rand.Prime(rand.Reader, bits/2)
  15. assert.Nil(t, err)
  16. max, err := rand.Prime(rand.Reader, bits/2)
  17. assert.Nil(t, err)
  18. a, err := rand.Int(rand.Reader, max)
  19. assert.Nil(t, err)
  20. b, err := rand.Int(rand.Reader, max)
  21. assert.Nil(t, err)
  22. A := new(big.Int).Exp(g, a, p)
  23. B := new(big.Int).Exp(g, b, p)
  24. sA := new(big.Int).Exp(B, a, p)
  25. sB := new(big.Int).Exp(A, b, p)
  26. if sA.Int64() != sB.Int64() {
  27. t.Errorf("secret not equal")
  28. }
  29. }