-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsha1.go
131 lines (124 loc) · 3.47 KB
/
sha1.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
package hash
func SHA1(input []byte) [20]byte {
var blocks [][64]byte
// preparations
var currentBlock [64]byte
iterator := 0
for i := 0; i < len(input); i, iterator = i+1, iterator+1 {
if iterator == 64 {
blocks = append(blocks, currentBlock)
currentBlock = [64]byte{}
iterator = 0
}
currentBlock[iterator] = input[i]
}
// https://ru.wikipedia.org/wiki/SHA-1 [Инициализация]
if iterator <= 55 { // IF INPUT < 448 BIT
currentBlock[iterator] = 1 << 7
fillLastBitsWithInputLength(¤tBlock, len(input))
blocks = append(blocks, currentBlock)
} else if iterator == 64 { // IF INPUT == MOD 512
blocks = append(blocks, currentBlock)
currentBlock = [64]byte{}
currentBlock[0] = 1 << 7
fillLastBitsWithInputLength(¤tBlock, len(input))
blocks = append(blocks, currentBlock)
} else { // IF INPUT > 448 BIT
currentBlock[iterator] = 1 << 7
blocks = append(blocks, currentBlock)
currentBlock = [64]byte{}
fillLastBitsWithInputLength(¤tBlock, len(input))
blocks = append(blocks, currentBlock)
}
// initialize buffers
var h0, h1, h2, h3, h4 uint32 = 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0
// processing blocks
for _, chunk := range blocks {
var W [80]uint32
// fill first 16 words from chunk
for i, j := 0, 0; i < 64; i, j = i+4, j+1 {
W[j] = (uint32(chunk[i]) << 24) | (uint32(chunk[i+1]) << 16) | (uint32(chunk[i+2]) << 8) | uint32(chunk[i+3])
// fmt.Printf("%x\n", W[j])
// fmt.Println("----------------")
}
// THIS STEP WAS TESTED
// extend from 16 to 80 words
for i := 16; i < 80; i++ {
W[i] = cyclicLeftShift((W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16]), 1) // CIRLCE LEFT SHIFT! NOT JUST LEFT SHIFT !
}
// initialize working vars
a, b, c, d, e := h0, h1, h2, h3, h4
// MAIN LOOP
for i := 0; i < 80; i++ {
var K, F uint32
switch {
case i >= 0 && i <= 19:
F = (b & c) | ((^b) & d)
K = 0x5A827999
case i >= 20 && i <= 39:
F = b ^ c ^ d
K = 0x6ED9EBA1
case i >= 40 && i <= 59:
F = (b & c) | (b & d) | (c & d)
K = 0x8F1BBCDC
case i >= 60 && i <= 79:
F = b ^ c ^ d
K = 0xCA62C1D6
}
temp := (cyclicLeftShift(a, 5)) + F + e + K + W[i] // CIRLCE LEFT SHIFT! NOT JUST LEFT SHIFT !
e = d
d = c
c = cyclicLeftShift(b, 30) // CIRLCE LEFT SHIFT! NOT JUST LEFT SHIFT !
b = a
a = temp
}
// ADD CHUNK TO THE RESULT/NEXT-OPER.
h0 = h0 + a
h1 = h1 + b
h2 = h2 + c
h3 = h3 + d
h4 = h4 + e
}
var result [20]byte
for i := 0; i < 20; i += 4 {
var h uint32
if i == 0 {
h = h0
} else if i == 4 {
h = h1
} else if i == 8 {
h = h2
} else if i == 12 {
h = h3
} else if i == 16 {
h = h4
}
result[i] = byte(h >> 24)
result[i+1] = byte(h >> 16)
result[i+2] = byte(h >> 8)
result[i+3] = byte(h)
}
return result
}
/*
HELPERS
*/
func getBinaryLength(length int) [8]byte {
// constant for amount of required shift
right := [8]int{56, 48, 40, 32, 24, 16, 8, 0}
var binary64bitlength [8]byte
for i := 0; i < 8; i++ {
binary64bitlength[i] = uint8(length >> right[i])
}
return binary64bitlength
}
func cyclicLeftShift(value uint32, shift int) uint32 {
return (value << shift) | (value >> (32 - shift))
}
func fillLastBitsWithInputLength(currentBlock *[64]byte, input int) {
binaryLength := getBinaryLength(input * 8) // get binary length of the input (64bit value in [8]byte array format)
// add binary length to the 512 block
for i, j := 56, 0; i < 64 && j < 8; i, j = i+1, j+1 {
currentBlock[i] = binaryLength[j]
}
}