MD5 hash calculation


 
Thread Tools Search this Thread
Top Forums Programming MD5 hash calculation
# 1  
Old 02-23-2007
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  
Old 02-23-2007
I don't see md5.h being included. How does it know what a md5_context is?
# 3  
Old 02-23-2007
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  
Old 02-23-2007
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  
Old 02-24-2007
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 .....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to reverse the b64 format(encoded b64(b64(md5($pass)))) into md5 hash format?

I have about 1500 rows (encoded b64(b64(md5($pass))) algorythm) in a file. I would like reverse the b64 into md5 hash format. How could I do that from command line? So I need only the correct md5 hash formats. These row format: 4G5qc2WQzGES6QkWAUgl5w P9tKxonBOg3ymr8vOBLnDA... (1 Reply)
Discussion started by: freeroute
1 Replies

2. Shell Programming and Scripting

Apply md5 hash to a field in csv file

I have a .csv file and I want to md5 hash the second column for each row in the file. File is something like data1,foobar1,123,345 data2,foobar2,456,9393 data3,foobar3,1002,10109 Output would be like data1,6c81243028f8e455fa617dd5f0232ce1,123,345... (3 Replies)
Discussion started by: jjwags
3 Replies

3. Shell Programming and Scripting

Perl giving unexpected md5 hash values

I am trying to speed up creating a line by line hash file from a huge file using Perl. Here is my current (working but too slow) Bash code: (while read line; do hash=$(echo -n $line | md5sum); echo ${hash:0:32}; done)And here is my Perl code: perl -MDigest::MD5 -le 'foreach $line ( <STDIN> )... (3 Replies)
Discussion started by: Michael Stora
3 Replies

4. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

5. Shell Programming and Scripting

Create md5 sums and archive the resulting md5 files

Hello everyone, I am looking to basically creating md5sum files for all iso files in a directory and archive the resulting md5 files into a single archive in that very same directory. I worked out a clumsy solution such as: #find files for which md5sum are to be created and store the... (1 Reply)
Discussion started by: SurfTranquille
1 Replies

6. UNIX for Dummies Questions & Answers

How to convert MD5 hash into shadow format?

I am trying to use John the Ripper but it doesn't take regular MD5 hashes, only shadow MD5 hashes. For example this hash: 900150983cd24fb0d6963f7d28e17f72 (which, decrypted, is 'abc') within a text file, John the Ripper does not detect because it is not in shadow format. How can I convert this MD5... (2 Replies)
Discussion started by: guitarscn
2 Replies

7. Programming

SHA1 hash calculation

hi i want to generate SHA1 hash of string in Linux (atmark) and downloaded the XYSSL-0.9 version code for the same.i have the algorithm which takes file as argument and returns hash of file . And of same file while I generated the key using online tools then it doesn't match with my compiled... (4 Replies)
Discussion started by: ahsaas42
4 Replies

8. Shell Programming and Scripting

MD5 hash filename

I am a newbie to shell programming. Can someone help me with the following ? Shell script that accomplishes the following - # Step 1 List all files in the directory # Step 2 Loop through each filename say a.htm and MD5 hash the filename to say b.htm # Step 3 copy a.htm to b.htm # Step 4... (1 Reply)
Discussion started by: bebar
1 Replies

9. Programming

md5 hash a string or char array in SCO

Can someone provide me with code to md5 hash a string or character array in the SCO environment. All help is appreciated thanks. (5 Replies)
Discussion started by: jcarter2333
5 Replies

10. Shell Programming and Scripting

How to create md5 Hash variable?

I have a script that runs the grub-md5-crypt command based on whether the pass_value variable is a non-zero string. The md5 hash is being created in the /opt/hostconfigs/$HOST file, but I can't echo $md5_value. It is blank. Is there a way to create and echo a md5 hash variable? if then... (1 Reply)
Discussion started by: cstovall
1 Replies
Login or Register to Ask a Question