MCPcopy Create free account
hub / github.com/BruceDevices/firmware / readDecryptedFile

Function readDecryptedFile

src/core/passwords.cpp:74–154  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

72*/
73
74String readDecryptedFile(FS &fs, String filepath) {
75
76 if (cachedPassword.length() == 0) {
77 cachedPassword = keyboard("", 32, "Password:", true);
78 if (cachedPassword.length() == 0 || cachedPassword == "\x1B") return ""; // cancelled
79 }
80
81 File cyphertextFile = fs.open(filepath, FILE_READ);
82 if (!cyphertextFile) return "";
83
84 String line;
85 String cypertextData = "";
86 String plaintext = "";
87 bool unsupported_params = false;
88
89 while (cyphertextFile.available()) {
90 line = cyphertextFile.readStringUntil('\n');
91 if (line.startsWith("Filetype:") && !line.endsWith("Bruce Encrypted File")) unsupported_params = true;
92 if (line.startsWith("Algo:") && !line.endsWith("XOR")) unsupported_params = true;
93 if (line.startsWith("KeyDerivationAlgo:") && !line.endsWith("MD5")) unsupported_params = true;
94 if (line.startsWith("KeyDerivationPasses:") && !line.endsWith("10"))
95 unsupported_params = true; // TODO: parse
96 if (line.startsWith("Data:")) cypertextData = line.substring(strlen("Data:"));
97 }
98
99 cyphertextFile.close();
100
101 if (unsupported_params || cypertextData.length() == 0) {
102 Serial.println("err: invalid Encrypted file (altered?)");
103 return "";
104 }
105
106 // else try decrypting
107 cypertextData.trim();
108 String cypertextDataDec = "";
109 cypertextDataDec.reserve(cypertextData.length());
110
111 uint8_t decimal = 0;
112 char temp[3]; // Temporary storage for each hex pair
113
114 for (int i = 0; i < cypertextData.length(); i += 3) {
115 // Converts two characters hex to a single byte
116
117 uint8_t highNibble = hexCharToDecimal(cypertextData[i]);
118 uint8_t lowNibble = hexCharToDecimal(cypertextData[i + 1]);
119 decimal = (highNibble << 4) | lowNibble;
120
121 // Serial.println((char) decimal);
122
123 // cypertextDataDec += decimal;
124 // cypertextDataDec_index += 1;
125
126 // temp[0] = cypertextData[i]; // First hex nibble
127 // temp[1] = cypertextData[i + 1]; // Second hex nibble
128 // temp[2] = '\0'; // Null-terminate the string
129
130 // Convert the hex pair to a byte (char)
131 // char decimal = (char) strtol(temp, NULL, 16);

Callers 3

loopSDFunction · 0.85
decryptFileCallbackFunction · 0.85
typeFileCallbackFunction · 0.85

Calls 9

hexCharToDecimalFunction · 0.85
xorEncryptDecryptMD5Function · 0.85
isValidAsciiFunction · 0.85
displayErrorFunction · 0.85
closeMethod · 0.80
keyboardFunction · 0.70
availableMethod · 0.45
readStringUntilMethod · 0.45
printlnMethod · 0.45

Tested by

no test coverage detected