forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffiehellmankeyexchange.go
41 lines (36 loc) · 1.09 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
// Package diffiehellman implements Deffie Hellman Key Exchange Algorithm
// for more information watch : https://www.youtube.com/watch?v=NmM9HA2MQGI
package diffiehellman
const (
generator = 3
primeNumber int64 = 6700417 // prime number discovered by Leonhard Euler
)
// GenerateShareKey : generates a key using client private key , generator and primeNumber
// this key can be made public
// shareKey = (g^key)%primeNumber
func GenerateShareKey(prvKey int64) int64 {
return modularExponentiation(generator, prvKey, primeNumber)
}
// GenerateMutualKey : generates a mutual key that can be used by only alice and bob
// mutualKey = (shareKey^prvKey)%primeNumber
func GenerateMutualKey(prvKey, shareKey int64) int64 {
return modularExponentiation(shareKey, prvKey, primeNumber)
}
// r = (b^e)%mod
func modularExponentiation(b, e, mod int64) int64 {
//runs in O(log(n)) where n = e
//uses exponentiation by squaring to speed up the process
if mod == 1 {
return 0
}
var r int64 = 1
b = b % mod
for e > 0 {
if e&1 == 1 {
r = (r * b) % mod
}
e = e >> 1
b = (b * b) % mod
}
return r
}