Skip to content

Commit

Permalink
fix: Ensure checksum numbers are zero-padded for Belgian national num…
Browse files Browse the repository at this point in the history
…bers (#560)

* fix: Ensure checksum numbers are zero-padded for Belgian national numbers

* fix: Keep code compatible with .NET6
  • Loading branch information
vyruz1986 authored Sep 2, 2024
1 parent d1d3386 commit e41bd68
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
16 changes: 15 additions & 1 deletion Source/Bogus.Tests/ExtensionTests/BelgianExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bogus.DataSets;
using System;
using Bogus.DataSets;
using Bogus.Extensions.Belgium;
using FluentAssertions;
using Xunit;
Expand Down Expand Up @@ -43,6 +44,19 @@ public void includes_formatting_national_number()
result.Should().ContainAll("-", ".");
}

[Theory]
[InlineData("850103725", "07")]
public void checksum_is_zero_padded(string givenNationalNumber, string expectedChecksum)
{
var year = int.Parse(givenNationalNumber.Substring(0, 2));
var month = int.Parse(givenNationalNumber.Substring(2, 2));
var day = int.Parse(givenNationalNumber.Substring(4, 2));
var dateOfBirth = new DateTime(year, month, day);

var checkNumber = ExtensionsForBelgium.CalculateCheckNumber(givenNationalNumber, dateOfBirth);

checkNumber.Should().Be(expectedChecksum);
}

private void ShouldHaveCorrectChecksum(string candidate)
{
Expand Down
21 changes: 15 additions & 6 deletions Source/Bogus/Extensions/Belgium/ExtensionsForBelgium.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Bogus.Extensions.Belgium;
using System;

namespace Bogus.Extensions.Belgium;

/// <summary>
/// API extensions specific for a geographical location.
Expand Down Expand Up @@ -31,12 +33,8 @@ public static string NationalNumber(this Person p, bool includeFormatSymbols = t
: p.Random.Even(2, 998);

var baseNumber = $"{p.DateOfBirth:yyMMdd}{sequence:000}";
var baseNumberLong = ulong.Parse(baseNumber);

var bornAfter2000 = p.DateOfBirth.Year >= 2000;
var checkNumber = bornAfter2000
? 97 - (int)((baseNumberLong + 2000000000L) % 97)
: 97 - (int)(baseNumberLong % 97);
var checkNumber = CalculateCheckNumber(baseNumber, p.DateOfBirth);

var nationalNumber = $"{baseNumber}{checkNumber}";

Expand All @@ -45,6 +43,17 @@ public static string NationalNumber(this Person p, bool includeFormatSymbols = t
: nationalNumber;
}

internal static string CalculateCheckNumber(string baseNumber, DateTime dateOfBirth)
{
var baseNumberLong = ulong.Parse(baseNumber);
var bornAfter2000 = dateOfBirth.Year >= 2000;
var checkNumber = bornAfter2000
? 97 - (int)((baseNumberLong + 2000000000L) % 97)
: 97 - (int)(baseNumberLong % 97);

return checkNumber.ToString("D2");
}

private static string FormatNationalNumber(string nationalNumber)
{
string year = nationalNumber.Substring(0, 2);
Expand Down

0 comments on commit e41bd68

Please sign in to comment.