-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashPreprocess.h
80 lines (69 loc) · 2.44 KB
/
hashPreprocess.h
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
// Takes in a string and returns a vector of the 8 bit ASCII representations of each letter
std::vector<std::bitset<8>> convertToBits(std::string msg) {
std::vector<std::bitset<8>> textAsBinary;
for (char letter : msg) {
textAsBinary.push_back(int(letter));
}
return textAsBinary;
}
// Pads the bits so that there is exactly 64 less than a multiple of 512
std::vector<std::bitset<8>> padBits(std::vector<std::bitset<8>> bits) {
// first find how much to pad
int numBits = sizeOfBitstream(bits);
int intervalToPad = 512;
int amountToPad;
int* pAmountToPad = &amountToPad;
while(true) {
if (numBits > (intervalToPad - 64)) {
intervalToPad += 512;
} else {
*pAmountToPad = intervalToPad - 64 - numBits;
break;
}
}
// Then pad with 1 + that many zeroes
for (int i = 0; i < (amountToPad / 8); i++) {
if (i == 0){
bits.push_back(128);
} else {
bits.push_back(0);
}
}
return bits;
}
// Final Padding is based on length
std::vector<std::bitset<8>> padBitsFromLength(std::vector<std::bitset<8>> bits, int bitSize) {
std::vector<std::bitset<8>> tempVector;
while (bitSize > 0) {
tempVector.insert(tempVector.begin(), std::bitset<8>(bitSize & 0xFF));
bitSize >>= 8;
}
for (int i = 8 - tempVector.size(); i > 0; i--){
bits.push_back(0);
}
for (std::bitset<8> byte : tempVector) {
bits.push_back(byte);
}
return bits;
}
// set first 16 Ws to be the 32 bit words from the 512 block
std::vector<std::bitset<32>> prepareFirstSixteen(std::vector<std::bitset<32>> msgScheduler, std::vector<std::bitset<8>> bits) {
// Each W is size 32
std::bitset<32> tempBits = {0};
// We append every 4 bytes (32 bits) from `bits` to `msgScheduler`
for (int i = 0; i <= 64; i++){
if (i % 4 == 0 && i > 0) {
msgScheduler.push_back(tempBits);
tempBits = std::bitset<32>(0);
}
// Set a temp mask equal to 00000...[the next byte from `bits`]
tempBits = rotateLeft(tempBits, 8);
std::bitset<32> tempMask = {0};
for (int index = 0; index < 8; index ++) {
tempMask[index] = bits[i][index];
}
// combine the mask with the tempBits
tempBits |= tempMask;
}
return msgScheduler;
}