Skip to content

Instantly share code, notes, and snippets.

@NSMutableString
Forked from 0xc010d/NSString+Mod97Check.h
Created August 10, 2013 11:49
Show Gist options
  • Save NSMutableString/6200151 to your computer and use it in GitHub Desktop.
Save NSMutableString/6200151 to your computer and use it in GitHub Desktop.
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment