I'm trying to run this code on Eclipse. But there is an error about the header mhash.h
"Error: No such a file or directory"
The code is this:
#include <mhash.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
MHASH td;
unsigned char buffer;
unsigned char *hash;
td = mhash_init(MHASH_MD5);
if (td == MHASH_FAILED) exit(1);
while (fread(&buffer, 1, 1, stdin) == 1) {
mhash(td, &buffer, 1);
}
hash = mhash_end(td);
printf("Hash:");
for (i = 0; i < mhash_get_block_size(MHASH_MD5); i++) {
printf("%.2x", hash[i]);
}
printf("\n");
exit(0);
}
You need to install mhash first. Get it from http://sourceforge.net/projects/mhash
Related
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define KEYSIZE 16
int main(){
FILE *fptr;
fptr = fopen("program.txt", "w");
if (fptr == NULL) {
printf("Error!");
exit(1);
}
int num_keys = 1524006529 - 1523999329;
const char *keys[num_keys];
int idx = 0;
int j;
for (j = 1523999329; j<= 1524006529; j++){
int i;
char key[KEYSIZE];
srand (j);
for (i = 0; i< KEYSIZE; i++){
key[i] = rand()%256;
printf("%.2x", (unsigned char)key[i]);
fprintf(fptr, "%.2x", (unsigned char)key[i]);
}
fprintf(fptr, "%s", "\n");
printf("\n");
keys[idx] = key;
idx ++;
}
fclose(fptr);
return 0;
}
I want to create a text file (program.txt) that has a new string key variable on each line. The code above produces a txt file that has this content though:
h?G??+?
Mp???+??
a?G????#*Jb`e0?H?????G??????
S6&}?Μ??G??OFf??|???s8f?G????ɼ???5q??(???K?G?? ??J????9?+???G?????cZ?n0?Jr?G?????#O9]??????jH??G???krT?̇)?A
?g?v?G??????h^(?^?
û/?G???^vB?~W?39???G??\????"?iJ_????G??p??>??????I???U?G????????:??o?RV???G??/?C?^?????|?۬?G??q#0.L$+??Nv?????G????u? L?Ϩ????dv?G??[??C「<??i???G??UyM??Ƭ/??CxXUě?G?????φ
:W&?G????=J???o???I??G???G%DA+"?~5?Z¡?G??øL#??O=e?b?u.O?G????*jjɥ+T?)?օ?G??2w }a,ȃ??tC?s?G??J"/c??Mn???f?y??G????p?Ay?c????~"<?G?????Ӆ?=%??#???G??o?\??<w???4WϚBE?G??}oh??(??#HK???&?G????Vfz
???e?s?G??/?Ye??iV?؛?,-te?G??I?Ҁx??5?B!b?4?G??6Z??9?P?GcB?}???Y?~|"??dd??G??????=?ms?
??G?????^??[H??\:???G??2(J??A??????G???????o#?i?/???G??P?5?5?5X??
What am I doing wrong here?
Is there a ready-made function in C that can list the contents of a directory using wildcards to filter out file names, for example, the equivalent of:
echo [!b]????
which shows the names of directory entries that are four characters long and do not start with "b"?
I know I can use scandir, but then, I need to provide my own filter function:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int filter(const struct dirent *entry)
{
if (strlen(entry->d_name) == 4 && entry->d_name[0] != 'b') return 1;
else return 0;
}
void main(void)
{
struct dirent **list;
int count;
count=scandir(".", &list, filter, alphasort)-1;
if (count < 0)
puts("Cannot open directory");
else
for (; count >= 0; count--)
puts(list[count]->d_name);
free(list);
}
Honestly, I am seriously considering actually calling shell to do it for me:
#include <stdio.h>
#include <stdlib.h>
void main(void)
{
FILE *fp;
char buffer[1024];
fp=popen("echo [!b]???", "r");
if (fp == NULL)
puts("Failed to run command.");
else
while (fgets(buffer, sizeof(buffer), fp) != NULL)
puts(buffer);
pclose(fp);
}
As mentioned in the comments the glob() function would be pretty good for this:
#include <stdio.h>
#include <glob.h>
int
main (void)
{
int i=0;
glob_t globbuf;
if (!glob("[!b]????", 0, NULL, &globbuf)) {
for (i=0; i <globbuf.gl_pathc; i++) {
printf("%s ",globbuf.gl_pathv[i]);
}
printf("\n");
globfree(&globbuf);
} else
printf("Error: glob()\n");
}
#include <stdio.h>
#include <stdlib.h>
void reverse(char* lines[], int count)
{
for (int i = count-1; i >= 0; i--)
{
printf("%s", lines[i]);
}
}
.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sortutil.h"
#include "reverse.h"
int getarray(char *lines[]);
void printarray(char *lines[], int max);
int main(int argc, char* argv[])
{
char* arr[100];
int numlines = getarray(arr);
printf("There are %d lines\n", numlines);
printarray(arr, numlines);
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-s") == 0)
{
sortutil(arr);
printarray(arr, numlines);
}
if (strcmp(argv[i], "-r") == 0)
{
reverse(arr, numlines);
printarray(arr, numlines);
}
}
}
int getarray(char *lines[])
{
int i = 0;
char *text = (char *)malloc(200);
while (fgets(text, 200, stdin) != NULL)
{
lines[i] = text;
i++;
text = (char *)malloc(200);
}
return i;
}
void printarray(char *lines[], int max)
{
for (int i = 0; i < max; i++)
{
printf("%s\n\n", lines[i]);
}
}
when i compile the main function it is telling me that there is an undefined reference to 'reverse'. I did #include "reverse.h" so it shouldn't have a problem seeing the reverse function. Am I missing something
You're missing the implementation. You defined the prototype, but the function body itself is missing. It is in a separate file, and you need to tell the linker about it. When you compile your main.cc - add the other file to the command line as well.
I have text file emails.txt..this is how the entry in the text file..
Emails.txt
abc#gmail.com
sfs#yahoo.com
i have to obtain the data from file and select 2 entries from the data randomly..
could anyone suggest me the technique to do this.
Thanks
You can read the file twice, first time to count the number of lines, then generate two random numbers in the range of 0 to number_of_lines found and then read the file again while looking for the lines you are interested in or you can do it like this:
Filename: emails.c
#include
int main (int argc, char **argv)
{
// open a handler to your file (read)
FILE *fp = fopen("emails.txt", "r");
// check if we have successfully opened the file for reading
if (fp != NULL)
{
// in your case 256 characters is enough for line size
// since emails are not that long but if longer buffer overflow
// is very possible and its not helpful as stackoverflow.com is :p
char line_buffer[256];
// count the number of lines read
unsigned int lines_read = 0;
// read up to line size or until EOL (End of Line) or EOF (End of File)
// will return NULL on error or eof
while (fgets(line_buffer, sizeof(line_buffer), fp) != NULL) {
// use rand() and seed it with the number of lines read
if ((rand() % ++lines_read) == 0) {
// do something with this line, it was randomly picked
// for the example, will print it on the screen
printf("%s \n", line_buffer);
}
}
// close file handler as we don't need it anymore
fclose(fp);
}
// return to the OS
return 0;
}
NOTE: This is C implementation, so save as .c file.
If you are using c++, and not just c -- you can use something like the following code:
#include <iostream>
#include <fstream>
#include <time.h>
#include <vector>
using namespace std;
int getrand(int num, int notnum)
{
int result = 0;
while (true)
{
result = abs(rand()) % num + 1;
if (result != notnum)
{
return result;
}
}
}
int main()
{
ifstream emails;
srand(time(NULL));
emails.open("emails.txt");
string email;
vector<string> emailVector;
while (emails >> email)
{
emailVector.push_back(email);
}
int index1 = getrand(emailVector.size(), 0);
int index2 = getrand(emailVector.size(), index1);
cout << "email 1: " << emailVector[index1 - 1] << endl;
cout << "email 2: " << emailVector[index2 - 1] << endl;
}
This will work:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BUF_SIZE 4096
#define RAND_COUNT 2
int count_lines(FILE *fp) {
char buf[BUF_SIZE];
int line_count=0;
fseek(fp, 0L, SEEK_SET);
while(fgets(buf, BUF_SIZE, fp) != NULL) {
line_count++;
}
fseek(fp, 0L, SEEK_SET);
return line_count;
}
int line_num(FILE *fp, char *buf, int line_num){
fseek(fp, 0L, SEEK_SET);
int i=0;
while(fgets(buf, BUF_SIZE, fp) != NULL) {
if (++i == line_num) {
return i;
}
}
return -1;
}
int main (int argc, const char * argv[]) {
FILE *fp=NULL;
char buf[BUF_SIZE];
char name[]="email.txt";
if((fp=fopen(name, "r"))==NULL){
printf("can't open: %s\n\n",name);
return -1;
}
int line_count=count_lines(fp);
printf("line count=%i\n",line_count);
srand ((unsigned int)time(NULL));
for (int i=1; i<=line_count; i++) {
line_num(fp,buf,i);
printf("%i = %s",i,buf);
}
for (int i=0; i<RAND_COUNT; i++) {
int a=(rand() % line_count);
line_num(fp,buf,a);
printf("line %i = %s\n",a,buf);
}
fclose(fp);
return 0;
}
I want to find md5sum of a file in Linux C, Is there any API where I can send file name to get md5sum of that file.
There's code here.
Also, the openssl libs have md5 functions (from here):
#include <openssl/md5.h>
#include <unistd.h>
int main()
{
int n;
MD5_CTX c;
char buf[512];
ssize_t bytes;
unsigned char out[MD5_DIGEST_LENGTH];
MD5_Init(&c);
bytes=read(STDIN_FILENO, buf, 512);
while(bytes > 0)
{
MD5_Update(&c, buf, bytes);
bytes=read(STDIN_FILENO, buf, 512);
}
MD5_Final(out, &c);
for(n=0; n<MD5_DIGEST_LENGTH; n++)
printf("%02x", out[n]);
printf("\n");
return(0);
}
You can use popen to run md5sum and read the output:
#include <stdio.h>
#include <ctype.h>
#define STR_VALUE(val) #val
#define STR(name) STR_VALUE(name)
#define PATH_LEN 256
#define MD5_LEN 32
int CalcFileMD5(char *file_name, char *md5_sum)
{
#define MD5SUM_CMD_FMT "md5sum %." STR(PATH_LEN) "s 2>/dev/null"
char cmd[PATH_LEN + sizeof (MD5SUM_CMD_FMT)];
sprintf(cmd, MD5SUM_CMD_FMT, file_name);
#undef MD5SUM_CMD_FMT
FILE *p = popen(cmd, "r");
if (p == NULL) return 0;
int i, ch;
for (i = 0; i < MD5_LEN && isxdigit(ch = fgetc(p)); i++) {
*md5_sum++ = ch;
}
*md5_sum = '\0';
pclose(p);
return i == MD5_LEN;
}
int main(int argc, char *argv[])
{
char md5[MD5_LEN + 1];
if (!CalcFileMD5("~/testfile", md5)) {
puts("Error occured!");
} else {
printf("Success! MD5 sum is: %s\n", md5);
}
}
You can use the mhash library (license is LGPL). On Debian systems:
sudo apt-get install libmhash-dev
See the man page man 3 mhash
But I don't think you can just give it the name of a file. You have to open the file yourself, read the data, and feed the data to this library's functions.
An easy answer to the question asked by Raja and using answer from sje397, the md5sum of a file can be calculated within the C program as below. Also notice that there is no need of writing the read command twice when you can use the do while loop.
int calculate_md5sum(char *filename)
{
//open file for calculating md5sum
FILE *file_ptr;
file_ptr = fopen(filename, "r");
if (file_ptr==NULL)
{
perror("Error opening file");
fflush(stdout);
return 1;
}
int n;
MD5_CTX c;
char buf[512];
ssize_t bytes;
unsigned char out[MD5_DIGEST_LENGTH];
MD5_Init(&c);
do
{
bytes=fread(buf, 1, 512, file_ptr);
MD5_Update(&c, buf, bytes);
}while(bytes > 0);
MD5_Final(out, &c);
for(n=0; n<MD5_DIGEST_LENGTH; n++)
printf("%02x", out[n]);
printf("\n");
return 0;
}
If you're looking to generate MD5 hash for a file and compare it with a string, you can use this.
Here, I have used D'Nabre's code from another SO answer and Michael Foukarakis's hex string to byte array code from this SO answer.
It needs to be linked against the OpenSSL library (gcc md5.c -o md5 -lssl) to work.
Sample usage:
unsigned char *file_hash = md5_for_file("~/testfile");
if (md5_is_match_str(file_hash, "b7be4ec867f9b0286b91dd40178774d6")) {
printf("Match\n");
} else {
printf("Mismatch\n");
}
free(file_hash);
md5.h:
#ifndef MD5_H
#define MD5_H
/** Caller to free result */
unsigned char *md5_for_file(char *filename);
/** md5_1 & md5_2 maybe NULL */
int md5_is_match(unsigned char *md5_1, unsigned char *md5_2);
/** md5 maybe NULL */
int md5_is_match_str(unsigned char *md5, const char *md5_str);
#endif //MD5_H
md5.c:
#include "md5.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
// Print the MD5 sum as hex-digits.
void print_md5_sum(unsigned char *md) {
int i;
for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
printf("%02x", md[i]);
}
printf("\n");
}
// Get the size of the file by its file descriptor
unsigned long get_size_by_fd(int fd) {
struct stat statbuf;
if (fstat(fd, &statbuf) < 0) exit(-1);
return statbuf.st_size;
}
unsigned char *md5_for_file(char *filename) {
int file_descript;
unsigned long file_size;
char *file_buffer;
unsigned char *result = malloc(sizeof(*result) * MD5_DIGEST_LENGTH);
if (NULL == result) {
printf("malloc failed\n");
goto END;
}
printf("using file:\t%s\n", filename);
file_descript = open(filename, O_RDONLY);
if (file_descript < 0) exit(-1);
file_size = get_size_by_fd(file_descript);
printf("file size:\t%lu\n", file_size);
file_buffer = mmap(0, file_size, PROT_READ, MAP_SHARED, file_descript, 0);
MD5((unsigned char *) file_buffer, file_size, result);
munmap(file_buffer, file_size);
print_md5_sum(result);
END:
return result;
}
int md5_is_match(unsigned char *md5_1, unsigned char *md5_2) {
if (!md5_1 || !md5_2) {
return 0;
}
int i;
for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
if (md5_1[i] != md5_2[i]) {
return 0;
}
}
return 1;
}
int md5_is_match_str(unsigned char *md5, char *md5_str) {
if (!md5 || !md5_str) { return 0; }
/** Make byte arrary from md5_str */
unsigned char md5_arr[MD5_DIGEST_LENGTH] = {0};
const char *pos = md5_str;
size_t count = 0;
/* WARNING: no sanitization or error-checking whatsoever */
for (count = 0; count < sizeof(md5_arr) / sizeof(md5_arr[0]); count++) {
sscanf(pos, "%2hhx", &md5_arr[count]);
pos += 2;
}
for (count = 0; count < sizeof(md5_arr) / sizeof(md5_arr[0]); count++) {
printf("%02x", md5_arr[count]);
}
printf("\n");
/** actual comparison */
if (memcmp(md5, md5_arr, MD5_DIGEST_LENGTH)) {
return 0;
}
return 1;
}