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.

45 lines
753 B

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