The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reattemps Calculation using awk zanetti321 UNIX for Advanced & Expert Users 0 04-16-2008 06:13 AM
memory calculation rrlog Linux 0 08-08-2007 09:18 PM
awk calculation kekanap Shell Programming and Scripting 4 11-17-2006 07:39 AM
time calculation liux99 UNIX for Advanced & Expert Users 2 08-18-2005 10:33 PM
Math calculation help sickboy Shell Programming and Scripting 4 06-11-2005 01:22 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-23-2007
zedex zedex is offline
Registered User
  
 

Join Date: Feb 2007
Location: india,mumbai
Posts: 138
MD5 hash calculation

hi

i want to generate MD5 hash of string in unix (hp) i have the algorithm which takes file as argument and returns hash of file but when i tried to generate hash of "a" result was "60b725f10c9c85c70d97880dfe8191b3" hash but actually it should have been "0cc175b9c0f1b6a831c399e269772661" now i am not getting what is wrong here ???

is there any conversion required on string before giving to hash generator???
if yes in which form its required??

here is the main part of MD5.c
Code:
#ifdef TEST

#include <stdlib.h>
#include <stdio.h>

/*
 * those are the standard RFC 1321 test vectors
 */

static char *msg[] = 
{
    "",
    "a",
    "abc",
    "message digest",
    "abcdefghijklmnopqrstuvwxyz",
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
    "12345678901234567890123456789012345678901234567890123456789012" \
        "345678901234567890"
};

static char *val[] =
{
    "d41d8cd98f00b204e9800998ecf8427e",
    "0cc175b9c0f1b6a831c399e269772661",
    "900150983cd24fb0d6963f7d28e17f72",
    "f96b697d7cb7938d525a2f31aaf161d0",
    "c3fcd3d76192e4007dfb496cca67e13b",
    "d174ab98d277d9f5a5611c2c9f419d9f",
    "57edf4a22be3c955ac49da2e2107b67a"
};

int main( int argc, char *argv[] )
{
    FILE *f;
    int i, j;
    char output[33];
    md5_context ctx;
    unsigned char buf[1000];
    unsigned char md5sum[16];

    if( argc < 2 )
    {
        printf( "\n MD5 Validation Tests:\n\n" );

        for( i = 0; i < 7; i++ )
        {
            printf( " Test %d ", i + 1 );

            md5_starts( &ctx );
            md5_update( &ctx, (uint8 *) msg[i], strlen( msg[i] ) );
            md5_finish( &ctx, md5sum );

            for( j = 0; j < 16; j++ )
            {
                sprintf( output + j * 2, "%02x", md5sum[j] );
            }

            if( memcmp( output, val[i], 32 ) )
            {
                printf( "failed!\n" );
                return( 1 );
            }

            printf( "passed.\n" );
        }

        printf( "\n" );
    }
    else
    {
        if( ! ( f = fopen( argv[1], "rb" ) ) )
        {
            perror( "fopen" );
            return( 1 );
        }

        md5_starts( &ctx );

        while( ( i = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
        {
            md5_update( &ctx, buf, i );
        }

        md5_finish( &ctx, md5sum );

        for( j = 0; j < 16; j++ )
        {
            printf( "%02x", md5sum[j] );
        }

        printf( "  %s\n", argv[1] );
    }

    return( 0 );
}

#endif

Last edited by blowtorch; 02-23-2007 at 03:17 AM.. Reason: add code tags
  #2 (permalink)  
Old 02-23-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
I don't see md5.h being included. How does it know what a md5_context is?
  #3 (permalink)  
Old 02-23-2007
zedex zedex is offline
Registered User
  
 

Join Date: Feb 2007
Location: india,mumbai
Posts: 138
Quote:
Originally Posted by Perderabo
I don't see md5.h being included. How does it know what a md5_context is?

this is not full program if u want i can give you..
its just a bottom part of program ...upper part contains complex calculation so i skipped that part ...

i can give u link from where i took this program.
  #4 (permalink)  
Old 02-23-2007
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
Seems like a trailing newline (0x0a) sneaked in somewhere.

As you can see from this:

Code:
root@voiptest2:~# echo 'a' | md5sum
60b725f10c9c85c70d97880dfe8191b3  -
root@voiptest2:~# echo 'a' | hexdump -C
00000000  61 0a                                             |a.|
00000002
root@voiptest2:~# echo -n 'a' | md5sum
0cc175b9c0f1b6a831c399e269772661  -
So you took the code from http://xyssl.org/code/source/md5/, if you run the original self test, did you get the same result?
  #5 (permalink)  
Old 02-24-2007
zedex zedex is offline
Registered User
  
 

Join Date: Feb 2007
Location: india,mumbai
Posts: 138
Quote:
Originally Posted by cbkihong
Seems like a trailing newline (0x0a) sneaked in somewhere.

As you can see from this:

Code:
root@voiptest2:~# echo 'a' | md5sum
60b725f10c9c85c70d97880dfe8191b3  -
root@voiptest2:~# echo 'a' | hexdump -C
00000000  61 0a                                             |a.|
00000002
root@voiptest2:~# echo -n 'a' | md5sum
0cc175b9c0f1b6a831c399e269772661  -
So you took the code from http://xyssl.org/code/source/md5/, if you run the original self test, did you get the same result?


hey thanx man for your help the code at url you have mentioned is somewhat similar ..but that newline char worked man

$echo "message digest\c" >4
$a.out 4
f96b697d7cb7938d525a2f31aaf161d0 4
$

which is expected one.. now how can i write into file suppresing new line character because i am using md5 file to generate hash code with the input from cobol program so i need to have an intermediate function .....
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0