frosix

Multiparty signature service (experimental)
Log | Files | Refs | README | LICENSE

pin.c (2174B)


      1 /*
      2      This file is part of Frosix
      3      Copyright (C) 2021 Anastasis SARL
      4 
      5      Frosix 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      Frosix 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 Frosix; 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 util/pin.c
     23  * @brief pin conversion functions
     24  * @author Christian Grothoff
     25  */
     26 #include "platform.h"
     27 #include "frosix_util_lib.h"
     28 
     29 
     30 bool
     31 FROSIX_scan_pin (const char *as,
     32                  unsigned long long *pin)
     33 {
     34   char dummy;
     35   char s[16];
     36 
     37   if ( (NULL != as) &&
     38        (0 == strncasecmp ("A-", as, 2)) )
     39     as += 2; /* skip "A-" prefix if present */
     40   if (strlen (as) != 18)
     41     return false;
     42   if ( ('-' != as[5]) ||
     43        ('-' != as[9]) ||
     44        ('-' != as[14]) )
     45     return false;
     46   GNUNET_snprintf (s,
     47                    sizeof (s),
     48                    "%.5s%.3s%.4s%.3s",
     49                    as,
     50                    &as[6],
     51                    &as[10],
     52                    &as[15]);
     53   if (1 != sscanf (s,
     54                    "%llu%c",
     55                    pin,
     56                    &dummy))
     57   {
     58     GNUNET_break (0);
     59     return false;
     60   }
     61   return true;
     62 }
     63 
     64 
     65 const char *
     66 FROSIX_pin2s (uint64_t pin)
     67 {
     68   static char buf[22];
     69   char tmp[16];
     70 
     71   GNUNET_assert (pin < FROSIX_PIN_MAX_VALUE);
     72   GNUNET_snprintf (tmp,
     73                    sizeof (tmp),
     74                    "%015llu",
     75                    (unsigned long long) pin);
     76   GNUNET_snprintf (buf,
     77                    sizeof (buf),
     78                    "A-%.5s-%.3s-%.4s-%.3s",
     79                    tmp,
     80                    &tmp[5],
     81                    &tmp[8],
     82                    &tmp[12]);
     83   return buf;
     84 }