Internationalization of tinc.
[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
20 #include "config.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <time.h>
25
26 #include <xalloc.h>
27
28 #include "encr.h"
29
30 #include "system.h"
31
32 unsigned char initvec[] = { 0x22, 0x7b, 0xad, 0x55, 0x41, 0xf4, 0x3e, 0xf3 };
33
34 int main(int argc, char **argv)
35 {
36   FILE *fp;
37   int bits, c, i, bytes;
38   unsigned char *p;
39
40   if(argc > 2 || (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))))
41     {
42       fprintf(stderr, _("Usage: %s bits\n"), argv[0]);
43       return 1;
44     }
45
46   if(!argv[1])
47     argv[1] = "1024";
48   
49   if(!(bits = atol(argv[1])))
50     {
51       fprintf(stderr, _("Illegal number: %s\n"), argv[1]);
52       return 1;
53     }
54
55   bits = ((bits - 1) | 63) + 1;
56   fprintf(stderr, _("Generating %d bits number"), bits);
57   bytes = bits >> 3;
58
59   if((fp = fopen("/dev/urandom", "r")) == NULL)
60     {
61       perror(_("Opening /dev/urandom"));
62       return 1;
63     }
64
65   p = xmalloc(bytes);
66
67   setbuf(stdout, NULL);
68   for(i = 0; i < bytes; i++)
69     {
70       c = fgetc(fp);
71       if(feof(fp))
72         {
73           puts("");
74           fprintf(stderr, _("File was empty!\n"));
75         }
76       p[i] = c;
77     }
78   fclose(fp);
79
80   if(isatty(1))
81     {
82       fprintf(stderr, _(": done.\nThe following line should be ENTIRELY copied into a passphrase file:\n"));
83       printf("%d ", bits);
84       for(i = 0; i < bytes; i++)
85         printf("%02x", p[i]);
86       puts("");
87     }
88   else
89     {
90       printf("%d ", bits);
91       for(i = 0; i < bytes; i++)
92         printf("%02x", p[i]);
93       puts("");
94       fprintf(stderr, _(": done.\n"));
95     }
96
97   return 0;
98 }
99
100