可可基地64执行情况REST验证(Cocoa Base64 Implementation for R

2019-07-17 11:09发布

我可以用一个干净的,工作的base64实施授权的目的通过HTTP的REST协议。

任何人都可以帮我或点我的方向?

谢谢。

Answer 1:

应该有完全没有必要为你做到这一点。 在一个较高水平,可可提供NSURLConnection的将做HTTP认证。 如果不是,CFHTTP API提供低级别的访问。



Answer 2:

有迹象表明,这种扩展漂浮的几个变种NSStringNSData通过使用Objective-C的类别。

下面是我已经加入到我的“实用工具”工具箱一个例子:

标题:

#import <Foundation/NSString.h>

@interface NSString (Utilities)

+ (NSString *) base64StringFromData:(NSData *)data;

@end    

执行:

#import "NSString+Utilities.h"

@implementation NSString (Utilities)

+ (NSString *) base64StringFromData:(NSData *)data {
  static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

  if ([data length] == 0)      
    return @"";

  char *characters = malloc((([data length] + 2) / 3) * 4);
  if (characters == NULL)
    return nil;
  NSUInteger length = 0;

  NSUInteger i = 0;
  while (i < [data length]) {
    char buffer[3] = {0,0,0};
    short bufferLength = 0;
    while (bufferLength < 3 && i < [data length])
      buffer[bufferLength++] = ((char *)[data bytes])[i++];

    //  Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
    characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
    characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];

    if (bufferLength > 1)
      characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
    else characters[length++] = '=';

    if (bufferLength > 2)
      characters[length++] = encodingTable[buffer[2] & 0x3F];
    else characters[length++] = '=';        
  }

  return [[[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSUTF8StringEncoding freeWhenDone:YES] autorelease];
}

@end

实例:

NSString *inputString = @"myInputString";
NSLog(@"%@", [NSString base64StringFromData:[inputString dataUsingEncoding:NSUTF8StringEncoding]]);


文章来源: Cocoa Base 64 Implementation for REST Auth