1d2645760375358f5d9ae8a7e718c6bd19693c82
[tinc] / src / genauth.c
1 /*
2     genauth.c -- generate a random passphrase
3     Copyright (C) 1998,1999,2000 Ivo Timmermans <zarq@iname.com>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19     $Id: genauth.c,v 1.7 2000/05/31 18:21:27 zarq Exp $
20 */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <time.h>
27
28 #include <xalloc.h>
29
30 #include "encr.h"
31
32 #include "system.h"
33
34 unsigned char initvec[] = { 0x22, 0x7b, 0xad, 0x55, 0x41, 0xf4, 0x3e, 0xf3 };
35
36 int main(int argc, char **argv)
37 {
38   FILE *fp;
39   int bits, c, i, bytes;
40   unsigned char *p;
41
42   setlocale (LC_ALL, "");
43   bindtextdomain (PACKAGE, LOCALEDIR);
44   textdomain (PACKAGE);
45
46   if(argc > 2 || (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))))
47     {
48       fprintf(stderr, _("Usage: %s bits\n"), argv[0]);
49       return 1;
50     }
51
52   if(!argv[1])
53     argv[1] = "1024";
54   
55   if(!(bits = atol(argv[1])))
56     {
57       fprintf(stderr, _("Illegal number: %s\n"), argv[1]);
58       return 1;
59     }
60
61   bits = ((bits - 1) | 63) + 1;
62   fprintf(stderr, _("Generating %d bits number"), bits);
63   bytes = bits >> 3;
64
65   if((fp = fopen("/dev/urandom", "r")) == NULL)
66     {
67       perror(_("Opening /dev/urandom"));
68       return 1;
69     }
70
71   p = xmalloc(bytes);
72
73   setbuf(stdout, NULL);
74   for(i = 0; i < bytes; i++)
75     {
76       c = fgetc(fp);
77       if(feof(fp))
78         {
79           puts("");
80           fprintf(stderr, _("File was empty!\n"));
81         }
82       p[i] = c;
83     }
84   fclose(fp);
85
86   if(isatty(1))
87     {
88       fprintf(stderr, _(": done.\nThe following line should be ENTIRELY copied into a passphrase file:\n"));
89       printf("%d ", bits);
90       for(i = 0; i < bytes; i++)
91         printf("%02x", p[i]);
92       puts("");
93     }
94   else
95     {
96       printf("%d ", bits);
97       for(i = 0; i < bytes; i++)
98         printf("%02x", p[i]);
99       puts("");
100       fprintf(stderr, _(": done.\n"));
101     }
102
103   return 0;
104 }
105
106