From 03cc959fbdc7d7b46f3714f335f36307076aa121 Mon Sep 17 00:00:00 2001 From: Frede Hundewadt Date: Tue, 30 May 2023 10:04:53 +0200 Subject: [PATCH] added EAN13 validation --- Ean13Validator.cs | 85 ++++++++++++++++++++++++++++++++++++++ FCS.Lib.Utility.csproj | 1 + Properties/AssemblyInfo.cs | 4 +- Squid.cs | 9 +--- 4 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 Ean13Validator.cs diff --git a/Ean13Validator.cs b/Ean13Validator.cs new file mode 100644 index 0000000..46908ec --- /dev/null +++ b/Ean13Validator.cs @@ -0,0 +1,85 @@ +// *********************************************************************** +// Assembly : FCS.Lib.Utility +// Author : inno +// Created : 2023 05 24 12:23 +// +// Last Modified By : inno +// Last Modified On : 2023 05 24 12:23 +// *********************************************************************** +// +// Copyright (C) 2023-2023 FCS Frede's Computer Services. +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see [https://www.gnu.org/licenses] +// +// +// *********************************************************************** + +using System; + +namespace FCS.Lib.Utility; + +public class Ean13Validator +{ + private const int Size = 12; + + public static bool Validate(string number) + { + if (number.Length != Size + 1) + return false; + return number == ParsedNumber(number.Substring(0, 12)); + } + + private static string Ean13(int[]? firstDigits) + { + var summedProduct = 0; + var randomDigits = new Random(); + bool isNull; + if (firstDigits == null) + { + firstDigits = new int[Size]; + isNull = true; + } + else + isNull = false; + for (var idx = 0; idx < Size; idx++) + { + var alt = idx % 2 == 0 ? 1 : 3; + int digit; + if (isNull) + { + digit = randomDigits.Next(10); + firstDigits[idx] = digit; + } + else + digit = firstDigits[idx]; + summedProduct += digit * alt; + } + var checkDigit = 10 - summedProduct % 10; + if (checkDigit == 10) + checkDigit = 0; + return string.Join("", firstDigits) + checkDigit; + } + + private static string ParsedNumber(string number) + { + var firstDigits = new int[Size]; + if (number.Length != Size | !long.TryParse(number, out _)) + return null; + for (var idx = 0; idx < Size; idx++) + { + var digit = int.Parse(number[idx].ToString()); + firstDigits[idx] = digit; + } + return Ean13(firstDigits); + } +} \ No newline at end of file diff --git a/FCS.Lib.Utility.csproj b/FCS.Lib.Utility.csproj index b5c2bb4..81c21ad 100644 --- a/FCS.Lib.Utility.csproj +++ b/FCS.Lib.Utility.csproj @@ -52,6 +52,7 @@ + diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 7323968..2fb5514 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -17,5 +17,5 @@ using System.Runtime.InteropServices; [assembly: ComVisible(false)] [assembly: Guid("8D850197-78DB-4D16-A91F-E5BB6E8880A7")] -[assembly: AssemblyVersion("1.0.23077.1334")] -[assembly: AssemblyFileVersion("1.0.23077.1334")] \ No newline at end of file +[assembly: AssemblyVersion("1.0.23146.0802")] +[assembly: AssemblyFileVersion("1.0.23146.0802")] \ No newline at end of file diff --git a/Squid.cs b/Squid.cs index 1a46c3e..df9a8d6 100644 --- a/Squid.cs +++ b/Squid.cs @@ -40,14 +40,13 @@ public readonly struct Squid : IEquatable /// /// A read-only object of the Squid struct. /// Value is guaranteed to be all zeroes. - /// Equivalent to . /// public static readonly Squid Empty = new(Guid.Empty); /// /// Creates a new Squid from a Squid encoded string. /// - /// A valid Squid encodd string. + /// A valid Squid encoded string. public Squid(string value) { Value = value; @@ -68,9 +67,7 @@ public readonly struct Squid : IEquatable /// Gets the underlying for the encoded Squid. /// /// The unique identifier. -#pragma warning disable CA1720 // Identifier contains type name public Guid Guid { get; } -#pragma warning restore CA1720 // Identifier contains type name /// /// The encoded string value of the @@ -92,7 +89,7 @@ public readonly struct Squid : IEquatable /// Returns a value indicating whether this object and a specified object represent the same type and value. /// Compares for equality against other string, Guid and Squid types. /// - /// A Systerm.String, System.Guid or Squid object + /// A System.String, System.Guid or Squid object /// true if the specified is equal to this instance; otherwise, false. public override bool Equals(object obj) { @@ -213,9 +210,7 @@ public readonly struct Squid : IEquatable obj = DecodeSquid(value); return true; } -#pragma warning disable CA1031 // Do not catch general exception types catch (Exception) -#pragma warning restore CA1031 // Do not catch general exception types { // Return empty Guid obj = Guid.Empty;