检测NSString是否为Base64

时间:2013-09-07 04:43:51

标签: ios objective-c ios6 nsstring base64

我最近尝试制作一个能够对Base64进行编码和解码的iOS应用程序,以及其他“语言”,如十六进制和二进制。我试图制作一个自动解码器(能够自动检测'语言')。但是,当我使用Base64时,自动解码器似乎无法检测到有效的Base64,因为Base64字符串中有换行符。我的Base64检测代码如下所示:

-(BOOL)isBase64Data:(NSString *)input
{
    if ([input length] % 4 == 0) {
        static NSCharacterSet *invertedBase64CharacterSet = nil;
        if (invertedBase64CharacterSet == nil) {
            invertedBase64CharacterSet = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="]invertedSet];
        }
        return [input rangeOfCharacterFromSet:invertedBase64CharacterSet options:NSLiteralSearch].location == NSNotFound;
    }
    return NO;
}

仅为了解更多信息,这是我在NSString中检测'language'类型的方法。

-(NSInteger)detectType:(BOOL)base64 hexadecimal:(BOOL)hex binary:(BOOL)binary
{
    //Make sure the checking of characters are in this order!
    if (binary) {
        return 0; //Type 0 means binary
    }
    else if (hex) {
        return 1; //Type 1 means hexadecimal
    }
    else if (base64) {
        return 2; //Type 2 means base64
    }
    else {
        return 3; //Type 3 is error/invalid text
    }
}

我只想在解码时调用此方法:

-(IBAction)decode:(id)sender
{
    //This is where I detect the type
    NSInteger type = [self detectType:[self isBase64Data:userInput.text] hexadecimal:[self isHexadecimal:userInput.text] binary:[self isBinary:userInput.text]];

    if ([userInput text].length<1) { //First, check if the text view is empty in the first place
        WCAlertView *alert=[[WCAlertView alloc]initWithTitle:@"Error: No input!" message:nil delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
        [alert show];
    }
    else if (type==0) {
        //Initiate the binary converter here
        TextBinViewController *binVC=[[TextBinViewController alloc]init];
        output.text=[binVC binToText:userInput.text];
    }
    else if (type==1) {
        //Initiate the hexadecimal converter here
        TextHexViewController *hexVC=[[TextHexViewController alloc]init];
        output.text=[hexVC hexToText:userInput.text];
    }
    else if (type==2) {
        //Initiate the base64 converter here
        TextBase64ViewController *baseVC=[[TextBase64ViewController alloc]init];
        output.text=[baseVC base64Decode:userInput.text];
    }
    else {
        //If the type matches none of the above, show an error (WCAlertView is a subclass of alertview that allows more styling)
        WCAlertView *alert=[[WCAlertView alloc]initWithTitle:@"Error: Invalid input!" message:nil delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
        [alert show];
        output.text=@"";
    }

    [userInput resignFirstResponder];
}

2 个答案:

答案 0 :(得分:4)

好吧,伙计们,我意识到我可以简单地修剪字符串中的所有换行符。更新后的-isBase64Data方法如下:

-(BOOL)isBase64Data:(NSString *)input
{

    input=[[input componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] componentsJoinedByString:@""];
    if ([input length] % 4 == 0) {
        static NSCharacterSet *invertedBase64CharacterSet = nil;
        if (invertedBase64CharacterSet == nil) {
            invertedBase64CharacterSet = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="]invertedSet];
        }
        return [input rangeOfCharacterFromSet:invertedBase64CharacterSet options:NSLiteralSearch].location == NSNotFound;
    }
    return NO;
}

现在检测到Base64字符串正常工作。我发现当字符串有换行符等时,[input length] % 4不会返回0.所以,我当前的解决方案只是使用whitespaceAndNewlineCharacterSet修剪这些字符。

答案 1 :(得分:0)

在你创建一个集合的有效字符串中添加一个新行,并将其反转。