anastasis

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

validation_FR_INSEE.c (1847B)


      1 /*
      2   This file is part of Anastasis
      3   Copyright (C) 2022 Anastasis SARL
      4 
      5   Anastasis is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 
     13   You should have received a copy of the GNU General Public License along with
     14   Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file reducer/validation_FR_INSEE.c
     18  * @brief Validation for French INSEE Numbers
     19  * @author Christian Grothoff
     20  */
     21 #include <string.h>
     22 #include <stdbool.h>
     23 #include <stdio.h>
     24 
     25 
     26 /**
     27  * Function to validate a French INSEE number.
     28  *
     29  * See https://en.wikipedia.org/wiki/INSEE_code
     30  *
     31  * Note that we do not implement checks on the month
     32  * and also allow non-binary prefixes.
     33  *
     34  * @param insee_number social security number to validate (input)
     35  * @return true if validation passed, else false
     36  */
     37 bool
     38 FR_INSEE_check (const char *insee_number);
     39 
     40 /* declaration to fix compiler warning */
     41 bool
     42 FR_INSEE_check (const char *insee_number)
     43 {
     44   char pfx[14];
     45   unsigned long long num;
     46   unsigned int cc;
     47   char dum;
     48 
     49   if (strlen (insee_number) != 15)
     50     return false;
     51   memcpy (pfx,
     52           insee_number,
     53           13);
     54   pfx[13] = '\0';
     55   if (1 !=
     56       sscanf (pfx,
     57               "%llu%c",
     58               &num,
     59               &dum))
     60     return false;
     61   if (1 !=
     62       sscanf (&insee_number[13],
     63               "%u%c",
     64               &cc,
     65               &dum))
     66     return false;
     67   if (97 - cc != num % 97)
     68     return false;
     69   return true;
     70 }