Skip to content

Commit 8422b48

Browse files
committed
minor edits
2 parents 1e503ca + 8e01f3d commit 8422b48

File tree

5 files changed

+81
-14
lines changed

5 files changed

+81
-14
lines changed

cachefile.dbm.dat

Whitespace-only changes.

ciphers/RSAcipher.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func generatePrimes(limit int)int{
1616
primes:= prime(limit)
1717
var choice []int
1818
choice = append(choice, 1,7,11,13,17,19,23,29)
19-
for{
19+
for{
2020
k:=rand.Intn(int(limit/30))
2121
i:=choice[rand.Intn(len(choice))]
2222
c:=30*k+i
@@ -137,36 +137,38 @@ func toRune(slice []int)string{
137137
}
138138
return str
139139
}
140-
140+
141141

142142
func main(){
143143
rand.Seed(time.Now().UTC().UnixNano())
144-
bits:=15
145-
144+
bits:=17
145+
146146
p:= generatePrimes(1<<bits)
147147
q:= generatePrimes(1<<bits)
148148
for p==q{
149149
q = generatePrimes(1<<bits)
150-
}
151-
150+
}
151+
152152
n:= p*q
153-
153+
154154
delta:=lcm(p-1,q-1)
155-
155+
156156
e:=generatePrimes(delta)
157157
d:=modularMultiplicativeInverse(e,delta)
158158

159159
fmt.Printf("%v \n%v \n%v \n%v\n",p,q,e,d)
160-
160+
161161

162162
str:="I think RSA is really great"
163163
message := []rune(str)
164164
asciiSlice :=toASCII(message)
165165

166-
fmt.Printf("asciiSlice :%v \n",asciiSlice)
166+
fmt.Printf("asciiSlice : %v \n",asciiSlice)
167167
encrypted := encryptRSA(asciiSlice,e,n)
168-
fmt.Printf("encrypted :%v \n",encrypted)
168+
fmt.Printf("encrypted : %v \n",encrypted)
169169
decrypted := decryptRSA(encrypted,d,n)
170170
fmt.Printf("decrypted : %v \n",decrypted)
171171
fmt.Printf("cleartext : %v \n",toRune(decrypted))
172+
//switched to atom
173+
172174
}

ciphers/diffieHellmanKeyExchange.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"math/rand"
66
)
7+
<<<<<<< HEAD
78

89
func main() {
910
bit := 30
@@ -19,27 +20,57 @@ func main() {
1920

2021
p := 2 + rand.Intn(1<<bit)
2122
g := 2 + rand.Intn(5)
23+
=======
24+
func main(){
25+
bit:=30
26+
/*
27+
p and g are pre-agreed constants
28+
that can be communicated over an insecure channel
29+
p should ideally be a large prime number but any integer works
30+
g should be a small integer, 2,3 works fine
31+
32+
PS: Note that the secret keys are never send over
33+
the network
34+
*/
35+
36+
p:=2+rand.Intn(1<<bit)
37+
g:=2+rand.Intn(5)
38+
>>>>>>> 8e01f3d134dcddea9e8ab08b08e45ae06a476ede
2239

2340
//Both parties choose a secret key
2441

2542
AliceSecret := 1 + rand.Intn(1<<bit)
2643
BobSecret := 1 + rand.Intn(1<<bit)
2744

45+
<<<<<<< HEAD
2846
fmt.Printf("Alice's secret key is: %v \n", AliceSecret)
2947
fmt.Printf("Bob's secret key is: %v \n", BobSecret)
48+
=======
49+
fmt.Printf("Alice's secret key is: %v\n",AliceSecret)
50+
fmt.Printf("Bob's secret key is: %v\n",BobSecret)
51+
>>>>>>> 8e01f3d134dcddea9e8ab08b08e45ae06a476ede
3052

3153
//Both parties send ((g^secret_key)%p)
3254
//It's not possible to determine the secretkey from the value sent
3355

56+
<<<<<<< HEAD
3457
AliceSends := modularExponentiation(g, AliceSecret, p)
3558
BobSends := modularExponentiation(g, BobSecret, p)
3659

3760
fmt.Printf("Alice sends to Bob: %v \n", AliceSends)
3861
fmt.Printf("Bob sends to Alice: %v \n", BobSends)
62+
=======
63+
AliceSends :=modularExponentiation(g,AliceSecret,p)
64+
BobSends :=modularExponentiation(g,BobSecret,p)
65+
66+
fmt.Printf("Alice sends to Bob: %v\n",AliceSends)
67+
fmt.Printf("Bob sends to Alice: %v\n",BobSends)
68+
>>>>>>> 8e01f3d134dcddea9e8ab08b08e45ae06a476ede
3969

4070
//Both parties calculate the shared secret key from the value send
4171
//(value_sent^secret_key)%p
4272
//Both calculations end up with same value despite the different inputs
73+
<<<<<<< HEAD
4374
AliceComputes := modularExponentiation(BobSends, AliceSecret, p)
4475
BobComputes := modularExponentiation(AliceSends, BobSecret, p)
4576

@@ -67,6 +98,38 @@ func modularExponentiation(b int, e int, mod int) int {
6798
}
6899
e = e >> 1
69100
b = (b * b) % mod
101+
=======
102+
AliceComputes :=modularExponentiation(BobSends,AliceSecret,p)
103+
BobComputes := modularExponentiation(AliceSends,BobSecret,p)
104+
105+
fmt.Printf("Alice Computes the shared secret key as: %v\n",AliceComputes)
106+
fmt.Printf("Bob Computes the shared secret key as: %v\n",BobComputes)
107+
108+
// simply confirms that the values are equal
109+
if AliceComputes == BobComputes{
110+
sharedKey:=AliceComputes
111+
fmt.Println("Voila, shared key is ",sharedKey)
112+
}
113+
114+
115+
116+
117+
}
118+
func modularExponentiation(b int, e int, mod int)int{
119+
//runs in O(log(n)) where n = e
120+
//uses exponentiation by squaring to speed up the process
121+
if mod == 1{
122+
return 0
123+
}
124+
r:=1
125+
b = b % mod
126+
for ;e>0;{
127+
if e%2==1{
128+
r=(r*b)%mod
129+
}
130+
e =e>>1
131+
b = (b*b)%mod
132+
>>>>>>> 8e01f3d134dcddea9e8ab08b08e45ae06a476ede
70133
}
71134
return r
72135
}

other/NestedBrackets.go

Lines changed: 3 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,8 @@ import (
1819
)
1920

2021
func isBalanced(input string) string {
21-
22+
if len(input)%2 == 0{
23+
return input + "is not balanced."
2224
if len(input) > 0 {
2325
var stack []byte
2426
for i := 0; i < len(input); i++ {

searches/binary_search.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func binarySearch(array []int, target int, lowIndex int, highIndex int) int {
77
if highIndex < lowIndex {
88
return -1
99
}
10-
mid := int((lowIndex + highIndex) / 2)
10+
mid := int((lowIndex + (highIndex-lowIndex)/2))
1111
if array[mid] > target {
1212
return binarySearch(array, target, lowIndex, mid)
1313
} else if array[mid] < target {
@@ -22,7 +22,7 @@ func iterBinarySearch(array []int, target int, lowIndex int, highIndex int) int
2222
endIndex := highIndex
2323
var mid int
2424
for startIndex < endIndex {
25-
mid = int((startIndex + endIndex) / 2)
25+
mid = int((startIndex + (endIndex-startIndex)/2))
2626
if array[mid] > target {
2727
endIndex = mid
2828
} else if array[mid] < target {

0 commit comments

Comments
 (0)