Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / cmd / digest / digest.c
blob7a37856d9cc53a02890e1df45dd847b58d5b31fc
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #include "secutil.h"
38 #include "pk11func.h"
39 #include "secoid.h"
41 #if defined(XP_WIN) || (defined(__sun) && !defined(SVR4))
42 #if !defined(WIN32)
43 extern int fread(char *, size_t, size_t, FILE*);
44 extern int fwrite(char *, size_t, size_t, FILE*);
45 extern int fprintf(FILE *, char *, ...);
46 #endif
47 #endif
49 #include "plgetopt.h"
51 static SECOidData *
52 HashTypeToOID(HASH_HashType hashtype)
54 SECOidTag hashtag;
56 if (hashtype <= HASH_AlgNULL || hashtype >= HASH_AlgTOTAL)
57 return NULL;
59 switch (hashtype) {
60 case HASH_AlgMD2:
61 hashtag = SEC_OID_MD2;
62 break;
63 case HASH_AlgMD5:
64 hashtag = SEC_OID_MD5;
65 break;
66 case HASH_AlgSHA1:
67 hashtag = SEC_OID_SHA1;
68 break;
69 default:
70 fprintf(stderr, "A new hash type has been added to HASH_HashType.\n");
71 fprintf(stderr, "This program needs to be updated!\n");
72 return NULL;
75 return SECOID_FindOIDByTag(hashtag);
78 static SECOidData *
79 HashNameToOID(const char *hashName)
81 HASH_HashType htype;
82 SECOidData *hashOID;
84 for (htype = HASH_AlgNULL + 1; htype < HASH_AlgTOTAL; htype++) {
85 hashOID = HashTypeToOID(htype);
86 if (PORT_Strcasecmp(hashName, hashOID->desc) == 0)
87 break;
90 if (htype == HASH_AlgTOTAL)
91 return NULL;
93 return hashOID;
96 static void
97 Usage(char *progName)
99 HASH_HashType htype;
101 fprintf(stderr,
102 "Usage: %s -t type [-i input] [-o output]\n",
103 progName);
104 fprintf(stderr, "%-20s Specify the digest method (must be one of\n",
105 "-t type");
106 fprintf(stderr, "%-20s ", "");
107 for (htype = HASH_AlgNULL + 1; htype < HASH_AlgTOTAL; htype++) {
108 fprintf(stderr, HashTypeToOID(htype)->desc);
109 if (htype == (HASH_AlgTOTAL - 2))
110 fprintf(stderr, " or ");
111 else if (htype != (HASH_AlgTOTAL - 1))
112 fprintf(stderr, ", ");
114 fprintf(stderr, " (case ignored))\n");
115 fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
116 "-i input");
117 fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
118 "-o output");
119 exit(-1);
122 static int
123 DigestFile(FILE *outFile, FILE *inFile, SECOidData *hashOID)
125 int nb;
126 unsigned char ibuf[4096], digest[32];
127 PK11Context *hashcx;
128 unsigned int len;
129 SECStatus rv;
131 hashcx = PK11_CreateDigestContext(hashOID->offset);
132 if (hashcx == NULL) {
133 return -1;
135 PK11_DigestBegin(hashcx);
138 for (;;) {
139 if (feof(inFile)) break;
140 nb = fread(ibuf, 1, sizeof(ibuf), inFile);
141 if (nb != sizeof(ibuf)) {
142 if (nb == 0) {
143 if (ferror(inFile)) {
144 PORT_SetError(SEC_ERROR_IO);
145 PK11_DestroyContext(hashcx,PR_TRUE);
146 return -1;
148 /* eof */
149 break;
152 rv = PK11_DigestOp(hashcx, ibuf, nb);
153 if (rv != SECSuccess) {
154 PK11_DestroyContext(hashcx, PR_TRUE);
155 return -1;
159 rv = PK11_DigestFinal(hashcx, digest, &len, 32);
160 PK11_DestroyContext(hashcx, PR_TRUE);
162 if (rv != SECSuccess) return -1;
164 nb = fwrite(digest, 1, len, outFile);
165 if (nb != len) {
166 PORT_SetError(SEC_ERROR_IO);
167 return -1;
170 return 0;
173 #include "nss.h"
176 main(int argc, char **argv)
178 char *progName;
179 FILE *inFile, *outFile;
180 char *hashName;
181 SECOidData *hashOID;
182 PLOptState *optstate;
183 PLOptStatus status;
184 SECStatus rv;
186 progName = strrchr(argv[0], '/');
187 progName = progName ? progName+1 : argv[0];
189 inFile = NULL;
190 outFile = NULL;
191 hashName = NULL;
193 rv = NSS_Init("/tmp");
194 if (rv != SECSuccess) {
195 fprintf(stderr, "%s: NSS_Init failed in directory %s\n",
196 progName, "/tmp");
197 return -1;
201 * Parse command line arguments
203 optstate = PL_CreateOptState(argc, argv, "t:i:o:");
204 while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
205 switch (optstate->option) {
206 case '?':
207 Usage(progName);
208 break;
210 case 'i':
211 inFile = fopen(optstate->value, "r");
212 if (!inFile) {
213 fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
214 progName, optstate->value);
215 return -1;
217 break;
219 case 'o':
220 outFile = fopen(optstate->value, "w");
221 if (!outFile) {
222 fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
223 progName, optstate->value);
224 return -1;
226 break;
228 case 't':
229 hashName = strdup(optstate->value);
230 break;
234 if (!hashName) Usage(progName);
236 if (!inFile) inFile = stdin;
237 if (!outFile) outFile = stdout;
239 hashOID = HashNameToOID(hashName);
240 if (hashOID == NULL) {
241 fprintf(stderr, "%s: invalid digest type\n", progName);
242 Usage(progName);
245 if (DigestFile(outFile, inFile, hashOID)) {
246 fprintf(stderr, "%s: problem digesting data (%s)\n",
247 progName, SECU_Strerror(PORT_GetError()));
248 return -1;
251 if (NSS_Shutdown() != SECSuccess) {
252 exit(1);
255 return 0;