answersLogoWhite

0

How do you write a c program for hashing?

Updated: 8/11/2023
User Avatar

DrFabianski

Lvl 1
11y ago

Best Answer

- Hashing is the transformation of a string of characters into a usually shorter fixed-length value or key that represents the original string. Hashing is used to index and retrieve items in a database because it is faster to find the item using the shorter hashed key than to find it using the original value. It is also used in many encryption algorithms. As a simple example of the using of hashing in databases, a group of people could be arranged in a database like this: Abernathy, Sara Epperdingle, Roscoe Moore, Wilfred Smith, David (and many more sorted into alphabetical order) Each of these names would be the key in the database for that person's data. A database search mechanism would first have to start looking character-by-character across the name for matches until it found the match (or ruled the other entries out). But if each of the names were hashed, it might be possible (depending on the number of names in the database) to generate a unique four-digit key for each name. For example: 7864 Abernathy, Sara 9802 Epperdingle, Roscoe 1990 Moore, Wilfred 8822 Smith, David (and so forth) A search for any name would first consist of computing the hash value (using the same hash function used to store the item) and then comparing for a match using that value. It would, in general, be much faster to find a match across four digits, each having only 10 possibilities, than across an unpredictable value length where each character had 26 possibilities. The hashing algorithm is called the hash function (and probably the term is derived from the idea that the resulting hash value can be thought of as a "mixed up" version of the represented value). In addition to faster data retrieval, hashing is also used to encrypt and decrypt digital signatures (used to authenticate message senders and receivers). The digital signature is transformed with the hash function and then both the hashed value (known as a message-digest) and the signature are sent in separate transmissions to the receiver. Using the same hash function as the sender, the receiver derives a message-digest from the signature and compares it with the message-digest it also received. They should be the same. The hash function is used to index the original value or key and then used later each time the data associated with the value or key is to be retrieved. Thus, hashing is always a one-way operation. There's no need to "reverse engineer" the hash function by analyzing the hashed values. In fact, the ideal hash function can't be derived by such analysis. A good hash function also should not produce the same hash value from two different inputs. If it does, this is known as a collision. A hash function that offers an extremely low risk of collision may be considered acceptable. Here are some relatively simple hash functions that have been used: * The division-remainder method: The size of the number of items in the table is estimated. That number is then used as a divisor into each original value or key to extract a quotient and a remainder. The remainder is the hashed value. (Since this method is liable to produce a number of collisions, any search mechanism would have to be able to recognize a collision and offer an alternate search mechanism.) * Folding: This method divides the original value (digits in this case) into several parts, adds the parts together, and then uses the last four digits (or some other arbitrary number of digits that will work ) as the hashed value or key. * Radix transformation: Where the value or key is digital, the number base (or radix) can be changed resulting in a different sequence of digits. (For example, a decimal numbered key could be transformed into a hexadecimal numbered key.) High-order digits could be discarded to fit a hash value of uniform length. * Digit rearrangement: This is simply taking part of the original value or key such as digits in positions 3 through 6, reversing their order, and then using that sequence of digits as the hash value or key. A hash function that works well for database storage and retrieval might not work as for cryptographic or error-checking purposes. There are several well-known hash functions used in cryptography. These include the message-digest hash functions MD2, MD4, and MD5, used for hashing digital signatures into a shorter value called a message-digest, and the Secure Hash Algorithm (SHA), a standard algorithm, that makes a larger (60-bit) message digest and is similar to MD4.

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

#include<stdio.h>

#include<conio.h>

struct hashing

{

int data;

int chain;

};

typedef struct hashing hash;

int hashkey(int);

int lprep();

int lpwithch();

int display(int[10]);

int main()

{

int ch;

clrscr();

printf("\n1.linear probing with replacement\n2.Chaining without replacement\n3.Exit");

printf("\nEnter ur choice:");

scanf("%d",&ch);

switch(ch)

{

case 1: lprep();

break;

case 2: lpwithch();

break;

case 3: exit(0);

default:printf("\nInvalid choice");

}

getch();

return 0;

}

int hashkey(int num)

{

int key;

key=num%10;

return key;

}

int lprep()

{

int no,temp,k=0;

int table[10],ch;

int num,key,i,n;

char p;

for(i=0;i<10;i++)

{

table[i]=-1;

}

do

{

printf("\nEnter the no which u wanna enter in hash table:");

scanf("%d",&num);

key=hashkey(num);

if(table[key]==-1)

{

table[key]=num;

}

else

{

no=hashkey(table[key]);

if(no!=key)

{

temp=table[key];

table[key]=num;

num=temp;

}

while(table[key]!=-1)

{

key++;

}

if(key>9)

{

key=0;

while(table[key]!=-1)

{

key++;

}

}

table[key]=num;

}

printf("\nDo u want to add more no(y/n):");

flushall();

p=getchar();

k++;

}while(((p=='Y')(p=='y'))&&(k<10));

display(table);

return 0;

}

int display(int table[10])

{

int i;

printf("\nMy hash table\n");

printf("\nlocation\tValue");

for(i=0;i<10;i++)

{

printf("\n%d\t\t%d",i,table[i]);

}

return 0;

}

int lpwithch()

{

int i,pos,choice,num,val,temp,x,k=0;

char p;

hash hasharray[10];

for(i=0;i<10;i++)

{

hasharray[i].chain=-1;

hasharray[i].data=-1;

}

do

{

printf("Enter the no you want to insert in Hash table:");

scanf("%d",&num);

pos=hashkey(num);

val=pos;

if(hasharray[pos].data==-1)

{

hasharray[pos].data=num;

}

else

{

while(hasharray[pos].data!=-1)

{

pos++;

}

if(pos>9)

{

pos=0;

while(hasharray[pos].data!=-1)

{

pos++;

}

}

hasharray[pos].data=num;

}

for(i=0;i<=9;i++)

{

if(hashkey(hasharray[i].data)==val)

{

if(hasharray[i].chain==-1 && hasharray[i].data!=num)

{

hasharray[i].chain=pos;

}

}

}

printf("\nDo u want to add more no(y/n):");

flushall();

p=getchar();

k++;

}while(((p=='Y')(p=='y'))&&(k<10));

printf("\nMy hash table\n");

printf("\nlocation\tValue\t\tChain");

for(i=0;i<10;i++)

{

printf("\n%d\t\t%d\t\t%d",i,hasharray[i].data,hasharray[i].chain);

}

return 0;

}

Written by: Fabianski Benjamin

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

how to program using hash functions? how to program using hash functions?

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a c program for hashing?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a c program for creating a virus?

a c program for creat a virus


Where do we write main function in a c program?

Into the source program.


Write a c program to find Volume and surface area of cube?

Write a c program to compute the surface area and volume of a cube


How do you write a c program to analysis a circuit?

There is no need to write a C program for circuit analysis; there are lots of packages out there that already do that (for example, Spice).


How do you write a C program to check whether the number is odd or even Ps-using DEV complier?

To write a C program to determine if something is odd or even you need to be a programmer. To write a program in C is complicate and only done by programmers.


How do you write socket program in c?

For first find an example program.


How do you write a program in embedded c?

Use a text-editor program.


Write a program in c for checksum?

yes


What is the program to write route cipher?

c#


Write a program to illustrate the usage of pointers with arrarys and functions?

* * * * * * * * * * write the c++ program and show me brifily?


How do you write an Algorithm for a C plus plus Program?

You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.


Write Client and server program in C language using UDP?

Write and run a client and a server program in C-language using UDP