-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhash.c
More file actions
76 lines (64 loc) · 1.68 KB
/
hash.c
File metadata and controls
76 lines (64 loc) · 1.68 KB
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
#include "hash.h"
#include <crypto/hash.h>
#include <linux/err.h>
#include "file.h"
#include "const.h"
char* get_md5(char* input, size_t len){
struct crypto_shash* shash;
struct shash_desc* desc;
size_t size;
char* result = kmalloc(MD5_RESULT_SIZE, GFP_KERNEL);
shash = crypto_alloc_shash("md5", 0, 0);
if (IS_ERR(shash)) {
return NULL;
}
size = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
desc = kmalloc(size, GFP_KERNEL);
if (NULL == desc) {
crypto_free_shash(shash);
return NULL;
}
desc->tfm = shash;
crypto_shash_init(desc);
crypto_shash_update(desc, input, len);
crypto_shash_final(desc, result);
crypto_free_shash(desc->tfm);
kfree(desc);
return result;
}
void print_md5(char* input){
size_t i;
char hexline[MD5_RESULT_SIZE * 2 + 1];
memset(hexline, 0, (MD5_RESULT_SIZE * 2) + 1);
for (i = 0; i < MD5_RESULT_SIZE; i++) {
sprintf(hexline + (i * 2), "%02X", (u8)input[i]);
}
printk(KERN_INFO "MD5 result: %s\n", hexline);
}
char* get_file_md5(char* file_path){
struct file* file;
char* data;
int size;
char* md5 = NULL;
size_t file_size = get_file_size(file_path);
if (0 == file_size) {
return NULL;
}
data = kmalloc(file_size, GFP_KERNEL);
if (NULL == data) {
return NULL;
}
file = file_open(file_path, O_RDONLY, 0);
if (NULL == file) {
return NULL;
}
size = file_read(file, 0, data, file_size);
if (size != file_size) {
file_close(file);
return NULL;
}
md5 = get_md5(data, file_size);
kfree(data);
file_close(file);
return md5;
}