Skip to content

Commit 86024c5

Browse files
committed
Standard formatting
I ran go fmt for every single go file in the repo. Maybe this can be automated via Github Actions
1 parent 3535f2c commit 86024c5

File tree

5 files changed

+127
-128
lines changed

5 files changed

+127
-128
lines changed

ciphers/RSAcipher.go

Lines changed: 78 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,58 @@ package main
22

33
import (
44
//"math/big"
5-
"math/rand"
5+
"fmt"
66
"math"
7+
"math/rand"
78
"time"
8-
"fmt"
99
)
10-
func generatePrimes(limit int)int{
10+
11+
func generatePrimes(limit int) int {
1112
/*
12-
generate primes by factoring
13-
relies on the 30k+i, though better formulae exist
14-
where k >=0 and i = (1,7,11,13,17,13,19,23,29)
13+
generate primes by factoring
14+
relies on the 30k+i, though better formulae exist
15+
where k >=0 and i = (1,7,11,13,17,13,19,23,29)
1516
*/
16-
primes:= prime(limit)
17+
primes := prime(limit)
1718
var choice []int
18-
choice = append(choice, 1,7,11,13,17,19,23,29)
19-
for{
20-
k:=rand.Intn(int(limit/30))
21-
i:=choice[rand.Intn(len(choice))]
22-
c:=30*k+i
19+
choice = append(choice, 1, 7, 11, 13, 17, 19, 23, 29)
20+
for {
21+
k := rand.Intn(int(limit / 30))
22+
i := choice[rand.Intn(len(choice))]
23+
c := 30*k + i
2324
found := true
24-
for _,v:= range primes{
25-
if c%v==0{
25+
for _, v := range primes {
26+
if c%v == 0 {
2627
found = false
2728
break
2829
}
2930
}
30-
if found{
31+
if found {
3132
return c
3233
}
3334
}
3435
}
35-
func prime(limit int)(primes []int){
36-
sqrtLimit:=int(math.Ceil(math.Sqrt(float64(limit))))
37-
exit:= false
38-
primes = append(primes,2,3,5)
39-
lastIndex :=2
40-
for ;primes[lastIndex]<sqrtLimit;{
41-
if exit == true{
36+
func prime(limit int) (primes []int) {
37+
sqrtLimit := int(math.Ceil(math.Sqrt(float64(limit))))
38+
exit := false
39+
primes = append(primes, 2, 3, 5)
40+
lastIndex := 2
41+
for primes[lastIndex] < sqrtLimit {
42+
if exit == true {
4243
break
4344
}
44-
for i:=primes[lastIndex]+2;i<primes[lastIndex]*primes[lastIndex];i+=2{
45-
found:= true
46-
for _,v:= range primes {
47-
if i%v==0{
48-
found= false
45+
for i := primes[lastIndex] + 2; i < primes[lastIndex]*primes[lastIndex]; i += 2 {
46+
found := true
47+
for _, v := range primes {
48+
if i%v == 0 {
49+
found = false
4950
break
5051
}
5152
}
52-
if found{
53-
primes = append(primes,i)
53+
if found {
54+
primes = append(primes, i)
5455
lastIndex++
55-
if i >=sqrtLimit{
56+
if i >= sqrtLimit {
5657
exit = true
5758
break
5859
}
@@ -62,113 +63,111 @@ func prime(limit int)(primes []int){
6263
}
6364
return
6465
}
65-
func lcm (a int, b int)int{
66+
func lcm(a int, b int) int {
6667
//complexity depends on gcd
67-
return int((a*b)/gcd(a,b))
68+
return int((a * b) / gcd(a, b))
6869

6970
}
70-
func gcd (a int, b int) int{
71+
func gcd(a int, b int) int {
7172
//complexity not clear
72-
for b != 0{
73-
t:=b
73+
for b != 0 {
74+
t := b
7475
b = a % b
7576
a = t
7677
}
7778
return a
7879
}
79-
func modularMultiplicativeInverse(e int, delta int)int{
80+
func modularMultiplicativeInverse(e int, delta int) int {
8081
//runs in O(n) where n = delta
81-
e= e % delta
82-
for i:=1;i<delta;i++{
83-
if (i*e)%delta==1{
82+
e = e % delta
83+
for i := 1; i < delta; i++ {
84+
if (i*e)%delta == 1 {
8485
return i
8586
}
8687
}
8788
return 0
8889
}
8990

90-
func modularExponentiation(b int, e int, mod int)int{
91+
func modularExponentiation(b int, e int, mod int) int {
9192
//runs in O(log(n)) where n = e
92-
if mod == 1{
93+
if mod == 1 {
9394
return 0
9495
}
95-
r:=1
96+
r := 1
9697
b = b % mod
97-
for ;e>0;{
98-
if e%2==1{
99-
r=(r*b)%mod
98+
for e > 0 {
99+
if e%2 == 1 {
100+
r = (r * b) % mod
100101
}
101-
e =e>>1
102-
b = (b*b)%mod
102+
e = e >> 1
103+
b = (b * b) % mod
103104
}
104105
return r
105106
}
106107

107-
func encryptRSA(message []int,e int,n int)[]int{
108+
func encryptRSA(message []int, e int, n int) []int {
108109
//runs in O(k*log(n)) where k = len(message) and n = e
109110
var ciphertext []int
110-
for _,v := range message{
111-
ciphertext = append(ciphertext, modularExponentiation(v,e,n))
111+
for _, v := range message {
112+
ciphertext = append(ciphertext, modularExponentiation(v, e, n))
112113
}
113114
return ciphertext
114115
}
115-
func decryptRSA(ciphertext []int, d int, n int )[]int{
116+
func decryptRSA(ciphertext []int, d int, n int) []int {
116117
//runs in O(k*log(n)) where k = len(ciphertext) and n = d
117118
var message []int
118-
for _,v := range ciphertext {
119-
message = append(message, modularExponentiation(v,d,n))
119+
for _, v := range ciphertext {
120+
message = append(message, modularExponentiation(v, d, n))
120121
}
121122
return message
122123
}
123-
func toASCII(slice []rune)[]int{
124+
func toASCII(slice []rune) []int {
124125
//runs in O(n) where n = len(slice)
125126
var converted []int
126-
for _,v:= range slice{
127+
for _, v := range slice {
127128
converted = append(converted, int(v))
128129
}
129130
return converted
130131
}
131132

132-
func toRune(slice []int)string{
133+
func toRune(slice []int) string {
133134
//runs in O(n) where n = len(slice)
134135
var str string
135-
for _,v:= range slice{
136+
for _, v := range slice {
136137
str += string(v)
137138
}
138139
return str
139140
}
140141

141-
142-
func main(){
142+
func main() {
143143
rand.Seed(time.Now().UTC().UnixNano())
144-
bits:=17
144+
bits := 17
145145

146-
p:= generatePrimes(1<<bits)
147-
q:= generatePrimes(1<<bits)
148-
for p==q{
149-
q = generatePrimes(1<<bits)
146+
p := generatePrimes(1 << bits)
147+
q := generatePrimes(1 << bits)
148+
for p == q {
149+
q = generatePrimes(1 << bits)
150150
}
151151

152-
n:= p*q
153-
154-
delta:=lcm(p-1,q-1)
152+
n := p * q
155153

156-
e:=generatePrimes(delta)
157-
d:=modularMultiplicativeInverse(e,delta)
154+
delta := lcm(p-1, q-1)
158155

159-
fmt.Printf("%v \n%v \n%v \n%v\n",p,q,e,d)
156+
e := generatePrimes(delta)
157+
d := modularMultiplicativeInverse(e, delta)
160158

159+
fmt.Printf("%v \n%v \n%v \n%v\n", p, q, e, d)
161160

162-
str:="I think RSA is really great"
161+
str := "I think RSA is really great"
163162
message := []rune(str)
164-
asciiSlice :=toASCII(message)
165-
166-
fmt.Printf("asciiSlice : %v \n",asciiSlice)
167-
encrypted := encryptRSA(asciiSlice,e,n)
168-
fmt.Printf("encrypted : %v \n",encrypted)
169-
decrypted := decryptRSA(encrypted,d,n)
170-
fmt.Printf("decrypted : %v \n",decrypted)
171-
fmt.Printf("cleartext : %v \n",toRune(decrypted))
163+
asciiSlice := toASCII(message)
164+
165+
fmt.Printf("asciiSlice : %v \n", asciiSlice)
166+
encrypted := encryptRSA(asciiSlice, e, n)
167+
fmt.Printf("encrypted : %v \n", encrypted)
168+
decrypted := decryptRSA(encrypted, d, n)
169+
fmt.Printf("decrypted : %v \n", decrypted)
170+
fmt.Printf("cleartext : %v \n", toRune(decrypted))
172171
//switched to atom
173172

174173
}

ciphers/xorCipher.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
package main
22

33
import "fmt"
4-
func encrypt(key int, plaintext []int)(ciphertext []int){
5-
for _,char:= range plaintext {
6-
ciphertext = append(ciphertext,xor(char,key))
4+
5+
func encrypt(key int, plaintext []int) (ciphertext []int) {
6+
for _, char := range plaintext {
7+
ciphertext = append(ciphertext, xor(char, key))
78
}
89
return
910
}
10-
func xor(char int,key int)int{
11-
return (char^key)
11+
func xor(char int, key int) int {
12+
return (char ^ key)
1213
}
1314

14-
func decrypt(key int, ciphertext []int)(plaintext []int){
15-
for _,char:= range ciphertext {
16-
plaintext = append(plaintext , xor(char,key))
15+
func decrypt(key int, ciphertext []int) (plaintext []int) {
16+
for _, char := range ciphertext {
17+
plaintext = append(plaintext, xor(char, key))
1718
}
1819
return
1920
}
2021

21-
func decodeToString(slice []int)(str string){
22-
for _,v:= range slice{
23-
str+=string(v)
22+
func decodeToString(slice []int) (str string) {
23+
for _, v := range slice {
24+
str += string(v)
2425
}
2526
return
2627
}
27-
func toASCII(slice []rune)[]int{
28+
func toASCII(slice []rune) []int {
2829
var converted []int
29-
for _,v:= range slice{
30+
for _, v := range slice {
3031
converted = append(converted, int(v))
3132
}
3233
return converted
3334
}
34-
func main(){
35+
func main() {
3536
str := "hello world"
36-
key :=97
37-
temp:= []rune(str)
38-
message:=toASCII(temp)
39-
encrypted:=encrypt(key,message)
37+
key := 97
38+
temp := []rune(str)
39+
message := toASCII(temp)
40+
encrypted := encrypt(key, message)
4041
//ciphertext:=decodeToString(key,message)
41-
decrypted:=decrypt(key,encrypted)
42-
plaintext:=decodeToString(decrypted)
42+
decrypted := decrypt(key, encrypted)
43+
plaintext := decodeToString(decrypted)
4344
fmt.Println(plaintext)
4445
}
45-

data-structures/linked-list/Linkedlist.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "fmt"
44

55
/* v is the value of node; next is the pointer to next node */
66
type node struct {
7-
v int
7+
v int
88
next *node
99
}
1010

@@ -24,9 +24,9 @@ func (l *node) pushFront(val int) *node {
2424
nnode = head
2525
/* create a second node with new value and `next -> nnode`
2626
* is this way, nnode2 is before nnode
27-
*/
28-
nnode2 := &node {
29-
v: val,
27+
*/
28+
nnode2 := &node{
29+
v: val,
3030
next: nnode,
3131
}
3232
/* now head is equals nnode2 */
@@ -62,7 +62,7 @@ func (l *node) popFront() *node {
6262
/* create a new node equals to first node pointed by head */
6363
cpnode := new(node)
6464
cpnode = head.next
65-
65+
6666
/* now head is equals cpnode (second node) */
6767
head = cpnode
6868

@@ -76,7 +76,7 @@ func (l *node) popBack() *node {
7676
/* create a new node equals to head */
7777
cpnode := new(node)
7878
cpnode = head
79-
79+
8080
/* read list to the penultimate node */
8181
for cpnode.next.next != nil {
8282
cpnode = cpnode.next
@@ -89,14 +89,14 @@ func (l *node) popBack() *node {
8989
func main() {
9090
lista := new(node)
9191
lista.pushBack(25).pushBack(24).pushBack(32) /* lista: 25 24 32 */
92-
lista.pushBack(56) /* lista: 25 24 32 56 */
93-
lista.pushFront(36) /* lista: 36 25 24 32 56 */
94-
lista.popFront() /* lista: 25 24 32 56 */
95-
lista.popBack() /* lista: 25 24 32 */
96-
92+
lista.pushBack(56) /* lista: 25 24 32 56 */
93+
lista.pushFront(36) /* lista: 36 25 24 32 56 */
94+
lista.popFront() /* lista: 25 24 32 56 */
95+
lista.popBack() /* lista: 25 24 32 */
96+
9797
/* read the list until head is not nil */
9898
for head != nil {
99-
fmt.Printf("%d ",head.v)
99+
fmt.Printf("%d ", head.v)
100100
head = head.next /*head points to next node */
101101
}
102102
}

0 commit comments

Comments
 (0)