Skip to content

Instantly share code, notes, and snippets.

@etodanik
Created April 28, 2017 18:14
Show Gist options
  • Save etodanik/73bb6afbb82fd7c897575fd390bfc333 to your computer and use it in GitHub Desktop.
Save etodanik/73bb6afbb82fd7c897575fd390bfc333 to your computer and use it in GitHub Desktop.
loadKey() function for Shabak Challenge Part 2
std::vector<EncryptionStepDescriptor> loadKey(string path) {
int count = 0;
ifstream file;
// We're storing our loaded descriptors in a vector
std::vector<EncryptionStepDescriptor> descriptors;
file.open(path, ios::binary);
cout << "Reading Key..." << endl;
if (file.is_open()) {
while (file.good()) {
// This is our descriptor structure that is going to hold the data loaded directly from the binary
EncryptionStepDescriptor step;
if (
// Load the first byte (8 bits) of the file directly into the chunk of memory that corresponds
// to our `operationCode` member in `step`
!file.read(reinterpret_cast<char*>(&step.operationCode), sizeof(uint8_t))
) {
// file.good() will return true at the very end of the file, so we want to make sure we stop here
break;
}
// Load the second byte into `operationParameter` and the last 4 bytes (32 bit) into `step`
file.read(reinterpret_cast<char*>(&step.operationParameter), sizeof(uint8_t));
file.read(reinterpret_cast<char*>(&step.lengthToOperateOn), sizeof(uint32_t));
descriptors.push_back(step);
count++;
}
cout << "Loaded " << count << " steps" << endl;
}
else {
cout << "Could not open file" << endl;
}
return descriptors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment