-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsha_find.h
88 lines (76 loc) · 1.71 KB
/
sha_find.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
86
87
88
#include <unistd.h>
#include<iostream>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
//#include <string.h>
#include<arpa/inet.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <type_traits>
#include <sstream>
#include<thread>
#include<bits/stdc++.h>
#include <openssl/sha.h>
using namespace std;
#define BUFSIZE 512
string do_f(FILE *f);
string ptt(unsigned char *md,int sum);
#ifndef _OSD_POSIX
//int read(int, void *, unsigned int);
#endif
string sha_search(string file_name)
{
// cout<<"hello sha"<<endl;
//FILE *fp = fopen(mt_name.c_str(),"w");
int i, err = 0;
FILE *IN;
IN = fopen(file_name.c_str(), "r");
if (IN == NULL) {
printf("JUMP");
}
string w=do_f(IN);
return w;
}
string do_f(FILE *f)
{
SHA_CTX c;
unsigned char md[SHA_DIGEST_LENGTH];
int fd;
int i;
unsigned char buf[BUFSIZE];
// cout<<"3"<<endl;
int sum=0;
fd = fileno(f);
SHA1_Init(&c);
for (;;) {
i = read(fd, buf, BUFSIZE);
if (i <= 0)
break;
//printf("%d\n",i);
sum=sum+i;
SHA1_Update(&c, buf, (unsigned long)i);
}
SHA1_Final(&(md[0]), &c);
fclose(f);
string t=ptt(md,sum);
return t;
}
string ptt(unsigned char *md,int sum)
{
//FILE *fp;
int i;
//printf("%d\n",SHA_DIGEST_LENGTH);
string s;
char fhash[SHA_DIGEST_LENGTH*2];
for (i = 0; i < SHA_DIGEST_LENGTH; i++)
{
//printf("%x\n",md[i]);
//fprintf(fp,"%02x", md[i]);
sprintf((char*)&(fhash[i*2]),"%02x",md[i]);
}
s=fhash;
// cout<<s<<endl;
return s;
}