ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i need to compute hash code for a file , currently im reading entire file at once and i perform hashing ,but i need to read a file chunk by chunk basis (say 512bytes) using a buffer and hash it and update it, im finding little difficult to read chunk by chunk basis ,please someone help me
Code:
long ComputeHashCode(string fname)
{
int length,buflen;
char * buffer;
unsigned long adler = 1L;
// checksum_calculator chk;
unsigned int cs = 0;
ifstream is;
// chk.init();
is.open (fname, ios::binary );
// get length of file:
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);
// allocate memory:
buffer = new char [length];
// read data as a block:
is.read (buffer,length);
is.close();
// cs=chk.calculate(buffer);
adler = update_adler32(adler,(unsigned char*) buffer, length);
// cout.write (buffer,length);
delete[] buffer;
return adler;
}
This would be a lot easier in C. Here's a starting point that's comparable to what you have:
Code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#define BUFFER_SIZE 512
unsigned long ComputeHashCode(const char *filename)
{
//allocate a buffer (you can use any method)
static char buffer[BUFFER_SIZE];
unsigned long adler = 1L;
//open the file without buffering
int current_file = open(filename, O_RDONLY);
if (current_file < 0)
{
fprintf(stderr, "could not open '%s': %s\n", filename, strerror(errno));
return 0;
}
//keep reading 'BUFFER_SIZE' bytes until there is nothing left to read
int read_size = 0;
while ((read_size = read(current_file, buffer, BUFFER_SIZE)) > 0)
{
// write(STDOUT_FILENO, buffer, read_size);
adler = update_adler32(adler, (unsigned char*) buffer, read_size);
}
//close the current file
close(current_file);
return adler;
}
int main(int argc, char *argv[])
{
//(this just loops through the filenames provided as arguments)
for (int I = 1; I < argc; I++)
{
unsigned long adler = ComputeHashCode(argv[I]);
fprintf(stdout, "%lu\n", adler);
}
return 0;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.