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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
.. _mozilla_projects_nss_nss_sample_code_sample1:
sample1
=======
.. container::
1. A program to compute the hash of a file and save it to another file.
.. code:: c
/* NSPR Headers */
#include <prprf.h>
#include <prtypes.h>
#include <plgetopt.h>
#include <prio.h>
#include <prprf.h>
/* NSS headers */
#include <secoid.h>
#include <secmodt.h>
#include <sechash.h>
typedef struct {
const char *hashName;
SECOidTag oid;
} NameTagPair;
/* The hash algorithms supported */
static const NameTagPair HASH_NAMES[] = {
{ "MD2", SEC_OID_MD2 },
{ "MD5", SEC_OID_MD5 },
{ "SHA1", SEC_OID_SHA1 },
{ "SHA256", SEC_OID_SHA256 },
{ "SHA384", SEC_OID_SHA384 },
{ "SHA512", SEC_OID_SHA512 }
};
/* Maps a hash name to a SECOidTag.
* Returns NULL if the name is not a supported algorithm
*/
static SECOidTag HashNameToOIDTag(const char *hashName)
{
int i, nhashes = sizeof(HASH_NAMES);
SECOidTag hashtag = SEC_OID_UNKNOWN;
for (i = 0; i < nhashes; i++) {
if (PORT_Strcasecmp(hashName, HASH_NAMES[i].hashName) == 0) {
hashtag = HASH_NAMES[i].oid;
break;
}
}
return hashtag;
}
/* Newline */
static void Newline(PRFileDesc* out)
{
PR_fprintf(out, "\n");
}
/* PrintAsHex */
void PrintAsHex(PRFileDesc* out, unsigned char *data, unsigned int len)
{
unsigned i;
int column;
unsigned int limit = 15;
unsigned int level = 1;
column = level;
if (!len) {
PR_fprintf(out, "(empty)\n");
return;
}
for (i = 0; i < len; i++) {
if (i != len - 1) {
PR_fprintf(out, "%02x:", data[i]);
column += 3;
} else {
PR_fprintf(out, "%02x", data[i]);
column += 2;
break;
}
if (column > 76 || (i % 16 == limit)) {
Newline(out);
column = level;
limit = i % 16;
}
}
if (column != level) {
Newline(out);
}
}
/* Prints a usage message and exits */
static void Usage(const char *progName)
{
int htype;
int HASH_AlgTOTAL = sizeof(HASH_NAMES) / sizeof(HASH_NAMES[0]);
fprintf(stderr, "Usage: %s -t type [ < input ] [ > output ]\n", progName);
fprintf(stderr, "%-20s Specify the digest method (must be one of\n",
"-t type");
fprintf(stderr, "%-20s ", "");
for (htype = 0; htype < HASH_AlgTOTAL; htype++) {
fprintf(stderr, HASH_NAMES[htype].hashName);
if (htype == (HASH_AlgTOTAL - 2))
fprintf(stderr, " or ");
else if (htype != (HASH_AlgTOTAL - 1))
fprintf(stderr, ", ");
}
fprintf(stderr, " (case ignored))\n");
fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
"< input");
fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
"> output");
exit(-1);
}
/* Check for the missing arguments */
static void
PrintMsgAndExit(const char *progName, char opt)
{
fprintf(stderr, "%s: option -%c requires an argument\n", progName, opt);
Usage(progName);
}
#define REQUIRE_ARG(opt,value) if (!(value)) PrintMsgAndExit(progName, opt)
/* Digests a file according to the specified algorithm.
* It writes out the digest as a hexadecimal string.
*/
static int
DigestFile(PRFileDesc *outFile, PRFileDesc *inFile, SECOidTag hashOIDTag)
{
unsigned int nb;
unsigned char ibuf[4096];
unsigned char digest[64];
unsigned int len;
unsigned int digestLen;
HASH_HashType hashType;
HASHContext *hashContext = NULL;
hashType = HASH_GetHashTypeByOidTag(hashOIDTag);
hashContext = HASH_Create(hashType);
if (hashContext == NULL) {
return SECFailure;
}
do {
HASH_Begin(hashContext);
/* Incrementally hash the file contents */
while ((nb = PR_Read(inFile, ibuf, sizeof(ibuf))) > 0) {
HASH_Update(hashContext, ibuf, nb);
}
HASH_End(hashContext, digest, &len, 64);
/* Normally we would write it out in binary with
* nb = PR_Write(outFile, digest, len);
* but for illustration let's print it in hex.
*/
PrintAsHex(outFile, digest, len);
} while (0);
/* cleanup */
if (hashContext != NULL)
HASH_Destroy(hashContext);
return SECSuccess;
}
/*
* This sample computes the hash of a file and saves it to another file. It illustrates the use of NSS message APIs.
*/
int main(int argc, char **argv)
{
SECOidTag hashOIDTag;
PLOptState *optstate;
PLOptStatus status;
SECStatus rv;
char *hashName = NULL;
char *progName = strrchr(argv[0], '/');
progName = progName ? progName + 1 : argv[0];
rv = NSS_NoDB_Init("/tmp");
if (rv != SECSuccess) {
fprintf(stderr, "%s: NSS_Init failed in directory %s\n", progName, "/tmp");
return -1;
}
/* Parse command line arguments */
optstate = PL_CreateOptState(argc, argv, "t:");
while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
switch (optstate->option) {
case 't':
REQUIRE_ARG(optstate->option, optstate->value);
hashName = strdup(optstate->value);
break;
}
}
if (!hashName)
Usage(progName);
/* convert and validate */
hashOIDTag = HashNameToOIDTag(hashName);
if (hashOIDTag == SEC_OID_UNKNOWN) {
fprintf(stderr, "%s: invalid digest type - %s\n", progName, hashName);
Usage(progName);
}
/* Digest it and print the result */
rv = DigestFile(PR_STDOUT, PR_STDIN, hashOIDTag);
if (rv != SECSuccess) {
fprintf(stderr, "%s: problem digesting data (%d)\n", progName, PORT_GetError());
}
rv = NSS_Shutdown();
if (rv != SECSuccess) {
exit(-1);
}
return 0;
}
|