I am trying to get the hash values of md5 of a string. I am on Redhat Linux. using the 25-27 field in the file I need to generate the md5 and append it at the end of the record as a new field.
I have tried the below code but its painfully slow. can you please suggest any alternatives or help me tune it?
awk -F"\�" '{ print $25" " $26 " " $27 }' /var/IBM/CMA/LandingArea/Analysis/Add.txt | while read x ; do echo $x |md5sum ; done
$ cat md5line.c
#include <openssl/md5.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main()
{
int n;
MD5_CTX c;
unsigned char buf[1024], out[MD5_DIGEST_LENGTH];
while(fgets(buf, 1024, stdin))
{
MD5_Init(&c);
MD5_Update(&c, buf, strlen(buf)-1);
MD5_Final(out, &c);
for(n=0; n<MD5_DIGEST_LENGTH; n++)
printf("%02x", out[n]);
fputs("\n",stdout);
}
return(0);
}
$ gcc md5line.c -o md5line -lssl # May need "openssl-dev" or something like that
$ printf "%s\n" a b c d e
a
b
c
d
e
$ echo "a" | md5sum # This includes the \n!
60b725f10c9c85c70d97880dfe8191b3 -
$ printf "a" | md5sum # Does not add \n
0cc175b9c0f1b6a831c399e269772661 -
$ printf "%s\n" a b c d e | ./md5line # My code also ignores \n
0cc175b9c0f1b6a831c399e269772661
92eb5ffee6ae2fec3ad71c777531578f
4a8a08f09d37b73795649038408b5f33
8277e0910d750195b448797616e091ad
e1671797c52e15f763380b45e841ec32
$
Thanks for the code. I did try to compile but I get the error message as shown below. I have no c\c++ skills to be able to debug it. can you please help me?
g++ -c -O -fPIC -Wno-deprecated -m64 -mtune=generic -mcmodel=small md5tester.cpp -o libmd5teser.so
md5tester.cpp:1:25: error: openssl/md5.h: No such file or directory
md5tester.cpp: In function �int main()':
md5tester.cpp:9: error: �MD5_CTX' was not declared in this scope
md5tester.cpp:9: error: expected `;' before �c'
md5tester.cpp:10: error: �MD5_DIGEST_LENGTH' was not declared in this scope
md5tester.cpp:12: error: invalid conversion from �unsigned char*' to �char*'
md5tester.cpp:12: error: initializing argument 1 of �char* fgets(char*, int, FILE*)'
md5tester.cpp:14: error: �c' was not declared in this scope
md5tester.cpp:14: error: �MD5_Init' was not declared in this scope
md5tester.cpp:15: error: invalid conversion from �unsigned char*' to �const char*'
md5tester.cpp:15: error: initializing argument 1 of �size_t strlen(const char*)'
md5tester.cpp:15: error: �MD5_Update' was not declared in this scope
md5tester.cpp:16: error: �out' was not declared in this scope
md5tester.cpp:16: error: �MD5_Final' was not declared in this scope
#!/usr/bin/perl -w
use Digest::MD5 qw(md5 md5_hex md5_base64);
open my $DAT, $ARGV[0] or die "Could not open $ARGV[0]: $!";
while (my $line = <$DAT>) {
chomp $line;
my @fld = split /�/, $line;
print $line . "�" . md5_hex($fld[24]." ".$fld[25]." ".$fld[26] . "\n") . "\n";
}
close $DAT;
This is because the separator character you listed doesn't match so it's not finding 26 fields. I might need on od -c dump of a line to get the proper value of �
Thanks. I noticed that the string is being generated and appended to the new file.
Here is the octal dump
0000000 X X X 254 X X X | 1 0 0 0 0 4 254 1
0000020 6 X X X X X X X X 254 254 254 X X
0000040 X X X X X X X X 254 X X 254 2 4 X
0000060 8 254 5 1 7 8 9 1 3 1 254 X X X 254 254
0000100 254 254 254 254 1 6 254 254 X X X X X x D
0000120 r 254 254 X X X x X X X X X X 254 X
0000140 S W 254 1 4 2 8 254 A U S T R A L I
0000160 A 254 5 X X X 9 1 3 1 254 254 1 6 254 A
0000200 X X X X X D r 254 R 9 1 3 0 0 0
0000220 0 0 0 6 0 : A U S \r \n
0000233
Edit: This is not equivalent to Chubler's suggestion and it only prints the hashes. Oh well. At least I spent a little time kicking some rust off my very puny perl muscles.
As I said, you will probably need to install the openssh-dev library or whatever your distribution happens to call it. That's why md5.h is missing, because it's not installed.
I doubt you will get much out of the .h file without the library (.so files) on your system, as the .h file just defines the functions exported from the library.
Anything compiled will not link without the library.
I would be astonished if Redhat didn't come with openssl...
But he doesn't need "a header file" -- he needs openssl's header files, a bunch of them, and the right ones for his version of openssl, correctly installed and configured in the correct places.