4

I am trying to post the username and password to the server. I want to convert this username and password into Base64 Encoding. So this encoded string will be added for the Authorization field.

Is there any API already available to do the Base64 encoding in iOS or we have write on our own ??

Cyril
  • 1,216
  • 3
  • 19
  • 40
  • possible duplicate of [Any base64 library on iphone-sdk?](http://stackoverflow.com/questions/392464/any-base64-library-on-iphone-sdk) – rmaddy Nov 06 '12 at 06:06
  • I mean API provided by cocoa-touch. So I guess we have write on our own – Cyril Nov 06 '12 at 06:07

2 Answers2

5

This can be done by implementing a class method for base64 conversion the below code is for conversion..

+ (NSString*)base64forData:(NSData*)theData 
{
    const uint8_t* input = (const uint8_t*)[theData bytes];
    NSInteger length = [theData length];

    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;

    NSInteger i;
    for (i=0; i < length; i += 3) {
        NSInteger value = 0;
        NSInteger j;
        for (j = i; j < (i + 3); j++) {
            value <<= 8;

            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }

        NSInteger theIndex = (i / 3) * 4;
        output[theIndex + 0] =                    table[(value >> 18) & 0x3F];
        output[theIndex + 1] =                    table[(value >> 12) & 0x3F];
        output[theIndex + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[theIndex + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }

    return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
}

and in your URL request add the following code for to sent the username and password for authorization...

[request addValue:[NSString stringWithFormat:@"Basic %@",[className base64forData:[[NSString stringWithFormat:@"%@:%@",UsernameString,passwordString] dataUsingEncoding: NSUTF8StringEncoding]]] forHTTPHeaderField:@"Authorization"];

Rahul
  • 187
  • 1
  • 14
2

How do I do base64 encoding on iphone-sdk?

Have a look at this.

and then use these methods to make use of the above methods(given in link) Converting NSString to Base64 Data for XML Serialization taken from above link.

+ (NSString *)toBase64String:(NSString *)string {
NSData *data = [string dataUsingEncoding: NSUnicodeStringEncoding];

NSString *ret = [NSStringUtil base64StringFromData:data length:[data length]];

return ret;
}

+ (NSString *)fromBase64String:(NSString *)string {
NSData  *base64Data = [NSStringUtil base64DataFromString:string];

NSString* decryptedStr = [[NSString alloc] initWithData:base64Data encoding:NSUnicodeStringEncoding];

return [decryptedStr autorelease];
}
Community
  • 1
  • 1
Amit Hooda
  • 2,133
  • 3
  • 23
  • 37
  • 7
    copy answer from http://stackoverflow.com/questions/4838263/converting-nsstring-to-base64-data-for-xml-serialization please mantion the copy URL with your answer if answers is not your own logic – Nitin Gohel Nov 06 '12 at 06:11