public string Generate() { var nipNumberBuilder = new StringBuilder(); string taxOfficePrefix = TaxOffice.Codes[_random.Next(TaxOffice.Codes.Length)]; nipNumberBuilder.Append(taxOfficePrefix); nipNumberBuilder.Append(GenerateRandomNumbers(6)); int checksum = NipCheckSumCalculator.Calculate(nipNumberBuilder.ToString()); while (checksum == 10) { // change last number, check sum must be different from 10 nipNumberBuilder.Remove(nipNumberBuilder.Length - 1, 1); nipNumberBuilder.Append(_random.Next(10).ToString()); checksum = NipCheckSumCalculator.Calculate(nipNumberBuilder.ToString()); } nipNumberBuilder.Append(checksum); return(nipNumberBuilder.ToString()); }
public static bool IsValid(string nip) { var regex = new Regex("^\\d{10}$"); if (!regex.IsMatch(nip)) { return(false); } int checkSum = NipCheckSumCalculator.Calculate(nip); if (nip.Last() - '0' != checkSum) { return(false); } return(TaxOffice.Codes.Contains(nip.Substring(0, 3))); }