Skip to content

Commit

Permalink
Added Base64UrlEncoded variance
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Poirier <[email protected]>
  • Loading branch information
Dave Poirier committed Oct 7, 2015
1 parent 546fb6c commit e9a0c7d
Show file tree
Hide file tree
Showing 7 changed files with 872 additions and 3,642 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Base64.xcodeproj/xcuserdata
.DS_Store
Base64.xcodeproj/project.xcworkspace/xcuserdata
7 changes: 7 additions & 0 deletions Base64.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Base64/MF_AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
#import <Cocoa/Cocoa.h>

@interface MF_AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSTextField *textField;

-(IBAction)encode:(id)sender;
-(IBAction)decode:(id)sender;
@end
29 changes: 24 additions & 5 deletions Base64/MF_AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

@implementation MF_AppDelegate

@synthesize window = _window;
@synthesize textField = _textField;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
for( int i = 0; i < 300; i++ ) {
Expand All @@ -40,14 +37,14 @@ -(NSData *)randomDataBlock
return [[NSData alloc] initWithBytes:bytes length:dataLength];
}

-(void)encode:(id)sender
-(IBAction)encode:(id)sender
{
NSString *raw = [_textField stringValue];
NSString *encoded = [raw base64String];
[_textField setStringValue:encoded];
}

-(void)decode:(id)sender
-(IBAction)decode:(id)sender
{
NSString *encoded = [_textField stringValue];
NSData *data = [NSData dataWithBase64String:encoded];
Expand All @@ -62,4 +59,26 @@ -(void)decode:(id)sender
}
}

-(IBAction)encodeBase64UrlEncoding:(id)sender
{
NSString *raw = [_textField stringValue];
NSString *encoded = [raw base64UrlEncodedString];
[_textField setStringValue:encoded];
}

-(IBAction)decodeBase64UrlEncoding:(id)sender
{
NSString *encoded = [_textField stringValue];
NSData *data = [NSData dataWithBase64UrlEncodedString:encoded];
NSString *raw = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if( raw )
{
[_textField setStringValue:raw];
}
else
{
[_textField setStringValue:[NSString stringWithFormat:@"WARNING: data could not be converted to a NSString, dumping decoded binary data: %@", data]];
}
}

@end
6 changes: 6 additions & 0 deletions Base64/MF_Base64Additions.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@

@interface NSString (Base64Addition)
+(NSString *)stringFromBase64String:(NSString *)base64String;
+(NSString *)stringFromBase64UrlEncodedString:(NSString *)base64UrlEncodedString;
-(NSString *)base64String;
-(NSString *)base64UrlEncodedString;
@end

@interface NSData (Base64Addition)
+(NSData *)dataWithBase64String:(NSString *)base64String;
+(NSData *)dataWithBase64UrlEncodedString:(NSString *)base64UrlEncodedString;
-(NSString *)base64String;
-(NSString *)base64UrlEncodedString;
@end

@interface MF_Base64Codec : NSObject
+(NSData *)dataFromBase64String:(NSString *)base64String;
+(NSString *)base64StringFromData:(NSData *)data;
+(NSString *)base64UrlEncodedStringFromBase64String:(NSString *)base64String;
+(NSString *)base64StringFromBase64UrlEncodedString:(NSString *)base64UrlEncodedString;
@end
44 changes: 44 additions & 0 deletions Base64/MF_Base64Additions.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,34 @@
#import "MF_Base64Additions.h"

@implementation MF_Base64Codec

+(NSString *)base64StringFromBase64UrlEncodedString:(NSString *)base64UrlEncodedString
{
NSString *s = base64UrlEncodedString;
s = [s stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
s = [s stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
switch (s.length % 4) {
case 2:
s = [s stringByAppendingString:@"=="];
break;
case 3:
s = [s stringByAppendingString:@"="];
break;
default:
break;
}
return s;
}

+(NSString *)base64UrlEncodedStringFromBase64String:(NSString *)base64String
{
NSString *s = base64String;
s = [s stringByReplacingOccurrencesOfString:@"=" withString:@""];
s = [s stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
s = [s stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
return s;
}

+(NSData *)dataFromBase64String:(NSString *)encoding
{
NSData *data = nil;
Expand Down Expand Up @@ -186,20 +214,36 @@ -(NSString *)base64String
NSData *utf8encoding = [self dataUsingEncoding:NSUTF8StringEncoding];
return [MF_Base64Codec base64StringFromData:utf8encoding];
}
-(NSString *)base64UrlEncodedString
{
return [MF_Base64Codec base64UrlEncodedStringFromBase64String:[self base64String]];
}
+(NSString *)stringFromBase64String:(NSString *)base64String
{
NSData *utf8encoding = [MF_Base64Codec dataFromBase64String:base64String];
return [[NSString alloc] initWithData:utf8encoding encoding:NSUTF8StringEncoding];
}
+(NSString *)stringFromBase64UrlEncodedString:(NSString *)base64UrlEncodedString
{
return [self stringFromBase64String:[MF_Base64Codec base64StringFromBase64UrlEncodedString:base64UrlEncodedString]];
}
@end

@implementation NSData (Base64Addition)
+(NSData *)dataWithBase64String:(NSString *)base64String
{
return [MF_Base64Codec dataFromBase64String:base64String];
}
+(NSData *)dataWithBase64UrlEncodedString:(NSString *)base64UrlEncodedString
{
return [self dataWithBase64String:[MF_Base64Codec base64StringFromBase64UrlEncodedString:base64UrlEncodedString]];
}
-(NSString *)base64String
{
return [MF_Base64Codec base64StringFromData:self];
}
-(NSString *)base64UrlEncodedString
{
return [MF_Base64Codec base64UrlEncodedStringFromBase64String:[self base64String]];
}
@end
Loading

0 comments on commit e9a0c7d

Please sign in to comment.