-
Notifications
You must be signed in to change notification settings - Fork 0
/
sha1.c
126 lines (108 loc) · 2.56 KB
/
sha1.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
static u_int32_t S(int n, u_int32_t X);
static u_int32_t f(int t, u_int32_t B, u_int32_t C, u_int32_t D);
int main(void)
{
char *msg = "abcdefghi";
u_int32_t block[16];
for(int i = 0; i < 16; i++)
{
block[i] = 0;
}
int counter = 0;
int index = 0;
int need_shifting = 0;
for(int i = 0; i < strlen(msg); i++)
{
index = i / 4;
block[index] = block[index] | *(msg + i);
if(((i + 1) % 4))
{
block[index] = block[index] << 8;
}
counter++;
}
//if counter is multiple of 4 (4 bytes), add 0x10 and dont shift
//if counter is not a multiple of 4 (4 bytes), add 0x10 and shift
//in both cases, pad with a 1 (aka 0x80 because dealing with char)
if(!((counter + 1) % 4))
{
block[index] = block[index] | 0x80;
}else
{
index = counter / 4;
block[index] = block[index] | 0x80;
counter++;
while((counter % 4))
{
block[index] = block[index] << 8;
counter++;
}
}
//add size (in bits) of msg
block[15] = block[15] | strlen(msg) * 8;
u_int32_t W[64];
u_int32_t H[5];
H[0] = 0x67452301;
H[1] = 0xEFCDAB89;
H[2] = 0x98BADCFE;
H[3] = 0x10325476;
H[4] = 0xC3D2E1F0;
u_int32_t K[4];
K[0] = 0x5A827999;
K[1] = 0x6ED9EBA1;
K[2] = 0x8F1BBCDC;
K[3] = 0xCA62C1D6;
u_int32_t LETTER[5];
u_int32_t TEMP = 0;
for(int i = 0; i < 16; i++)
{
W[i] = block[i];
}
for(int t = 16; t < 80; t++)
{
W[t] = S(1, (W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]));
}
for(int i = 0; i < 5; i++)
{
LETTER[i] = H[i];
}
for(int t = 0; t < 80; t++)
{
TEMP = S(5, LETTER[0]) + f(t, LETTER[1], LETTER[2], LETTER[3]) + LETTER[4] + W[t] + K[t / 20];
LETTER[4] = LETTER[3];
LETTER[3] = LETTER[2];
LETTER[2] = S(30, LETTER[1]);
LETTER[1] = LETTER[0];
LETTER[0] = TEMP;
}
for(int i = 0; i < 5; i++)
{
H[i] += LETTER[i];
printf("%08x", H[i]);
}
printf("\n");
}
static u_int32_t S(int n, u_int32_t X)
{
return ((X << n) | (X >> (32 - n)));
}
static u_int32_t f(int t, u_int32_t B, u_int32_t C, u_int32_t D)
{
if(t <= 19)
{
return (B & C) | ((~B) & D);
}else if(t <= 39)
{
return B ^ C ^ D;
}else if(t <= 59)
{
return (B & C) | (B & D) | (C & D);
}else
{
return B ^ C ^ D;
}
}