gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-anastasis] branch master updated: implement DE SVN and prime vali


From: gnunet
Subject: [taler-anastasis] branch master updated: implement DE SVN and prime validation
Date: Sat, 27 Mar 2021 02:18:03 +0100

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository anastasis.

The following commit(s) were added to refs/heads/master by this push:
     new 6303cef  implement DE SVN and prime validation
6303cef is described below

commit 6303cef6c73489020250f5230e84537d9b41223a
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Sat Mar 27 02:18:00 2021 +0100

    implement DE SVN and prime validation
---
 src/reducer/Makefile.am                            |  3 +-
 src/reducer/validation_DE_SVN.c                    | 41 +++++++++++++++++++++-
 .../{validation_DE_SVN.c => validation_XY_PRIME.c} | 35 ++++++++++++------
 3 files changed, 66 insertions(+), 13 deletions(-)

diff --git a/src/reducer/Makefile.am b/src/reducer/Makefile.am
index eae9a63..ef0e113 100644
--- a/src/reducer/Makefile.am
+++ b/src/reducer/Makefile.am
@@ -20,7 +20,8 @@ libanastasisredux_la_SOURCES = \
   anastasis_api_backup_redux.c \
   validation_CH_AHV.c \
   validation_DE_SVN.c \
-  validation_XX_SQUARE.c 
+  validation_XX_SQUARE.c \
+  validation_XY_PRIME.c
 libanastasisredux_la_LIBADD = \
   $(top_builddir)/src/restclient/libanastasisrest.la \
   $(top_builddir)/src/lib/libanastasis.la \
diff --git a/src/reducer/validation_DE_SVN.c b/src/reducer/validation_DE_SVN.c
index 06c2a54..9f54ea3 100644
--- a/src/reducer/validation_DE_SVN.c
+++ b/src/reducer/validation_DE_SVN.c
@@ -27,6 +27,7 @@
  * Function to validate a German Social Security number.
  *
  * See https://www.financescout24.de/wissen/ratgeber/sozialversicherungsnummer
+ * and https://de.wikipedia.org/wiki/Versicherungsnummer
  * for the structure!
  *
  * @param avh_number ahv number to validate (input)
@@ -35,6 +36,44 @@
 bool
 DE_SSN_check (const char *ssn_number)
 {
-  // FIXME: not implemented!
+  static const unsigned int factors[] = {
+    2, 1, 2, 5, 7, 1, 2, 1, 2, 1, 2, 1
+  };
+  unsigned int sum = 0;
+
+  if (strlen (ssn_number) != 12)
+    return false;
+  for (unsigned int i = 0; i<8; i++)
+  {
+    unsigned char c = (unsigned char) ssn_number[i];
+
+    if ( ('0' > c) || ('9' < c) )
+      return false;
+    sum += (c - '0') * factors[i];
+  }
+  {
+    unsigned char c = (unsigned char) ssn_number[8];
+
+    if ( ('A' > c) || ('Z' < c) )
+      return false;
+    sum += (c - 'A' + 1) * factors[8];
+  }
+  for (unsigned int i = 9; i<11; i++)
+  {
+    unsigned char c = ssn_number[i];
+
+    if ( ('0' > c) || ('9' < c) )
+      return false;
+    sum += (c - '0') * factors[i];
+  }
+  if (ssn_number[11] != '0' + (sum % 10))
+    return false;
+  {
+    unsigned int month = (ssn_number[4] - '0') * 10 + (ssn_number[5] - '0');
+
+    if ( (0 == month) ||
+         (12 < month) )
+      return false;
+  }
   return true;
 }
diff --git a/src/reducer/validation_DE_SVN.c b/src/reducer/validation_XY_PRIME.c
similarity index 55%
copy from src/reducer/validation_DE_SVN.c
copy to src/reducer/validation_XY_PRIME.c
index 06c2a54..ab24014 100644
--- a/src/reducer/validation_DE_SVN.c
+++ b/src/reducer/validation_XY_PRIME.c
@@ -14,27 +14,40 @@
   Anastasis; see the file COPYING.GPL.  If not, see 
<http://www.gnu.org/licenses/>
 */
 /**
- * @file redux/validation_DE_SVN.c
- * @brief
+ * @file redux/validation_XY_PRIME.c
+ * @brief anastasis reducer api
  * @author Christian Grothoff
  * @author Dominik Meister
  * @author Dennis Neufeld
  */
 #include <string.h>
 #include <stdbool.h>
+#include <stdio.h>
+#include <gcrypt.h>
 
 /**
- * Function to validate a German Social Security number.
+ * Function to validate a prime number.
  *
- * See https://www.financescout24.de/wissen/ratgeber/sozialversicherungsnummer
- * for the structure!
- *
- * @param avh_number ahv number to validate (input)
- * @return true if validation passed, else false
+ * @param pr_number prime number to validate (input)
+ * @return true if pr_number is a prime, else false
  */
 bool
-DE_SSN_check (const char *ssn_number)
+XY_PRIME_check (const char *pr_number)
 {
-  // FIXME: not implemented!
-  return true;
+  unsigned long long n;
+  char dummy;
+  gcry_mpi_t p;
+  bool is_prime;
+
+  if (1 != sscanf (pr_number,
+                   "%llu%c",
+                   &n,
+                   &dummy))
+    return false;
+  p = gcry_mpi_set_ui (NULL,
+                       (unsigned long) n);
+  is_prime = (0 == gcry_prime_check (p,
+                                     0));
+  gcry_mpi_release (p);
+  return is_prime;
 }

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]