Skip to content

Commit 702eacc

Browse files
committed
Added Diffie Key exchange
1 parent 4db3b08 commit 702eacc

File tree

3 files changed

+55
-52
lines changed

3 files changed

+55
-52
lines changed

ciphers/diffieHellmanKeyExchange.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,42 @@ func main(){
1818

1919
p:=2+rand.Intn(1<<bit)
2020
g:=2+rand.Intn(5)
21-
21+
2222
//Both parties choose a secret key
23-
23+
2424
AliceSecret := 1 + rand.Intn(1<<bit)
2525
BobSecret := 1 + rand.Intn(1<<bit)
26-
27-
fmt.Printf("Alice's secret key is: %v",AliceSecret)
28-
fmt.Printf("Bob's secret key is: %v",BobSecret)
29-
30-
//Both parties send ((g^secret_key)%p)
26+
27+
fmt.Printf("Alice's secret key is: %v\n",AliceSecret)
28+
fmt.Printf("Bob's secret key is: %v\n",BobSecret)
29+
30+
//Both parties send ((g^secret_key)%p)
3131
//It's not possible to determine the secretkey from the value sent
32-
32+
3333
AliceSends :=modularExponentiation(g,AliceSecret,p)
3434
BobSends :=modularExponentiation(g,BobSecret,p)
3535

36-
fmt.Printf("Alice sends to Bob: %v",AliceSends)
37-
fmt.Printf("Bob sends to Alice: %v",BobSends)
36+
fmt.Printf("Alice sends to Bob: %v\n",AliceSends)
37+
fmt.Printf("Bob sends to Alice: %v\n",BobSends)
3838

3939
//Both parties calculate the shared secret key from the value send
4040
//(value_sent^secret_key)%p
4141
//Both calculations end up with same value despite the different inputs
4242
AliceComputes :=modularExponentiation(BobSends,AliceSecret,p)
4343
BobComputes := modularExponentiation(AliceSends,BobSecret,p)
4444

45-
fmt.Printf("Alice Computes the shared secret key as: %v",AliceComputes)
46-
fmt.Printf("Bob Computes the shared secret key as: %v",BobComputes)
47-
45+
fmt.Printf("Alice Computes the shared secret key as: %v\n",AliceComputes)
46+
fmt.Printf("Bob Computes the shared secret key as: %v\n",BobComputes)
47+
4848
// simply confirms that the values are equal
4949
if AliceComputes == BobComputes{
5050
sharedKey:=AliceComputes
51-
fmt.Println(" Voila, shared key is",sharedKey)
51+
fmt.Println("Voila, shared key is ",sharedKey)
5252
}
5353

54-
5554

56-
55+
56+
5757
}
5858
func modularExponentiation(b int, e int, mod int)int{
5959
//runs in O(log(n)) where n = e

other/NestedBrackets.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// For example, the string "()()[()]" is properly nested but "[(()]" is not.
88
// The function called isBalanced takes as input a string which is a sequence of brackets and
99
// returns true if input is nested and false otherwise.
10+
//note that only an even number of brackets can be properly nested
1011

1112
package main
1213

@@ -18,7 +19,9 @@ import (
1819
)
1920

2021
func isBalanced(input string) string {
21-
22+
/* if len(input)%2 == 0{
23+
return input + "is not balanced."
24+
}*/
2225
if len(input) > 0 {
2326
var stack []byte
2427
for i := 0; i < len(input); i++ {

searches/binary_search.go

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
/*
2-
Binary search implementation in Go
3-
*/
4-
package main
5-
6-
func binarySearch(array []int, target int, lowIndex int, highIndex int) int {
7-
if highIndex < lowIndex {
8-
return -1
9-
}
10-
mid := int((lowIndex + highIndex) / 2)
11-
if array[mid] > target {
12-
return binarySearch(array, target, lowIndex, mid)
13-
} else if array[mid] < target {
14-
return binarySearch(array, target, mid+1, highIndex)
15-
} else {
16-
return mid
17-
}
18-
}
19-
20-
func iterBinarySearch(array []int, target int, lowIndex int, highIndex int) int {
21-
startIndex := lowIndex
22-
endIndex := highIndex
23-
var mid int
24-
for startIndex < endIndex {
25-
mid = int((startIndex + endIndex) / 2)
26-
if array[mid] > target {
27-
endIndex = mid
28-
} else if array[mid] < target {
29-
startIndex = mid
30-
} else {
31-
return mid
32-
}
33-
}
34-
return -1
35-
}
1+
/*
2+
Binary search implementation in Go
3+
*/
4+
package main
5+
6+
func binarySearch(array []int, target int, lowIndex int, highIndex int) int {
7+
if highIndex < lowIndex {
8+
return -1
9+
}
10+
mid := int((lowIndex + (highIndex-lowindex)/2))
11+
if array[mid] > target {
12+
return binarySearch(array, target, lowIndex, mid)
13+
} else if array[mid] < target {
14+
return binarySearch(array, target, mid+1, highIndex)
15+
} else {
16+
return mid
17+
}
18+
}
19+
20+
func iterBinarySearch(array []int, target int, lowIndex int, highIndex int) int {
21+
startIndex := lowIndex
22+
endIndex := highIndex
23+
var mid int
24+
for startIndex < endIndex {
25+
mid = int((startIndex + (endIndex-startIndex)/2))
26+
if array[mid] > target {
27+
endIndex = mid
28+
} else if array[mid] < target {
29+
startIndex = mid
30+
} else {
31+
return mid
32+
}
33+
}
34+
return -1
35+
}

0 commit comments

Comments
 (0)