]> git.saurik.com Git - apple/security.git/blob - sec/SecurityTool/digest_calc.c
Security-55471.14.tar.gz
[apple/security.git] / sec / SecurityTool / digest_calc.c
1 //
2 // digest_calc.c
3 // Digest calculation command for SecurityTool
4 //
5 // Created by John Kelley on 4/27/11.
6 // Copyright 2011 Apple, Inc. All rights reserved.
7 //
8
9 #include "builtin_commands.h"
10
11 #include <stdlib.h>
12 #include <strings.h>
13
14 #include <SecurityTool/readline.h>
15
16 #include <corecrypto/ccsha1.h>
17 #include <corecrypto/ccsha2.h>
18
19 extern int command_digest(int argc, char * const *argv)
20 {
21 int result = 1;
22 uint8_t *data = NULL;
23 size_t data_len;
24 const struct ccdigest_info *di;
25 unsigned char *digest = NULL;
26 unsigned long i,j;
27
28 if (argc < 3)
29 return 2; /* Return 2 triggers usage message. */
30
31 if (strcasecmp("sha1", argv[1]) == 0)
32 {
33 //printf("Calculating sha1\n");
34 di = ccsha1_di();
35 }
36 else if (strcasecmp("sha256", argv[1]) == 0)
37 {
38 //printf("Calculating sha256\n");
39 di = ccsha256_di();
40 }
41 else if (strcasecmp("sha512", argv[1]) == 0)
42 {
43 //printf("Calculating sha256\n");
44 di = ccsha512_di();
45 }
46 else
47 return 2; /* Return 2 triggers usage message. */
48
49 digest = malloc(di->output_size);
50 if (!digest)
51 goto exit;
52
53 for (i = 2; i < (unsigned int)argc; ++i)
54 {
55 printf("%s(%s)= ", argv[1], argv[i]);
56 if (read_file( argv[i], &data, &data_len) != 0 || !data)
57 {
58 printf("error reading file\n");
59 continue;
60 }
61 ccdigest(di, data_len, data, digest);
62
63 for (j = 0; j < di->output_size; j++)
64 printf("%02x", digest[j]);
65 printf("\n");
66 free(data);
67 data = NULL;
68 }
69 result = 0;
70
71 exit:
72 if(data)
73 free(data);
74 if (digest)
75 free(digest);
76
77 return result;
78 }