【问题标题】:IBAN Validator SwiftIBAN 验证器 Swift
【发布时间】:2017-10-03 21:03:03
【问题描述】:

我正在编写一个算法来验证 Swift 3 中的 IBAN(国际银行帐号),但无法计算出其中一个验证。

示例 IBAN - BE68539007547034

这里是验证规则-

  1. 输入数字的长度应为 16。
  2. 前 2 个字符是国家代码(不是数字)。
  3. 最后 14 位是数字。
  4. 最后 2 个字符是前 12 个数字字符的模 97 结果。

虽然 #1 - #3 很明确,但我需要明确 #4。如果有人以前做过并且知道这件事,请告诉我。

【问题讨论】:

  • 您需要取孔 IBAN,而不仅仅是 14 个数字,因为荷兰的 IBAN 数字看起来像:NL70INGB0123456564。看看这个IBAN wiki
  • 或者查看这个库:github.com/readefries/IBAN-Helper
  • @StephenJ 真正的 IBAN 验证是一件非常复杂的事情。有一个国家前缀列表,每个前缀也有一个固定的 IBAN 长度。此外,每个国家/地区都可以有额外的验证规则。但是,前端通常应该只检查 明显 的拼写错误,因此计算 mod97 通常就足够了。实际使用 IBAN 的后端实际上应该验证 IBAN 是否存在,这是一个完全不同的问题,访问具有该信息的银行 API。

标签: ios swift iban


【解决方案1】:

如果你按照wikipedia上的算法,验证算法是相当简单的:

extension String {
    private func mod97() -> Int {
        let symbols: [Character] = Array(self)
        let swapped = symbols.dropFirst(4) + symbols.prefix(4)

        let mod: Int = swapped.reduce(0) { (previousMod, char) in
            let value = Int(String(char), radix: 36)! // "0" => 0, "A" => 10, "Z" => 35
            let factor = value < 10 ? 10 : 100          
            return (factor * previousMod + value) % 97
        }

        return mod
    }    

    func passesMod97Check() -> Bool {
        guard self.characters.count >= 4 else {
            return false
        }

        let uppercase = self.uppercased()

        guard uppercase.range(of: "^[0-9A-Z]*$", options: .regularExpression) != nil else {
            return false
        }

        return (uppercase.mod97() == 1)
    }
}

用法:

let iban = "XX0000000..."
let valid = iban.passesMod97Check()

如果要验证特定国家/地区的格式,只需修改正则表达式,例如

"^[A-Z]{2}[0-9]{14}$"

或直接

"^BE\\d{14}$"

【讨论】:

  • 非常感谢。这看起来很有希望。需要一些时间来消化mod97 () :)。
  • 这实际上来自生产应用程序。
  • @Retterdesdialogs 您的解决方案的想法是正确的,您可以将数据类型更改为 64 位整数并且它可以工作,至少对于比利时 IBAN。无论如何,在通用解决方案中,您必须处理 A-Z 字符。
  • @Sulthan 好的,谢谢,但你的回答好多了,所以我刚刚删除了我的回答。
【解决方案2】:

来自Wikipedia

let IBAN = "GB82WEST12345698765432" // uppercase, no whitespace !!!!
var a = IBAN.utf8.map{ $0 }
while a.count < 4 {
    a.append(0)
}
let b = a[4..<a.count] + a[0..<4]
let c = b.reduce(0) { (r, u) -> Int in
    let i = Int(u)
    return i > 64 ? (100 * r + i - 55) % 97: (10 * r + i - 48) % 97
}
print( "IBAN \(IBAN) is", c == 1 ? "valid": "invalid")

打印

IBAN GB82WEST12345698765432 is valid

使用您的问题中的 IBAN 打印

IBAN BE68539007547034 is valid

【讨论】:

  • 这么多神奇的数字...你为什么不用"A"而不是64"0"而不是48等等?您的代码将更具可读性。
  • @Sulthan 只是为了避免从“A”等创建 Int,这是计算所必需的:-)。我喜欢你的回答中的 Int(String(char), radix: 36) !!!
【解决方案3】:

给你:

func isValidIBAN(text:String) -> Bool {
        let ibanRegEx = "[a-zA-Z]{2}+[0-9]{2}+[a-zA-Z0-9]{4}+[0-9]{7}([a-zA-Z0-9]?){0,16}"
        let ibanTest = NSPredicate(format:"SELF MATCHES %@", ibanRegEx)
        return ibanTest.evaluate(with: text)
    }

它很干净,而且很有效。

【讨论】:

    【解决方案4】:

    我在 Objective-C 中找到了适合我的绝佳解决方案 https://gist.github.com/0xc010d/5301790 您可以为 Swift 重写或使用桥接头。 mod97 IBAN检查算法的Objective-C实现

    #import <Foundation/Foundation.h>
    
    @interface NSString (Mod97Check)
    
    - (BOOL)passesMod97Check; // Returns result of mod 97 checking algorithm. Might be used to check IBAN.
                              // Expects string to contain digits and/or upper-/lowercase letters; space and all the rest symbols are not acceptable.
    
    @end
    
    #import "NSString+Mod97Check.h"
    
    @implementation NSString (Mod97Check)
    
    - (BOOL)passesMod97Check {
        NSString *string = [self uppercaseString];
        NSInteger mod = 0, length = [self length];
        for (NSInteger index = 4; index < length + 4; index ++) {
            unichar character = [string characterAtIndex:index % length];
            if (character >= '0' && character <= '9') {
                mod = (10 * mod + (character - '0')) % 97; // '0'=>0, '1'=>1, ..., '9'=>9
            }
            else if (character >= 'A' && character <= 'Z') {
                mod = (100 * mod + (character - 'A' + 10)) % 97; // 'A'=>10, 'B'=>11, ..., 'Z'=>35
            }
            else {
                return NO;
            }
        }
        return (mod == 1);
    }
    
    @end
    
    -(BOOL)isValidIBAN {
        NSString *iban = self;
        static NSString* const LettersAndDecimals = @"ABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789";
        iban = [[iban stringByReplacingOccurrencesOfString:@" " withString:@""] uppercaseString];
        NSCharacterSet *invalidChars = [[NSCharacterSet characterSetWithCharactersInString:LettersAndDecimals] invertedSet];
    
        if ([iban rangeOfCharacterFromSet:invalidChars].location != NSNotFound)
        {
            return NO;
        }
    
        int checkDigit = [iban substringWithRange:NSMakeRange(2, 2)].intValue;
        iban = [NSString stringWithFormat:@"%@%@",[iban substringWithRange:NSMakeRange(4, iban.length - 4)], [iban substringWithRange:NSMakeRange(0, 4)]] ;
    
        for (int i = 0; i < iban.length; i++) {
            unichar c = [iban characterAtIndex:i];
            if (c >= 'A' && c <= 'Z') {
                iban = [NSString stringWithFormat:@"%@%d%@", [iban substringWithRange:NSMakeRange(0, i)], (c - 'A' + 10),[iban substringWithRange:NSMakeRange(i+1, iban.length - i - 1)]];
            }
    
        }
        iban = [[iban substringWithRange:NSMakeRange(0, iban.length - 2)] stringByAppendingString:@"00"];
    
        while(true)
        {
            int iMin = (int)MIN(iban.length, 9);
            NSString* strPart = [iban substringWithRange:NSMakeRange(0, iMin)];
            int decnumber = strPart.intValue;
            if(decnumber < 97 || iban.length < 3)
                break;
            int del = decnumber % 97;
            iban =  [NSString stringWithFormat:@"%d%@", del, [iban substringFromIndex:iMin]];
        }
        int check = 98 - iban.intValue;
    
        return checkDigit == check;
    }
    

    【讨论】:

    • 您的passesMod97Check 函数写得非常好。它的空间和时间复杂度是有界的。
    猜你喜欢
    • 2019-04-06
    • 2014-01-25
    • 2015-11-19
    • 2014-03-22
    • 1970-01-01
    • 2016-11-08
    • 2012-01-12
    • 2020-03-25
    • 2011-12-10
    相关资源
    最近更新 更多