Commit 0b9c16f2 authored by Bhavesh Yadav's avatar Bhavesh Yadav

Added hash function

parent 7a2dcaca
...@@ -8,11 +8,26 @@ ...@@ -8,11 +8,26 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <openssl/md5.h>
#define MAXLOCATIONS 32 #define MAXLOCATIONS 32
int keyToId(char * key){ int keyToId(char * key){
//To be filled unsigned char digest[16];
MD5_CTX context;
unsigned intHashValue;
unsigned char *hexVal;
MD5_Init(&context);
MD5_Update(&context, key, strlen(key));
MD5_Final(digest, &context);
hexVal = malloc(sizeof(char));
int twoDigits;
for(int i =0; i<2;i++){
sprintf(hexVal+i,"%02X",digest[i]);
sscanf(hexVal+i,"%x",&twoDigits);
intHashValue = intHashValue*1000 + twoDigits; // The two digits can also have 3 numerical digits hence *1000
}
return intHashValue;
} }
int nodeToId(char ip[], int port){ int nodeToId(char ip[], int port){
......
#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <openssl/md5.h>
#define MAXLOCATIONS 32
MD5_CTX context;
int keyToId(char * key){
unsigned char digest[16];
MD5_Init(&context);
MD5_Update(&context, key, strlen(key));
MD5_Final(digest, &context);
unsigned intHashValue;
unsigned char *hexVal;
hexVal = malloc(sizeof(char));
int TwoDigits;
for(int i =0; i<2;i++){
sprintf(hexVal+i,"%02X",digest[i]);
sscanf(hexVal+i,"%x",&TwoDigits);
intHashValue = intHashValue*1000 + TwoDigits;
}
printf("%d\n",intHashValue);
}
int main(int argc, char* argv[]) {
keyToId(argv[1]);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment