I have a string in my function defined like ..
char *key="anyvalue";
Now I use a linux command as …
$openssl dgst -md5 -hmac "anyvalue" file.txt
Now the problem is I need to carry out following task through a C function ..
Here is the code …
void func (char *key) {
char *key_new=key;
system("openssl -dgst md5 -hmac <got stuck here> file.txt");
}
How could I pass the key value to the portion labled ??
I did this pretty simply in php. …
$key="somevalue"
exec("openssl -dgst md5 -hmac $key file.txt");
Is there something similiar avaliable in C ???
If not then Please tell me any other possible way ???
Limitation :
The key has to be passed through function .
I can’t take it as a C command line argument.
Edit :
I tried with this one … but first of all I would like to mention that its a small file in a big project and warning being treated as error .. so I need to take care of them also
Here is what I did –
char *sstring=NULL;
sprintf(sstring, "openssl dgst -md5 -hmac \"%s\"
-out data3.md5 data3.txt",(char *)key);
system(sstring);
if I won’t initialize then here comes the warning ..
gcc -o hmacmd5.so -I.. -fPIC -fsigned-char -pipe -Wall
-Wpointer-arith -Wwrite-strings -Wstrict-prototypes -Wnested-externs
-Winline -Werror -g -Wcast-align -DSENDIP_LIBS=\"/usr/local/lib/sendip\"
-shared hmacmd5.c ../libsendipaux.a ../libsendipaux.a
cc1: warnings being treated as errors
hmacmd5.c: In function ‘xoricv’:
hmacmd5.c:271:9: error: ‘sstring’ is used uninitialized in this function
make: *** [hmacmd5.so] Error 1
I think you are looking for
sprintf:In your case, you would use it as follows:
EDIT:
After seeing the code you tried, I can see I didn’t provide you a complete answer.
You have two choices here (assume that
STRING_SIZEbelow is some#definedsize, like 300 or something):1) use a preallocated buffer:
2) use malloc/free:
I would suggest the first approach. Along with this, I would highly suggest taking care to use @pmg’s suggestion of
snprintf, if your compiler supports it. This would look like this: