home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / pwgenera.zip / key.c < prev    next >
C/C++ Source or Header  |  1996-07-12  |  2KB  |  82 lines

  1. /* key.c */
  2.  
  3. #include <os2.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "opie_cfg.h"
  8. #include "opie.h"
  9. #include "key.h"
  10.  
  11.  
  12. /* BOOL KeyParseChallenge(CString challenge, int &algorithm, int &sequence, CString &seed) */
  13. int KeyParseChallenge(const char *challenge, 
  14.                       int *algorithm, int *sequence, char *seed)
  15. {
  16.     static char s[256];
  17.     static char mesg[64];
  18.     char *t;
  19.  
  20.     if (*challenge == '\0')
  21.     {
  22.         return 0;
  23.     }
  24.  
  25.     strcpy(s, challenge);
  26.  
  27.     /* parse algorithm */
  28.     t = strtok(s, " ");
  29.     if (t == NULL) {
  30.         return 0;
  31.     }
  32.     if (!stricmp(t, "otp-md4") || !stricmp(t, "md4")) {
  33.         *algorithm = 4;
  34.         t = strtok(NULL, " ");
  35.     }
  36.     else if (!stricmp(t, "otp-md5") || !stricmp(t, "md5")) {
  37.         *algorithm = 5;
  38.         t = strtok(NULL, " ");
  39.     }
  40.     else
  41.         *algorithm = -1;
  42.  
  43.     /* parse sequence number */
  44.     if (t == NULL) {
  45.         return 0;
  46.     }
  47.     *sequence = atoi(t);
  48.     if (*sequence < 1) {
  49.         sprintf(mesg, "seq=%d, t=%s", *sequence, t);
  50.         return 0;
  51.     }
  52.    
  53.     /* parse seed */
  54.     t = strtok(NULL, " ");
  55.     if (t == NULL) {
  56.         return 0;
  57.     }
  58.     strcpy(seed, t);   
  59.  
  60.     return 1;
  61. }
  62.  
  63.  
  64. /* BOOL KeyGenerateResponse(int algorithm, int keynum, CString &seed, CString &password, CString &response) */
  65. int KeyGenerateResponse(int algorithm, int keynum, const char *seed, 
  66.                         const char *password, char *response)
  67. {
  68.     char key[8], buf[33];   
  69.  
  70.     /* Crunch seed and secret password into starting key normally */
  71.     if (opiekeycrunch((unsigned)algorithm, key, (char *)seed,
  72.                       (char *)password) != 0)
  73.         return 0;
  74.  
  75.     while (keynum-- != 0)
  76.         opiehash(key, algorithm);
  77.  
  78.     strcpy(response, opiebtoe(buf, key));
  79.  
  80.     return 1;
  81. }
  82.