summaryrefslogtreecommitdiff
path: root/appl/cmd/auth/dsagen.b
blob: 51b50a127f5151f33e2ee4afb887be5d2d90827e (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
implement Dsagen;

include "sys.m";
	sys: Sys;

include "draw.m";

include "ipints.m";
	ipints: IPints;
	IPint: import ipints;

include "crypt.m";
	crypt: Crypt;

include "arg.m";

Dsagen: module
{
	init:	fn(nil: ref Draw->Context, nil: list of string);
};

init(nil: ref Draw->Context, args: list of string)
{
	sys = load Sys Sys->PATH;
	ipints = load IPints IPints->PATH;
	crypt = load Crypt Crypt->PATH;

	arg := load Arg Arg->PATH;
	arg->init(args);
	arg->setusage("auth/dsagen [-t 'attr=value attr=value ...']");
	tag: string;
	while((o := arg->opt()) != 0)
		case o {
		't' =>
			tag = arg->earg();
		* =>
			arg->usage();
		}
	args = arg->argv();
	if(args != nil)
		arg->usage();
	arg = nil;

	sk := crypt->dsagen(nil);
	if(tag != nil)
		tag = " "+tag;
	s := add("p", sk.pk.p);
	s += add("q", sk.pk.q);
	s += add("alpha", sk.pk.alpha);
	s += add("key", sk.pk.key);
	s += add("!secret", sk.secret);
	a := sys->aprint("key proto=dsa%s%s\n", tag, s);
	if(sys->write(sys->fildes(1), a, len a) != len a)
		error(sys->sprint("error writing key: %r"));
}

error(s: string)
{
	sys->fprint(sys->fildes(2), "dsagen: %s\n", s);
	raise "fail:error";
}

add(name: string, b: ref IPint): string
{
	return " "+name+"="+b.iptostr(16);
}