-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_hash.h
85 lines (77 loc) · 1.64 KB
/
mod_hash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
a fast non-cryptographic hash using then crc64-iso
polynome x^64 + x^4 + x^3 + x + 1 bytewise
Copyright (c) 2019 Jan-Daniel Kaplanski
MIT/X11 LICENSE
*/
#ifndef _MOD_HASH_H
#define _MOD_HASH_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <fcntl.h>
unsigned long long crc64(char *tohash, long length){
int i=0;
unsigned long long x64=0, x4=0,x3=0, num=0, buf=0, rval=0;
for(i=0;i<length;i++)
{
num = (unsigned long long) tohash[i];
x64 = pow(num,64);
x4 = pow(num,4);
x3 = pow(num,3);
buf = x64 + x4 +x3 + num + 1;
rval = rval + buf;
}
return rval;
}
int chkhsh(char tgz[512], char hfile[512]){
char * tohash = 0;
char * prehash = 0;
long lenhfile = 0, lentohash = 0;
unsigned long long hash = 0;
if((access(tgz, F_OK) != -1) && (access(hfile, F_OK) != -1))
{
FILE * ftgz = fopen(tgz, "rb");
FILE * fhfile = fopen(hfile, "rb");
if (ftgz)
{
fseek(ftgz, 0, SEEK_END);
lentohash = ftell(ftgz);
fseek(ftgz, 0, SEEK_SET);
tohash = malloc(lentohash);
if (tohash)
{
fread (tohash, 1, lentohash, ftgz);
}
fclose (ftgz);
}
if (fhfile)
{
fseek(fhfile, 0, SEEK_END);
lenhfile = ftell(fhfile);
fseek(fhfile, 0, SEEK_SET);
prehash = malloc(lenhfile);
if (prehash)
{
fread (prehash, 1, lenhfile, fhfile);
}
fclose (fhfile);
hash = strtoull(prehash, NULL, 16);
}
if (tohash && hash)
{
if (hash == (crc64(tohash, lentohash)))
{return 0;}
else
{return 1;}
}
else
{return 2;}
}
else
{return 2;}
return 1;
}
#endif