LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2012, 01:02 AM   #1
shamjs
Member
 
Registered: Sep 2011
Posts: 93

Rep: Reputation: Disabled
adler32 hashing


Hi all,

i need an help on adler32 algorithm here is my code

Code:
#include<stdio.h>
#include<iostream>
#include <fstream>
#include <stdlib.h>
#include<cstdlib>
#include<conio.h>
#include<string>




#include<string.h>

#include <sys/stat.h>


#define BASE 65521 /* largest prime smaller than 65536 */

      /*
         Update a running Adler-32 checksum with the bytes buf[0..len-1]
       and return the updated checksum. The Adler-32 checksum should be
       initialized to 1.

       Usage example:

         unsigned long adler = 1L;

         while (read_buffer(buffer, length) != EOF) {
           adler = update_adler32(adler, buffer, length);
         }
         if (adler != original_adler) error();
      */
      unsigned long update_adler32(unsigned long adler,
         unsigned char *buf, int len)
      {
        unsigned long s1 = adler & 0xffff;
        unsigned long s2 = (adler >> 16) & 0xffff;
        int n;

        for (n = 0; n < len; n++) {
          s1 = (s1 + buf[n]) % BASE;
          s2 = (s2 + s1)     % BASE;
        }
        return (s2 << 16) + s1;
      }
using namespace std;
	  void main()
	  {
	     unsigned long adler = 1L;
	     ofstream myfile;
             myfile.open ("example.txt");
             myfile << "this is an adler32 algorithm for computing hash code of a given file";
             myfile <<"\ncomputing using adler32 , using the HMAC (Hash Message Authentication Code) ";
		
		 myfile.close();

	    
	     

		
		 
		
		 
		 //Open file
		
	

		string STRING;
		ifstream infile;
		infile.open ("example.txt");
		string previousLine="";
		while(!infile.eof()) 
		{
			getline(infile,STRING); 
			if (STRING != previousLine)
			{
				previousLine=STRING;
				cout<<STRING<<endl; 
			        int strnglen =STRING.length();
				adler = update_adler32(adler, (unsigned char*)STRING.c_str(),strnglen);
			}

		}
		infile.close();

		  
			 
	  }
here is my contents of example.txt file

this is an adler32 algorithm for computing hash code of a given file
computing using adler32 , using the HMAC (Hash Message Authentication Code)

when i hash this file i get hash code as 2351772333
but when i compare my hash code with http://hash.online-convert.com/adler32-generator by giving example.txt as input to this online hash generator im getting hash code as c3eb32c4
decimal= 3286971076

i think im having problem during STRING update for sucessive(next) lines

since i get matching hash code when i write only "this is an adler32 algorithm for computing hash code of a given file"

hash code generated by my code=1230510179
hash code generated online =1230510179(decimal)

but if i write both lines to example.txt
and then hash it ,its not matching
please help me

Last edited by shamjs; 04-06-2012 at 01:15 AM.
 
Old 04-06-2012, 01:48 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
In C++, getline() discards the delimiter (newline). Your code is not computing the checksum for the file, just on the lines excluding the newlines.

A quick hack would be to assume that every line has a newline (this is not necessarily true!), and add
Code:
adler = update_adler32(adler, "\n", 1);
after the line checksum update.

A better option would be to read the file into memory as is, say using code similar to this example at cplusplus.com, and then checksum the entire file.

Here is an example program written in C99 you can use to compare against.
Code:
#define  _POSIX_C_SOURCE 200809L
#include <unistd.h>
#include <fcntl.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>

#define  ADLER32_BASE 65521

uint32_t adler32(uint32_t const sum, const int descriptor, off_t *const length)
{
    unsigned char buffer[32768]; /* Uses a lot of stack! */
    ssize_t       n;
    int           saved_errno;
    off_t         total = 0;
    unsigned int  sum1 = sum & 0xFFFFU;
    unsigned int  sum2 = (sum >> 16U) & 0xFFFFU;

    saved_errno = errno;

    while (1) {

        n = read(descriptor, buffer, sizeof buffer);
        if (n > (ssize_t)0) {
            const unsigned char       *head = buffer;
            const unsigned char *const tail = buffer + n;

            total += (off_t)n;

            while (head < tail) {
                sum1 = (sum1 + *(head++)) % ADLER32_BASE;
                sum2 = (sum2 + sum1)      % ADLER32_BASE;
            }

            continue;

        } else
        if (n == (ssize_t)-1 && errno == EINTR)
            continue;

        if (n == (ssize_t)-1)
            saved_errno = errno;
        else
        if (n != (ssize_t)0)
            saved_errno = EIO;

        if (length)
            *length = total;

        errno = saved_errno;

        return (uint32_t)sum1 + ((uint32_t)sum2 << 16U);
    }
}

int main(int argc, char *argv[])
{
    uint32_t sum;
    off_t    length;
    int      arg, fd, result;

    if (argc < 2) {
        fprintf(stderr, "\nUsage: %s FILE(s)...\n\n", argv[0]);
        return 0;
    }

    for (arg = 1; arg < argc; arg++) {

        do {
            fd = open(argv[arg], O_RDONLY | O_NOCTTY);
        } while (fd == -1 && errno == EINTR);
        if (fd == -1) {
            fprintf(stderr, "%s: %s.\n", argv[arg], strerror(errno));
            return 1;
        }

        errno = 0;
        sum = adler32(1, fd, &length);
        if (errno) {
            fprintf(stderr, "%s: %s.\n", argv[arg], strerror(errno));
            return 1;
        }

        printf("%s: 0x%08lx = %lu (%lu bytes)\n",
               argv[arg], (unsigned long)sum, (unsigned long)sum, (unsigned long)length);
        fflush(stdout);

        do {
            result = close(fd);
        } while (result == -1 && errno == EINTR);
        if (result == -1) {
            fprintf(stderr, "%s: Error closing file: %s.\n", argv[arg], strerror(errno));
            return 1;
        }
    }

    return 0;
}
The online Adler32 generator you linked to does not seem to work for me. It modifies its input (converting LF newlines to CRLF pairs, and so on), and seems to fail on all file inputs I try with it.

On the other hand, this one provides the exact same results as my example program above. (To verify you hashed the same data, you can use sha256sum to check the SHA256 hash of your test file, and compare it to the online one.)

Hope this helps,

Last edited by Nominal Animal; 04-06-2012 at 04:22 AM.
 
1 members found this post helpful.
Old 04-09-2012, 07:46 AM   #3
shamjs
Member
 
Registered: Sep 2011
Posts: 93

Original Poster
Rep: Reputation: Disabled
Hi Nominal Animal,

thanks for you inputs, basically there was a problem in reading delimiter's so i have used http://www.cplusplus.com/reference/i.../istream/read/ as suggested by you thanks a lot
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hashing vs Encryption thunyiwe Linux - Security 2 08-21-2011 03:48 PM
Help hashing ip sockets yaplej Programming 2 11-11-2010 08:19 PM
Address hashing techniques Sunilsbjoshi Programming 2 12-02-2007 01:55 PM
Help with cpp + tokenize + hashing kshkid Programming 2 12-29-2006 08:06 AM
Linux and MD5 hashing GAVollink Programming 0 06-04-2003 01:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration