codebude / QRCoder

A pure C# Open Source QR Code implementation
MIT License
4.46k stars 1.09k forks source link

The IBAN entered isn't valid #279

Closed scfontana closed 3 years ago

scfontana commented 3 years ago

Type of issue

[x] Bug
[ ] Question (e.g. about handling/usage)
[ ] Request for new feature/improvement

Expected Behavior

Valid IBANs such as CH47048350000GABRIELS can generate a QRCode

Current Behavior

Currently calling new PayloadGenerator.SwissQrCode.Iban(iban, PayloadGenerator.SwissQrCode.Iban.IbanType.Iban) throws an error (The IBAN entered isn't valid.)

Possible Solution (optional)

private static bool PieceWiseCalculationForValidation(string ibanString)
{
    string currentlyProcessingIbanString = ibanString.Substring(0, 9);
    string ibanStringStrippedOfPrevious = ibanString.Substring(9);
    int? n = null;

    while (!string.IsNullOrEmpty(currentlyProcessingIbanString) && int.TryParse(n != null ? n + currentlyProcessingIbanString : currentlyProcessingIbanString, NumberStyles.Any, CultureInfo.InvariantCulture, out int currentlyProcessingNumberFromIbanString))
    {
        n = currentlyProcessingNumberFromIbanString % 97;

        int remainingSubstringLength = ibanStringStrippedOfPrevious.Length < 7 ? ibanStringStrippedOfPrevious.Length : 7;
        currentlyProcessingIbanString = ibanStringStrippedOfPrevious.Substring(0, remainingSubstringLength);
        ibanStringStrippedOfPrevious = ibanStringStrippedOfPrevious.Substring(remainingSubstringLength);
    }

     return n == 1;
}

Steps to Reproduce (for bugs)

new PayloadGenerator.SwissQrCode.Iban("CH47048350000GABRIELS ", PayloadGenerator.SwissQrCode.Iban.IbanType.Iban)

Your Environment

codebude commented 3 years ago

Hi @scfontana ,

thanks for pointing out this bug. I fixed the problem in this commit https://github.com/codebude/QRCoder/commit/613ef0392ccdf8d350f0ea8f8ac3ceb9c059a918 and added also an additional test case.