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
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.
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 ***** */
41 #if defined(XP_WIN) || (defined(__sun) && !defined(SVR4))
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 *, ...);
52 HashTypeToOID(HASH_HashType hashtype
)
56 if (hashtype
<= HASH_AlgNULL
|| hashtype
>= HASH_AlgTOTAL
)
61 hashtag
= SEC_OID_MD2
;
64 hashtag
= SEC_OID_MD5
;
67 hashtag
= SEC_OID_SHA1
;
70 fprintf(stderr
, "A new hash type has been added to HASH_HashType.\n");
71 fprintf(stderr
, "This program needs to be updated!\n");
75 return SECOID_FindOIDByTag(hashtag
);
79 HashNameToOID(const char *hashName
)
84 for (htype
= HASH_AlgNULL
+ 1; htype
< HASH_AlgTOTAL
; htype
++) {
85 hashOID
= HashTypeToOID(htype
);
86 if (PORT_Strcasecmp(hashName
, hashOID
->desc
) == 0)
90 if (htype
== HASH_AlgTOTAL
)
102 "Usage: %s -t type [-i input] [-o output]\n",
104 fprintf(stderr
, "%-20s Specify the digest method (must be one of\n",
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",
117 fprintf(stderr
, "%-20s Define an output file to use (default is stdout)\n",
123 DigestFile(FILE *outFile
, FILE *inFile
, SECOidData
*hashOID
)
126 unsigned char ibuf
[4096], digest
[32];
131 hashcx
= PK11_CreateDigestContext(hashOID
->offset
);
132 if (hashcx
== NULL
) {
135 PK11_DigestBegin(hashcx
);
139 if (feof(inFile
)) break;
140 nb
= fread(ibuf
, 1, sizeof(ibuf
), inFile
);
141 if (nb
!= sizeof(ibuf
)) {
143 if (ferror(inFile
)) {
144 PORT_SetError(SEC_ERROR_IO
);
145 PK11_DestroyContext(hashcx
,PR_TRUE
);
152 rv
= PK11_DigestOp(hashcx
, ibuf
, nb
);
153 if (rv
!= SECSuccess
) {
154 PK11_DestroyContext(hashcx
, PR_TRUE
);
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
);
166 PORT_SetError(SEC_ERROR_IO
);
176 main(int argc
, char **argv
)
179 FILE *inFile
, *outFile
;
182 PLOptState
*optstate
;
186 progName
= strrchr(argv
[0], '/');
187 progName
= progName
? progName
+1 : argv
[0];
193 rv
= NSS_Init("/tmp");
194 if (rv
!= SECSuccess
) {
195 fprintf(stderr
, "%s: NSS_Init failed in directory %s\n",
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
) {
211 inFile
= fopen(optstate
->value
, "r");
213 fprintf(stderr
, "%s: unable to open \"%s\" for reading\n",
214 progName
, optstate
->value
);
220 outFile
= fopen(optstate
->value
, "w");
222 fprintf(stderr
, "%s: unable to open \"%s\" for writing\n",
223 progName
, optstate
->value
);
229 hashName
= strdup(optstate
->value
);
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
);
245 if (DigestFile(outFile
, inFile
, hashOID
)) {
246 fprintf(stderr
, "%s: problem digesting data (%s)\n",
247 progName
, SECU_Strerror(PORT_GetError()));
251 if (NSS_Shutdown() != SECSuccess
) {