xref: /NextBSD/contrib/opie/libopie/keycrunch.c (revision eb1a5f8de9f7ea602c373a710f531abbf81141c4)
1 /* keycrunch.c: The opiekeycrunch() library function.
2 
3 %%% copyright-cmetz-96
4 This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
5 The Inner Net License Version 3 applies to this software.
6 You should have received a copy of the license with this software. If
7 you didn't get a copy, you may request one from <license@inner.net>.
8 
9 	History:
10 
11 	Modified by cmetz for OPIE 2.4. Use struct opie_otpkey for arg.
12 	Created by cmetz for OPIE 2.3 using the old keycrunch.c as a guide.
13 */
14 
15 #include "opie_cfg.h"
16 
17 #if HAVE_STRING_H
18 #include <string.h>
19 #endif /* HAVE_STRING_H */
20 #if HAVE_STDLIB_H
21 #include <stdlib.h>
22 #endif /* HAVE_STDLIB_H */
23 #include <ctype.h>
24 
25 #include "opie.h"
26 
27 int opiekeycrunch FUNCTION((algorithm, result, seed, secret), int algorithm AND
28 struct opie_otpkey *result AND char *seed AND char *secret)
29 {
30   int i, rval = -1;
31   char *c;
32 
33   if (!result || !seed || !secret)
34     return 1;
35 
36   i = strlen(seed) + strlen(secret);
37   if (!(c = malloc(i + 1)))
38     return -1;
39 
40   {
41     char *c2 = c;
42 
43     if (algorithm & 0x10)
44       while(*c2 = *(secret++)) c2++;
45 
46     while(*seed)
47       if (isspace(*(c2++) = tolower(*(seed++))))
48 	goto kcret;
49 
50     if (!(algorithm & 0x10))
51       strcpy(c2, secret);
52   }
53 
54   opiehashlen(algorithm & 0x0f, c, result, i);
55   rval = 0;
56 
57 kcret:
58   {
59     char *c2 = c;
60     while(*c2)
61       *(c2++) = 0;
62   }
63 
64   free(c);
65   return rval;
66 }
67