-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.c
118 lines (106 loc) · 2.38 KB
/
shared.c
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "hash.h"
#include <stdlib.h>
#include <openssl/ssl.h>
char *randKey(char *pswd, char *k)
{
int i, len;
int seed = atoi(pswd);
srand(seed);
for(i = 0; i < 16; i++) {
//srand((int) seed[i]);
int r = rand() % 10;
char buf[2];
sprintf(buf, "%d", r);
strcat(k, buf);
}
k[16] = '\0';
printf("key\n%s\n", k);
return k;
}
int Recv(SSL *ssl, void *buf, int size)
{
ssize_t result;
result = SSL_read(ssl, buf, size);
if(result < 0) {
perror("recv() failed");
if(errno == ECONNRESET) {
printf("connection bad or whatever");
}
return -1;
}
else
return result;
}
int Send(SSL *ssl, void *data, int size)
{
ssize_t result;
result = SSL_write(ssl, data, size);
if(result != size) {
perror("send() failed");
return -1;
}
else
return result;
}
int validateHash(char hashval[], char *data)
{
char hashBuf[2048/8];
hash(data, hashBuf);
if(strcmp(hashBuf, hashval) == 0)
return 1;
return 0;
}
/*
void RecvFile(char *filename, int sock, int encrypted, char *pswd)
{
unsigned int sizeNet, size;
char *data, hashBuf[2048/8], hashCmp[2048/8];
int x = Recv(sock, hashBuf, 64);
char *key = randomNum("hello");
hashBuf[x] = '\0';
Recv(sock, &sizeNet, sizeof(unsigned int));
size = ntohs(sizeNet);
printf("size %d\n", size);
data = malloc(size);
Recv(sock, data, size-1);
if(!encrypted)
data[size] = '\0';
if(encrypted) {
char *decrypted = malloc(size);
printf("key %s\n", key);
aes(key, data, size - 1, decrypted, 0);
data = decrypted;
printf("DECRYPT\n%s\n", decrypted);
size = strlen(decrypted);
free(key);
}
if(validateHash(hashBuf, data) == 1)
printf("valid\n");
else {
printf("invalid\n");
return;
}
FILE *writef = fopen(filename, "wb");
fwrite(data, size-1, 1, writef);
fclose(writef);
free(data);
}
*/
/*
int main()
{
char *pass, *randnum;
pass = "hellohello";
randnum = randomNum(pass);
printf("%s\n", randnum);
char *pass2 = "hellohello";
free(randnum);
randnum = randomNum(pass2);
printf("%s\n", randnum);
}
*/