-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
280 lines (240 loc) · 7.49 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
// The leftRotate function performs a left rotation operation on the given slice A by n positions.
func leftRotate(A []int, n int) []int {
// temp := make([]int, len(A))
// copy(temp, A[n:])
// copy(temp[len(A)-n:], A[:n])
// return temp
return append(A[n:], A[:n]...)
}
// The xorOfFour function performs the XOR operation on four input slices A, B, C, and D element-wise.
func xorOfFour(A, B, C, D []int) []int {
temp := make([]int, len(A))
for i := 0; i < len(A); i++ {
temp[i] = (A[i] + B[i] + C[i] + D[i]) % 2
}
return temp
}
// The andOp function performs the logical AND operation on two input slices A and B element-wise.
func andOp(A, B []int) []int {
temp := make([]int, len(A))
for i := 0; i < len(A); i++ {
temp[i] = A[i] & B[i]
}
return temp
}
// The orOfThree function performs the logical OR operation on three input slices A, B, and C element-wise.
func orOfThree(A, B, C []int) []int {
temp := make([]int, len(A))
for i := 0; i < len(A); i++ {
temp[i] = A[i] | B[i] | C[i]
}
return temp
}
// The notOp function performs the logical NOT operation on the input slice A element-wise.
func notOp(A []int) []int {
temp := make([]int, len(A))
for i := 0; i < len(A); i++ {
temp[i] = 1 - A[i]
}
return temp
}
// The addTwo function adds two input slices A and B as binary numbers.
func addTwo(A, B []int) []int {
temp := make([]int, len(A))
carry := 0
for i := len(A) - 1; i >= 0; i-- {
sum := carry + A[i] + B[i]
temp[i] = sum % 2
carry = sum / 2
}
return temp
}
// This function adds five input slices A, B, C, D, and E as binary numbers.
func addFive(A, B, C, D, E []int) []int {
temp := make([]int, len(A))
carry := 0
for i := len(A) - 1; i >= 0; i-- {
sum := carry + A[i] + B[i] + C[i] + D[i] + E[i]
temp[i] = sum % 2
carry = sum / 2
}
return temp
}
// This function converts the binary representation stored in the input slice A into a hexadecimal string.
func getDigest(A []int) string {
s := ""
for i := 0; i < 160; i += 4 {
an := A[i]*8 + A[i+1]*4 + A[i+2]*2 + A[i+3]
// if an <= 9 {
// s += strconv.Itoa(an)
// } else if an == 10 {
// s += "a"
// } else if an == 11 {
// s += "b"
// } else if an == 12 {
// s += "c"
// } else if an == 13 {
// s += "d"
// } else if an == 14 {
// s += "e"
// } else {
// s += "f"
// }
s += strconv.FormatInt(int64(an), 16)
}
return s
}
// This function takes an input slice of integers (inputArray), which represents the input message, and performs various operations to prepare the message for hashing.
func Encrypt(inputArray []int) []int {
// Convert the all string into binary
length := len(inputArray)
messageBit := []int{}
// convert the char to binary
for i := 0; i < length; i++ {
a := inputArray[i]
anss := []int{}
// Convert the character to binary representation
for a != 0 {
h := a % 2
anss = append(anss, h)
a = a / 2
}
// Append leading zeros until the binary representation becomes 8 bits
tmp := len(anss)
for j := 0; j < 8-tmp; j++ {
anss = append(anss, 0)
}
reverse(anss)
// Append the binary representation of the character to the message bits
for j := 0; j < 8; j++ {
messageBit = append(messageBit, anss[j])
}
}
// Append a '1' to the message bits
messageBit = append(messageBit, 1)
// Append zeros until the length of message bits is a multiple of 512 and has a remainder of 448
for len(messageBit)%512 != 448 {
messageBit = append(messageBit, 0)
}
// Calculate the length of the original message in bits
length = length * 8
// Create a slice of size 64 to store the length of the message in binary representation
ml := make([]int, 64)
p := 63
// Convert the length to binary representation
for length != 0 {
h := length % 2
ml[p] = h
length = length / 2
p = p - 1
}
// Append the binary representation of the length to the message bits
for j := 0; j < 64; j++ {
messageBit = append(messageBit, ml[j])
}
// Return the prepared message bits
return messageBit
}
// This function reverses the order of elements in the input slice arr.
func reverse(arr []int) {
for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
arr[i], arr[j] = arr[j], arr[i]
}
}
// This is the main hash function. It takes a string input inp, converts it to an array of ASCII values (inpBytes), and performs the SHA-1 hashing algorithm on the input message.
func Hash(inp string) string {
// converts the individual char of string to its ascii value
message := []rune(inp)
inpBytes := make([]int, len(message))
for i, c := range message {
inpBytes[i] = int(c)
}
messageBit := Encrypt(inpBytes)
A := []int{0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1} // A=[67452301] <- this is in hexadecimal form
B := []int{1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1} // B=[efcdab89]
C := []int{1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0} // C=[98badcfe]
D := []int{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0} // D=[10325476]
E := []int{1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0} // E=[c3d2e1f0]
size := len(messageBit)
nn := 0
for n := 0; n < size; n += 512 {
var w [][]int
for i := nn; i < nn+16; i++ {
var temp []int
for j := i * 32; j < (i+1)*32; j++ {
temp = append(temp, messageBit[j])
}
w = append(w, temp)
}
for i := 16; i < 80; i++ {
temp := xorOfFour(w[i-3], w[i-8], w[i-14], w[i-16])
w = append(w, leftRotate(temp, 1))
}
a := make([]int, len(A))
b := make([]int, len(B))
c := make([]int, len(C))
d := make([]int, len(D))
e := make([]int, len(E))
copy(a, A)
copy(b, B)
copy(c, C)
copy(d, D)
copy(e, E)
zeros := make([]int, 32)
for i := 0; i < 80; i++ {
var F []int
var k []int
if i >= 0 && i < 20 {
F = orOfThree(andOp(b, c), andOp(notOp(b), d), zeros)
k = []int{0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1} // k=[5A827999] <- this is in hexadecimal form
} else if i >= 20 && i < 40 {
F = xorOfFour(b, c, d, zeros)
k = []int{0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1} // k=[6ED9EBA1]
} else if i >= 40 && i < 60 {
F = orOfThree(andOp(b, c), andOp(b, d), andOp(c, d))
k = []int{1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0} // k=[8F1BBCDC]
} else {
F = xorOfFour(b, c, d, zeros)
k = []int{1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0} // k=[CA62C1D6]
}
temp := addFive(leftRotate(a, 5), F, e, k, w[i])
copy(e, d)
copy(d, c)
copy(c, leftRotate(b, 30))
copy(b, a)
copy(a, temp)
}
A = addTwo(A, a)
B = addTwo(B, b)
C = addTwo(C, c)
D = addTwo(D, d)
E = addTwo(E, e)
nn += 16
}
dig := getDigest(append(append(append(append(A, B...), C...), D...), E...))
return dig
}
// The main function handles user input and calls the Hash function to compute the SHA-1 hash of the input message.
func main() {
// taking input string.
var input string
fmt.Println("Enter Input: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
input = scanner.Text()
// if input is empty.
if strings.TrimSpace(input) == "" {
fmt.Println("Please Enter Input.")
os.Exit(1)
}
// Output the digest
fmt.Println("Digest (SHA-1):", Hash(input))
}