A very simple hash function. Take input from stdin.
The perl version is available here.
#include <stdio.h>
#include <stdlib.h>
int main() {
int count = 0; /* number of characters seen */
FILE *in_file; /* input */
int modulus = 256;
int ch;
int total = 0;
in_file = fdopen(0, "r"); /* 0 is stdin */
if (in_file == NULL) {
printf("Error input%s\n");
exit(8);
}
while (1) {
ch = fgetc(in_file);
if ((ch == EOF) || (ch == 10))
break;
++count;
printf("\n%c %d",ch, ch);
total += ch;
total = total % modulus;
}
printf("\nNumber of characters of input is %d\n", count);
printf("\nResult : %d\n", total);
fclose(in_file);
return (0);
}
The result of the code
CGs-MacBook:Desktop chika$ ./a.out
elliptic curve cryptography
e 101
l 108
l 108
i 105
p 112
t 116
i 105
c 99
32
c 99
u 117
r 114
v 118
e 101
32
c 99
r 114
y 121
p 112
t 116
o 111
g 103
r 114
a 97
p 112
h 104
y 121
Number of characters of input is 27
Result : 231
For a slight different input:
Elliptic curve cryptography
E 69
l 108
l 108
i 105
p 112
t 116
i 105
c 99
32
c 99
u 117
r 114
v 118
e 101
32
c 99
r 114
y 121
p 112
t 116
o 111
g 103
r 114
a 97
p 112
h 104
y 121
Number of characters of input is 27
Result : 199
1 Response to “Simple Hash Function”