public CoordinationNumberFormatter(string value) : base(value) { if (SerialNumber.Length != 4) { throw new PersonIdentifierFormatException(value, ExceptionMessage.IncorrectLength); } if (Value.Length != 10 && Value.Length != 12) { throw new PersonIdentifierFormatException(value, ExceptionMessage.IncorrectLength); } TwoDigitSerialNumber = Convert.ToInt32(SerialNumber.Substring(0, 2)); GenderIndicator = Convert.ToInt32(SerialNumber.Substring(2, 1)); LuhnControlNumber = Convert.ToInt32(SerialNumber.Substring(3, 1)); var luhnValidationDay = Day; Day -= AddToDays; if (Day < 1) { throw new PersonIdentifierFormatException(value, ExceptionMessage.CoordinationNumberDayError); } if (!LuhnAlgorithm.Validate($"{TwoDigitYear:00}{Month:00}{luhnValidationDay:00}{SerialNumber}")) { throw new PersonIdentifierFormatException(value, ExceptionMessage.LuhnError); } }
public static PersonalNumberIdentifier BuildPersonalNumber(this PersonIdentifierBuilder builder) { var random = new Random(Guid.NewGuid().GetHashCode()); var date = new PersonIdentifierBuilder.PersonIdentifierDateBuilder(random).Build(builder); var gender = new PersonIdentifierBuilder.PersonIdentifierGenderBuilder(random).BuildAsInt(builder); var serial = random.Next(0, 100).ToString("00"); var luhnChecksum = LuhnAlgorithm.Generate($"{date:yyMMdd}{serial}{gender}"); return(new PersonalNumberIdentifier($"{date:yyyyMMdd}{serial}{gender}{luhnChecksum}")); }
public static CoordinationNumberIdentifier BuildCoordinationNumber(this PersonIdentifierBuilder builder) { var random = new Random(Guid.NewGuid().GetHashCode()); var date = new PersonIdentifierBuilder.PersonIdentifierDateBuilder(random).Build(builder); var gender = new PersonIdentifierBuilder.PersonIdentifierGenderBuilder(random).BuildAsInt(builder); var dayWithAddedDays = date.Day + CoordinationNumberFormatter.AddToDays; var serial = random.Next(0, 100).ToString("00"); var luhnChecksum = LuhnAlgorithm.Generate($"{date:yyMM}{dayWithAddedDays:00}{serial}{gender}"); return(new CoordinationNumberIdentifier($"{date:yyyyMM}{dayWithAddedDays:00}{serial}{gender}{luhnChecksum}")); }
public static NationalReserveNumberIdentifier BuildNationalReserveNumber(this PersonIdentifierBuilder builder) { var random = new Random(Guid.NewGuid().GetHashCode()); int year, month, day; var dateBuilder = new PersonIdentifierBuilder.PersonIdentifierDateBuilder(random); if (!dateBuilder.HaveDate(builder)) { //Skapa reservnummer där fördelsedatum är okänt year = random.Next(1, 100); month = random.Next(20, 100); day = random.Next(40, 60); } else { var date = dateBuilder.Build(builder); year = date.Year + (random.Next(0, 2) == 0 ? 300 : 600); //Orkar inte hantera fler alternativ!! month = date.Month; day = date.Day; } var genderBuilder = new PersonIdentifierBuilder.PersonIdentifierGenderBuilder(random); string serial; do { var firstRandom = Letters[random.Next(0, Letters.Length)]; var secondRandom = Letters[random.Next(0, Letters.Length)]; var gender = genderBuilder.HaveGender(builder) ? genderBuilder.BuildAsInt(builder).ToString() : Letters[random.Next(0, 21)]; serial = $"{firstRandom}{secondRandom}{gender}"; } while (UnwantedStrings.Contains(serial)); var luhnPart = $"{year:0000}{month:00}{day:00}{serial}"; var check = LuhnAlgorithm.Generate(luhnPart); var century = year > 1000 ? Convert.ToInt32(year.ToString().Substring(0, 2)) : 0; var twoDigitYear = year > 1000 ? Convert.ToInt32(year.ToString().Substring(2, 2)) : year; return(new NationalReserveNumberIdentifier(new NationalReserveNumberFormatter($"{century:00}{twoDigitYear:00}{month:00}{day:00}{serial}{check}"))); }