summaryrefslogtreecommitdiff
path: root/tools/nettle-hash.c
blob: afb388e50fd3929d2ef48d84d40d50a3fcaa11a1 (plain)
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
231
232
/* nettle-hash.c
 *
 * General hashing tool. */

/* nettle, low-level cryptographics library
 *
 * Copyright (C) 2011 Niels Möller
 *  
 * The nettle library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * The nettle library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "nettle-meta.h"
#include "base16.h"

#include "getopt.h"
#include "misc.h"

#define BUFSIZE 16384

static void
list_algorithms (void)
{
  unsigned i;
  const struct nettle_hash *alg;
  printf ("%10s digestsize (internal block size), in units of octets\n", "name");

  for (i = 0; (alg = nettle_hashes[i]); i++)
    printf ("%10s %d (%d)\n",
	    alg->name, alg->digest_size, alg->block_size);
};

static const struct nettle_hash *
find_algorithm (const char *name)
{
  const struct nettle_hash *alg;
  unsigned i;
  
  for (i = 0; (alg = nettle_hashes[i]); i++)
    if (!strcmp(name, alg->name))
      return alg;

  return NULL;
}

/* Also in examples/io.c */
static int
hash_file(const struct nettle_hash *hash, void *ctx, FILE *f)
{
  for (;;)
    {
      char buffer[BUFSIZE];
      size_t res = fread(buffer, 1, sizeof(buffer), f);
      if (ferror(f))
	return 0;
      
      hash->update(ctx, res, buffer);
      if (feof(f))
	return 1;
    }
}

static int
digest_file(const struct nettle_hash *alg,
	    unsigned digest_length, int raw,
	    FILE *f)
{
  void *ctx;
  uint8_t *digest;
  ctx = xalloc(alg->context_size);

  alg->init(ctx);

  if (!hash_file (alg, ctx, f))
    {
      free (ctx);
      return 0;
    }

  digest = xalloc(digest_length);
  alg->digest(ctx, digest_length, digest);
  free(ctx);

  if (raw)
    fwrite (digest, digest_length, 1, stdout);

  else
    {
      unsigned i;
      char *hex = xalloc(BASE16_ENCODE_LENGTH(8) + 1);
      for (i = 0; i + 8 < digest_length; i += 8)
	{
	  base16_encode_update(hex, 8, digest + i);
	  hex[BASE16_ENCODE_LENGTH(8)] = 0;
	  printf("%s ", hex);
	}
      base16_encode_update(hex, digest_length - i, digest + i);
      hex[BASE16_ENCODE_LENGTH(digest_length - i)] = 0;
      printf("%s %s\n", hex, alg->name);
      free(hex);
    }
  
  free(digest);

  return 1;
}

/* FIXME: Be more compatible with md5sum and sha1sum. Options -c
   (check), -b (binary), -t (text), and output format with hex hash
   sum, optional star (meaning binary mode), and file name. */
int
main (int argc, char **argv)
{
  const char *alg_name = NULL;
  const struct nettle_hash *alg;
  unsigned length = 0;
  int raw = 0;
  int c;

  enum { OPT_HELP = 0x300, OPT_RAW, OPT_LIST };
  static const struct option options[] =
    {
      /* Name, args, flag, val */
      { "help", no_argument, NULL, OPT_HELP },
      { "version", no_argument, NULL, 'V' },
      { "algorithm", required_argument, NULL, 'a' },
      { "length", required_argument, NULL, 'l' },
      { "list", no_argument, NULL, OPT_LIST },
      { "raw", no_argument, NULL, OPT_RAW },

      { NULL, 0, NULL, 0 }
    };

  while ( (c = getopt_long(argc, argv, "Va:l:", options, NULL)) != -1)
    switch (c)
      {
      default:
	abort();
      case OPT_HELP:
	printf("nettle-hash -a ALGORITHM [OPTIONS] [FILE ...]\n"
	       "Options:\n"
	       "  --help              Show this help.\n"
	       "  -V, --version       Show version information.\n"
	       "  --list              List supported hash algorithms.\n"
	       "  -a, --algorithm=ALG Hash algorithm to use.\n"
	       "  -l, --length=LENGTH Desired digest length (octets)\n"
	       "  --raw               Raw binary output.\n");
	return EXIT_SUCCESS;
      case 'V':
	printf("nettle-hash (" PACKAGE_STRING ")\n");
	return EXIT_SUCCESS;
      case 'a':
	alg_name = optarg;
	break;
      case 'l':
	{
	  int arg;
	  arg = atoi (optarg);
	  if (arg <= 0)
	    die ("Invalid length argument: `%s'\n", optarg);
	  length = arg;
	}
	break;
      case OPT_RAW:
	raw = 1;
	break;
      case OPT_LIST:
	list_algorithms();
	return EXIT_SUCCESS;
      }

  if (!alg_name)
    die("Algorithm argument (-a option) is mandatory.\n"
	"See nettle-hash --help for further information.\n");
      
  alg = find_algorithm (alg_name);
  if (!alg)
    die("Hash algorithm `%s' not supported or .\n"
	"Use nettle-hash --list to list available algorithms.\n",
	alg_name);

  if (length == 0)
    length = alg->digest_size;
  else if (length > alg->digest_size)
    die ("Length argument %d too large for selected algorithm.\n",
	 length);
    
  argv += optind;
  argc -= optind;

  if (argc == 0)
    digest_file (alg, length, raw, stdin);
  else
    {
      int i;
      for (i = 0; i < argc; i++)
	{
	  FILE *f = fopen (argv[i], "rb");
	  if (!f)
	    die ("Cannot open `%s': %s\n", argv[i], STRERROR(errno));
	  printf("%s: ", argv[i]);
	  if (!digest_file (alg, length, raw, f))
	    die("Reading `%s' failed: %s\n", argv[i], STRERROR(errno));
	  fclose(f);
	}
    }
  if (fflush(stdout) != 0 )
    die("Write failed: %s\n", STRERROR(errno));

  return EXIT_SUCCESS;
}