anastasis

Credential backup and recovery protocol and service
Log | Files | Refs | Submodules | README | LICENSE

pin.c (2229B)


      1 /*
      2      This file is part of GNU Anastasis.
      3      Copyright (C) 2021 Anastasis SARL
      4 
      5      Anastasis is free software; you can redistribute it and/or modify
      6      it under the terms of the GNU General Public License as published
      7      by the Free Software Foundation; either version 3, or (at your
      8      option) any later version.
      9 
     10      Anastasis is distributed in the hope that it will be useful, but
     11      WITHOUT ANY WARRANTY; without even the implied warranty of
     12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13      General Public License for more details.
     14 
     15      You should have received a copy of the GNU General Public License
     16      along with Anastasis; see the file COPYING.  If not, write to the
     17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18      Boston, MA 02110-1301, USA.
     19 */
     20 
     21 /**
     22  * @file anastasis/src/util/pin.c
     23  * @brief pin conversion functions
     24  * @author Christian Grothoff
     25  */
     26 #include "platform.h"
     27 #include "anastasis_util_lib.h"
     28 
     29 
     30 bool
     31 ANASTASIS_scan_pin (const char *as,
     32                     unsigned long long *pin)
     33 {
     34   char dummy;
     35   char s[16];
     36 
     37   if (NULL == as)
     38     return false;
     39   if (0 == strncasecmp ("A-", as, 2))
     40     as += 2; /* skip "A-" prefix if present */
     41   if (strlen (as) != 18)
     42     return false;
     43   if ( ('-' != as[5]) ||
     44        ('-' != as[9]) ||
     45        ('-' != as[14]) )
     46     return false;
     47   GNUNET_snprintf (s,
     48                    sizeof (s),
     49                    "%.5s%.3s%.4s%.3s",
     50                    as,
     51                    &as[6],
     52                    &as[10],
     53                    &as[15]);
     54   if (1 != sscanf (s,
     55                    "%llu%c",
     56                    pin,
     57                    &dummy))
     58   {
     59     GNUNET_break (0);
     60     return false;
     61   }
     62   return true;
     63 }
     64 
     65 
     66 const char *
     67 ANASTASIS_pin2s (uint64_t pin)
     68 {
     69   static char buf[22];
     70   char tmp[16];
     71 
     72   GNUNET_assert (pin < ANASTASIS_PIN_MAX_VALUE);
     73   GNUNET_snprintf (tmp,
     74                    sizeof (tmp),
     75                    "%015llu",
     76                    (unsigned long long) pin);
     77   GNUNET_snprintf (buf,
     78                    sizeof (buf),
     79                    "A-%.5s-%.3s-%.4s-%.3s",
     80                    tmp,
     81                    &tmp[5],
     82                    &tmp[8],
     83                    &tmp[12]);
     84   return buf;
     85 }