-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiffieHellmanKeyExchange.go
executable file
·88 lines (76 loc) · 1.91 KB
/
DiffieHellmanKeyExchange.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
83
84
85
86
87
88
package main
import (
"fmt"
"crypto/rand"
"math/big"
"time"
)
var bits int = 256 //tested upto 2048
func main() {
g:=FindGenerator()
p:=new(big.Int)
p,_=SafePrime()
//channel to communicate b/w alice and bob
c:= make (chan *big.Int)
go IsAlice(p,g,c)
go IsBob(p,g,c)
time.Sleep(time.Millisecond*100)
}
func IsAlice (p *big.Int,g *big.Int, c chan *big.Int) {
//generate private key
a,_:=GenerateSecret(p)
//calcualte public key
ga:=new(big.Int).Exp(g, a, p)
//recevie bobs public key
bpub:= <- c
//send alices public key
c <- ga
//calculate gab
gba:=new(big.Int).Exp(bpub, a, p)
fmt.Println("alice shared secret",gba)
}
func IsBob (p *big.Int,g *big.Int,c chan *big.Int) {
//generate private key
b,_:=GenerateSecret(p)
//calcualte public key
gb:=new(big.Int).Exp(g, b, p)
c <- gb //sends public key to alice
apub:= <- c //waits for alices public key
gab:=new(big.Int).Exp(apub, b, p)
fmt.Println("bob shared secret",gab)
}
func SafePrime() (*big.Int,error) {
//find prime p = 2q+1 st q is also a suitable prime
//keep generating random primes till p=2q+1 is satisfied
p:=new(big.Int)
for {
q,err:=rand.Prime(rand.Reader,bits)
if err != nil {
return nil,err
}
one:=big.NewInt(1)
p = p.Lsh(q, 1)
p = p.Add(p, one)
if p.ProbablyPrime(20){
return p,nil
}
}
return nil,nil
}
func FindGenerator() *big.Int {
//can take g=2 for now and modify later
two:=big.NewInt(2)
return two
}
//generate private key
func GenerateSecret(p *big.Int) (*big.Int,error) {
s,err:=rand.Int(rand.Reader,p)
if err != nil {
return nil,err
} else {
return s,nil
}
}
func KeyDeriviationFucn () {
//not needed for this but can be added later if needed
}