1 | /* |
---|
2 | * MD5 Shell Command Implmentation |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.rtems.org/license/LICENSE. |
---|
7 | */ |
---|
8 | |
---|
9 | #ifdef HAVE_CONFIG_H |
---|
10 | #include "config.h" |
---|
11 | #endif |
---|
12 | |
---|
13 | #include <errno.h> |
---|
14 | #include <fcntl.h> |
---|
15 | #include <stdio.h> |
---|
16 | #include <stdlib.h> |
---|
17 | #include <string.h> |
---|
18 | #include <sys/types.h> |
---|
19 | #include <unistd.h> |
---|
20 | |
---|
21 | #include <rtems.h> |
---|
22 | #include <rtems/shell.h> |
---|
23 | |
---|
24 | #include <md5.h> |
---|
25 | |
---|
26 | #define BUFFER_SIZE (4 * 1024) |
---|
27 | |
---|
28 | static int rtems_shell_main_md5( |
---|
29 | int argc, |
---|
30 | char *argv[]) |
---|
31 | { |
---|
32 | uint8_t* buffer; |
---|
33 | uint8_t hash[16]; |
---|
34 | size_t h; |
---|
35 | |
---|
36 | buffer = malloc(BUFFER_SIZE); |
---|
37 | if (!buffer) |
---|
38 | { |
---|
39 | printf("error: no memory\n"); |
---|
40 | return 1; |
---|
41 | } |
---|
42 | |
---|
43 | --argc; |
---|
44 | ++argv; |
---|
45 | |
---|
46 | while (argc) |
---|
47 | { |
---|
48 | struct stat sb; |
---|
49 | MD5_CTX md5; |
---|
50 | int in; |
---|
51 | |
---|
52 | if (stat(*argv, &sb) < 0) |
---|
53 | { |
---|
54 | free(buffer); |
---|
55 | printf("error: stat of %s: %s\n", *argv, strerror(errno)); |
---|
56 | return 1; |
---|
57 | } |
---|
58 | |
---|
59 | in = open(*argv, O_RDONLY); |
---|
60 | if (in < 0) |
---|
61 | { |
---|
62 | free(buffer); |
---|
63 | printf("error: opening %s: %s\n", *argv, strerror(errno)); |
---|
64 | return 1; |
---|
65 | } |
---|
66 | |
---|
67 | MD5Init(&md5); |
---|
68 | |
---|
69 | while (sb.st_size) |
---|
70 | { |
---|
71 | ssize_t size = sb.st_size > BUFFER_SIZE ? BUFFER_SIZE : sb.st_size; |
---|
72 | |
---|
73 | if (read(in, buffer, size) != size) |
---|
74 | { |
---|
75 | close(in); |
---|
76 | free(buffer); |
---|
77 | printf("error: reading %s: %s\n", *argv, strerror(errno)); |
---|
78 | return 1; |
---|
79 | } |
---|
80 | |
---|
81 | MD5Update(&md5, buffer, size); |
---|
82 | |
---|
83 | sb.st_size -= size; |
---|
84 | } |
---|
85 | |
---|
86 | MD5Final(hash, &md5); |
---|
87 | |
---|
88 | close(in); |
---|
89 | |
---|
90 | printf("MD5 (%s) = ", *argv); |
---|
91 | for (h = 0; h < sizeof(hash); ++h) |
---|
92 | printf("%02x", (int) hash[h]); |
---|
93 | printf("\n"); |
---|
94 | |
---|
95 | --argc; |
---|
96 | ++argv; |
---|
97 | } |
---|
98 | |
---|
99 | free(buffer); |
---|
100 | |
---|
101 | return 0; |
---|
102 | } |
---|
103 | |
---|
104 | rtems_shell_cmd_t rtems_shell_MD5_Command = { |
---|
105 | "md5", /* name */ |
---|
106 | "md5 [file ...]", /* usage */ |
---|
107 | "files", /* topic */ |
---|
108 | rtems_shell_main_md5, /* command */ |
---|
109 | NULL, /* alias */ |
---|
110 | NULL /* next */ |
---|
111 | }; |
---|