I've got the following code to calculate sha_256 of an input, and i wonder how can know what is the optimal data chunk size for each iteration of CC_SHA256_Update. Does it constant value or a variable that depend on system environment ?
CC_SHA256_CTX sha256;
CC_SHA256_Init(&sha256);
const long bufSize = 32768; //how can i find the optimized size ?
char* buffer = (char *) malloc(bufSize);
int bytesRead = 0;
if(!buffer) {
return -1;
}
while((bytesRead = (int) fread(buffer, 1, bufSize, file))) {
CC_SHA256_Update(&sha256, buffer, bytesRead);
}
EDIT : I've tried a different approach as described in the selected answer below, and acquire the data using mmap (rather then malloc+fread). unfortunately, id didn't improve run time efficiency (it slightly increased)
int fsize(const char *filename) {
struct stat st;
if (stat(filename, &st) == 0)
return st.st_size;
return -1;
}
int fd = open(path, O_RDONLY);
int sz = fsize(path);
char * buffer = mmap((caddr_t)0, sz, PROT_READ , MAP_SHARED, fd, 0);
CC_SHA256_CTX sha256;
CC_SHA256_Init(&sha256);
CC_SHA256_Update(&sha256, buffer, sz);
CC_SHA256_Final(output, &sha256);
close(fd);
return 0;