I am working on an rc4 encryption implementation for s3fs and I'd like to better optimize my design so I'd like feedback on my encryption/decryption here. Below is the code which accepts a file descriptor of an open file for a parameter:void crypty(int fd){    // Initial steps, read the file into a buffer    ssize_t placeHolder; // Needed to reduce warnings in compilation    int fileLength = lseek(fd, 0, SEEK_END); // use of system call to measure offset in accordance to the end of the file, yielding the size    unsigned char* fileContents; // generate a new c-string pointer    fileContents = (unsigned char*) calloc(fileLength, sizeof(char)); // assign that pointer to a block of memmory allocated by calloc() that has enough cells for the full file size and the size of each cell is the size of a char or 1byte    placeHolder = pread(fd, fileContents, fileLength, 0); // read from the file of the file descriptor into the file-contents of size up to the size of fileLength or the length of the file  // Cast the results into a string for temporary storage    string plain(reinterpret_cast<char*>(fileContents), fileLength); // strongest cast to the bit level to ensure string is converted fully from c-string ideal for pointers     int length = plain.size(); // sets length to the length of the string     unsigned char *buf = (unsigned char*)malloc(length+1); // allocates requested memory of a certain size and then sets the pointer to it to be the pointer I declared prior     memset(buf, 0, length+1); // fills the memory with blank placeholder, chosen to be 0     // Grabbing password     ifstream pass; // opens file stream     string code; // makes string to grab password     pass.open(pass.txt); // opens password file     if(!pass.good()){        code = morganna; // Fail safe in case s3 doesn't open my file        pass.close();     }     else{         //Take the passphrase given and turn it into an md5 hash for use in rc4         getline(pass,code);         pass.close();     }        // Starting open-ssl styled md5 conversion         char codeConverted[code.length()]; // makes an equivalent length c-string         strcpy(codeConverted,code.c_str()); // makes an equivalent length copy of the string         unsigned char encryptedPass[MD5_DIGEST_LENGTH]; // generates an array to begin md-5 hashing         char convertedPass[16]; // makes a c-string to hold the conversion         MD5((unsigned char*)&codeConverted, strlen(codeConverted), (unsigned char*)&encryptedPass); // conduct hash         for (int i = 0; i < 16; i++){         sprintf(&mdString[i*2], %02x, (unsigned int)encryptedPass[i]); // takes each digit in hex and converts to ascii equivalence for readability in testing         }         for (int i = 0; i < 16;i++){             convertedPass[i] = encryptedPass[i]; // copy fully converted password into new string         }         // Do encryption         RC4_KEY key;         RC4_set_key(&key,sizeof(convertedPass), (const unsigned char*)convertedPass);         RC4(&key, length, (const unsigned char*)plain.c_str(), buf);         // Write the encryption back to the old file         string result((char*)buf, length);         free(buf); // deallocate memory used prior         // Final result         const char *outputBuf = result.c_str();         placeHolder = pwrite(fd, outputBuf, result.length(), 0); // Writes WITHOUT changing the offset from prior         placeHolder = ftruncate(fd, result.length()); // truncates file         return; // returns blank because function is void }
Optimizing rc4 encryption
c++;openssl