added EAN13 validation
This commit is contained in:
parent
818497003b
commit
03cc959fbd
4 changed files with 90 additions and 9 deletions
85
Ean13Validator.cs
Normal file
85
Ean13Validator.cs
Normal file
|
@ -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 file="Ean13Validator.cs" company="FCS">
|
||||
// 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]
|
||||
// </copyright>
|
||||
// <summary></summary>
|
||||
// ***********************************************************************
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -52,6 +52,7 @@
|
|||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Ean13Validator.cs" />
|
||||
<Compile Include="ExtensionsEx.cs" />
|
||||
<Compile Include="Generators.cs" />
|
||||
<Compile Include="GuidGenerator.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")]
|
||||
[assembly: AssemblyVersion("1.0.23146.0802")]
|
||||
[assembly: AssemblyFileVersion("1.0.23146.0802")]
|
9
Squid.cs
9
Squid.cs
|
@ -40,14 +40,13 @@ public readonly struct Squid : IEquatable<Squid>
|
|||
/// <summary>
|
||||
/// A read-only object of the Squid struct.
|
||||
/// Value is guaranteed to be all zeroes.
|
||||
/// Equivalent to <see cref="Guid.Empty" />.
|
||||
/// </summary>
|
||||
public static readonly Squid Empty = new(Guid.Empty);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new Squid from a Squid encoded string.
|
||||
/// </summary>
|
||||
/// <param name="value">A valid Squid encodd string.</param>
|
||||
/// <param name="value">A valid Squid encoded string.</param>
|
||||
public Squid(string value)
|
||||
{
|
||||
Value = value;
|
||||
|
@ -68,9 +67,7 @@ public readonly struct Squid : IEquatable<Squid>
|
|||
/// Gets the underlying <see cref="System.Guid" /> for the encoded Squid.
|
||||
/// </summary>
|
||||
/// <value>The unique identifier.</value>
|
||||
#pragma warning disable CA1720 // Identifier contains type name
|
||||
public Guid Guid { get; }
|
||||
#pragma warning restore CA1720 // Identifier contains type name
|
||||
|
||||
/// <summary>
|
||||
/// The encoded string value of the <see cref="Guid" />
|
||||
|
@ -92,7 +89,7 @@ public readonly struct Squid : IEquatable<Squid>
|
|||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="obj">A Systerm.String, System.Guid or Squid object</param>
|
||||
/// <param name="obj">A System.String, System.Guid or Squid object</param>
|
||||
/// <returns><c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
|
@ -213,9 +210,7 @@ public readonly struct Squid : IEquatable<Squid>
|
|||
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;
|
||||
|
|
Loading…
Reference in a new issue