-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSHA1.cpp
More file actions
60 lines (56 loc) · 1.42 KB
/
SHA1.cpp
File metadata and controls
60 lines (56 loc) · 1.42 KB
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
#include<iostream>
#include<bitset>
#include<string>
using namespace std;
namespace SHA1{
uint_8 originalLength,blockCount,bitCount;
bitset<512> b[65535];
bitset<256> d[5];
void checkBitPosition(){
if(bitCount == 512){
bitCount = 0;
++blockCount;
}
}
void reset(){
for(int i=0;i<blockCount;++i) b[i].reset();
originalLength = blockCount = bitCount = 0;
d[0] = 0x67452301;
d[1] = 0xefcdab89;
d[2] = 0x98badcfe;
d[3] = 0x10325476;
d[4] = 0xc3d2e1f0;
}
void init(string &s){
for(char &c:s){
for(int i=0;i<8;++i){
b[blockCount][bitCount++] = c&(1<<(7-i));
++originalLength;
}
checkBitPosition();
}
}
void padding(){
b[blockCount][bitCount++] = 1;
while(bitCount != 448){
++bitCount;
checkBitPosition();
}
bitCount+=32;
for(int i=0;i<32;++i)b[blockCount][bitCount++] = originalLength&(1<<(31-i));
assert(bitCount == 512);
checkBitPosition();
}
void processBlock(const int &blockNum){
uint_8 w[79];
for(int i=0;i<16;++i)w[i]=int((blockNum>>(i<<5)).to_ulong());
}
string SHA1(string s){
reset();
init(s);
padding();
for(int i=0;i<blockCount;++i)processBlock(i);
}
}
int main(){
}