Switches from GCDWebServer to CocoaHTTPServer so we can have HTTPS eventually
This commit is contained in:
@@ -14,7 +14,8 @@
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMBridge : NSObject
|
||||
@property (nonatomic, assign) const char *dylibPath;
|
||||
@property (nonatomic, assign) const char *dylibPath;
|
||||
@property (nonatomic, readonly) NSOperationQueue *operationQueue;
|
||||
|
||||
+ (instancetype)sharedInstance;
|
||||
|
||||
|
||||
@@ -8,9 +8,10 @@
|
||||
|
||||
#import "MBIMBridge.h"
|
||||
#import "MBIMBridgeOperation.h"
|
||||
#import "MBIMHTTPConnection.h"
|
||||
#import "hooking.h"
|
||||
|
||||
#import <GCDWebServers/GCDWebServers.h>
|
||||
#import <CocoaHTTPServer/CocoaHTTPServer.h>
|
||||
|
||||
#import <IMCore/IMCore.h>
|
||||
#import <IMCore/IMCore_Private.h>
|
||||
@@ -21,7 +22,7 @@
|
||||
static NSString *const MBIMBridgeToken = @"net.buzzert.kordophone";
|
||||
|
||||
@interface MBIMBridge (/* INTERNAL */)
|
||||
@property (nonatomic, strong) GCDWebServer *webServer;
|
||||
@property (nonatomic, strong) HTTPServer *httpServer;
|
||||
@property (nonatomic, strong) NSOperationQueue *operationQueue;
|
||||
|
||||
- (instancetype)_init;
|
||||
@@ -130,33 +131,16 @@ static NSString *const MBIMBridgeToken = @"net.buzzert.kordophone";
|
||||
#pragma mark -
|
||||
#pragma mark Web Server initialization
|
||||
|
||||
- (void)_handleAsyncServerRequest:(GCDWebServerRequest *)request completion:(GCDWebServerCompletionBlock)completion
|
||||
{
|
||||
NSString *endpointName = [[request URL] lastPathComponent];
|
||||
Class operationClass = [MBIMBridgeOperation operationClassForEndpointName:endpointName];
|
||||
if (operationClass != nil) {
|
||||
MBIMBridgeOperation *operation = [[operationClass alloc] initWithRequest:request completion:completion];
|
||||
[[self operationQueue] addOperation:operation];
|
||||
} else {
|
||||
completion([GCDWebServerDataResponse responseWithStatusCode:404]);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)startWebServer
|
||||
{
|
||||
[GCDWebServer setLogLevel:3];
|
||||
self.httpServer = [[HTTPServer alloc] init];
|
||||
[self.httpServer setConnectionClass:[MBIMHTTPConnection class]];
|
||||
[self.httpServer setPort:8080];
|
||||
|
||||
__auto_type __weak weakSelf = self;
|
||||
self.webServer = [[GCDWebServer alloc] init];
|
||||
[self.webServer addDefaultHandlerForMethod:@"GET"
|
||||
requestClass:[GCDWebServerRequest class]
|
||||
asyncProcessBlock:^(__kindof GCDWebServerRequest * _Nonnull request, GCDWebServerCompletionBlock _Nonnull completionBlock) { [weakSelf _handleAsyncServerRequest:request completion:completionBlock]; }];
|
||||
|
||||
[self.webServer addDefaultHandlerForMethod:@"POST"
|
||||
requestClass:[GCDWebServerURLEncodedFormRequest class]
|
||||
asyncProcessBlock:^(__kindof GCDWebServerRequest * _Nonnull request, GCDWebServerCompletionBlock _Nonnull completionBlock) { [weakSelf _handleAsyncServerRequest:request completion:completionBlock]; }];
|
||||
|
||||
[self.webServer startWithPort:8080 bonjourName:nil];
|
||||
NSError *error = nil;
|
||||
if (![self.httpServer start:&error]) {
|
||||
NSLog(@"Error starting HTTP server: %@", [error localizedDescription]);
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
||||
13
kordophone/Bridge/MBIMHTTPConnection.h
Normal file
13
kordophone/Bridge/MBIMHTTPConnection.h
Normal file
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// MBIMHTTPConnection.h
|
||||
// CocoaHTTPServer
|
||||
//
|
||||
// Created by James Magahern on 11/16/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import <CocoaHTTPServer/CocoaHTTPServer.h>
|
||||
|
||||
@interface MBIMHTTPConnection : HTTPConnection
|
||||
|
||||
@end
|
||||
70
kordophone/Bridge/MBIMHTTPConnection.m
Normal file
70
kordophone/Bridge/MBIMHTTPConnection.m
Normal file
@@ -0,0 +1,70 @@
|
||||
//
|
||||
// MBIMHTTPConnection.m
|
||||
// CocoaHTTPServer
|
||||
//
|
||||
// Created by James Magahern on 11/16/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMHTTPConnection.h"
|
||||
#import "MBIMBridge.h"
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
@implementation MBIMHTTPConnection {
|
||||
NSMutableData *_bodyData;
|
||||
}
|
||||
|
||||
- (BOOL)supportsMethod:(NSString *)method atPath:(NSString *)path
|
||||
{
|
||||
if ([method isEqualToString:@"GET"] || [method isEqualToString:@"POST"]) {
|
||||
return YES;
|
||||
}
|
||||
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path
|
||||
{
|
||||
__block NSObject<HTTPResponse> *response = nil;
|
||||
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
|
||||
MBIMBridgeOperationCompletionBlock completion = ^(NSObject<HTTPResponse> *incomingResponse) {
|
||||
response = incomingResponse;
|
||||
dispatch_semaphore_signal(sema);
|
||||
};
|
||||
|
||||
NSURL *url = [NSURL URLWithString:path];
|
||||
NSString *endpointName = [url lastPathComponent];
|
||||
Class operationClass = [MBIMBridgeOperation operationClassForEndpointName:endpointName];
|
||||
if (operationClass != nil) {
|
||||
MBIMBridgeOperation *operation = [[operationClass alloc] initWithRequestURL:url completion:completion];
|
||||
operation.requestBodyData = _bodyData;
|
||||
|
||||
[[[MBIMBridge sharedInstance] operationQueue] addOperation:operation];
|
||||
dispatch_semaphore_wait(sema, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.0 * NSEC_PER_SEC)));
|
||||
} else {
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:404];
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
- (BOOL)expectsRequestBodyFromMethod:(NSString *)method atPath:(NSString *)path
|
||||
{
|
||||
if ([method isEqualToString:@"POST"]) {
|
||||
return YES;
|
||||
}
|
||||
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (void)prepareForBodyWithSize:(UInt64)contentLength
|
||||
{
|
||||
_bodyData = [[NSMutableData alloc] initWithCapacity:contentLength];
|
||||
}
|
||||
|
||||
- (void)processBodyData:(NSData *)postDataChunk
|
||||
{
|
||||
[_bodyData appendData:postDataChunk];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -7,18 +7,23 @@
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <GCDWebServers/GCDWebServers.h>
|
||||
#import <CocoaHTTPServer/CocoaHTTPServer.h>
|
||||
|
||||
#import "MBIMJSONDataResponse.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
typedef void (^MBIMBridgeOperationCompletionBlock)(NSObject * _Nullable response);
|
||||
|
||||
@interface MBIMBridgeOperation : NSOperation
|
||||
@property (class, nonatomic, readonly) NSString *endpointName;
|
||||
|
||||
@property (nonatomic, readonly) GCDWebServerRequest *request;
|
||||
@property (nonatomic, readonly) GCDWebServerCompletionBlock serverCompletionBlock;
|
||||
@property (nonatomic, strong) NSData *requestBodyData;
|
||||
@property (nonatomic, readonly) NSURL *requestURL;
|
||||
@property (nonatomic, readonly) MBIMBridgeOperationCompletionBlock serverCompletionBlock;
|
||||
|
||||
+ (nullable Class)operationClassForEndpointName:(NSString *)endpointName;
|
||||
- (instancetype)initWithRequest:(GCDWebServerRequest *)request completion:(GCDWebServerCompletionBlock)completionBlock;
|
||||
- (instancetype)initWithRequestURL:(NSURL *)requestURL completion:(MBIMBridgeOperationCompletionBlock)completionBlock;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
@interface MBIMBridgeOperation (/*INTERNAL*/)
|
||||
@property (nonatomic, copy) GCDWebServerCompletionBlock serverCompletionBlock;
|
||||
@property (nonatomic, strong) GCDWebServerRequest *request;
|
||||
@property (nonatomic, strong) NSURL *requestURL;
|
||||
@property (nonatomic, copy) MBIMBridgeOperationCompletionBlock serverCompletionBlock;
|
||||
@end
|
||||
|
||||
@implementation MBIMBridgeOperation
|
||||
@@ -44,11 +44,11 @@
|
||||
return [[self _operationClassMapping] objectForKey:endpointName];
|
||||
}
|
||||
|
||||
- (instancetype)initWithRequest:(GCDWebServerRequest *)request completion:(GCDWebServerCompletionBlock)completionBlock
|
||||
- (instancetype)initWithRequestURL:(NSURL *)requestURL completion:(MBIMBridgeOperationCompletionBlock)completionBlock
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
self.request = request;
|
||||
self.requestURL = requestURL;
|
||||
self.serverCompletionBlock = completionBlock;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
#import "MBIMConversationListOperation.h"
|
||||
#import "MBIMHTTPUtilities.h"
|
||||
|
||||
#import <IMCore/IMCore.h>
|
||||
|
||||
@@ -28,7 +29,7 @@
|
||||
NSMutableDictionary *chatDict = [NSMutableDictionary dictionary];
|
||||
chatDict[@"guid"] = [chat guid];
|
||||
chatDict[@"displayName"] = [chat displayName];
|
||||
chatDict[@"date"] = GCDWebServerFormatRFC822([chat lastFinishedMessageDate]);
|
||||
chatDict[@"date"] = MBIMWebServerFormatRFC822([chat lastFinishedMessageDate]);
|
||||
|
||||
IMMessage *lastMessage = [chat lastMessage];
|
||||
if (lastMessage) {
|
||||
@@ -48,7 +49,7 @@
|
||||
[conversations addObject:chatDict];
|
||||
}
|
||||
|
||||
GCDWebServerResponse *response = [GCDWebServerDataResponse responseWithJSONObject:conversations];
|
||||
MBIMJSONDataResponse *response = [MBIMJSONDataResponse responseWithJSONObject:conversations];
|
||||
self.serverCompletionBlock(response);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
#import "MBIMMessagesListOperation.h"
|
||||
#import "MBIMHTTPUtilities.h"
|
||||
|
||||
#import <IMCore/IMCore.h>
|
||||
|
||||
@@ -21,9 +22,9 @@
|
||||
|
||||
- (void)main
|
||||
{
|
||||
GCDWebServerResponse *response = nil;
|
||||
NSObject<HTTPResponse> *response = nil;
|
||||
do {
|
||||
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:[self.request URL] resolvingAgainstBaseURL:NO];
|
||||
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:self.requestURL resolvingAgainstBaseURL:NO];
|
||||
|
||||
NSString *guid = nil;
|
||||
for (NSURLQueryItem *queryItem in [urlComponents queryItems]) {
|
||||
@@ -35,14 +36,14 @@
|
||||
|
||||
if (!guid) {
|
||||
NSLog(@"No query item provided");
|
||||
response = [GCDWebServerDataResponse responseWithStatusCode:500];
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
IMChat *chat = [sChatRegistry existingChatWithGUID:guid];
|
||||
if (!chat) {
|
||||
NSLog(@"Chat with guid: %@ not found", guid);
|
||||
response = [GCDWebServerDataResponse responseWithStatusCode:500];
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -53,12 +54,12 @@
|
||||
for (IMMessageItem *imMessage in [[chat chatItems] messages]) {
|
||||
NSMutableDictionary *messageDict = [NSMutableDictionary dictionary];
|
||||
messageDict[@"text"] = [[imMessage body] string];
|
||||
messageDict[@"date"] = GCDWebServerFormatRFC822([imMessage time]);
|
||||
messageDict[@"date"] = MBIMWebServerFormatRFC822([imMessage time]);
|
||||
messageDict[@"sender"] = [imMessage sender];
|
||||
[messages addObject:messageDict];
|
||||
}
|
||||
|
||||
response = [GCDWebServerDataResponse responseWithJSONObject:messages];
|
||||
response = [MBIMJSONDataResponse responseWithJSONObject:messages];
|
||||
} while (0);
|
||||
|
||||
self.serverCompletionBlock(response);
|
||||
|
||||
@@ -41,20 +41,25 @@
|
||||
|
||||
- (void)main
|
||||
{
|
||||
assert([self.request isKindOfClass:[GCDWebServerURLEncodedFormRequest class]]);
|
||||
NSObject<HTTPResponse> *response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
|
||||
GCDWebServerURLEncodedFormRequest *formRequest = (GCDWebServerURLEncodedFormRequest *)self.request;
|
||||
NSDictionary *args = [formRequest arguments];
|
||||
NSError *error = nil;
|
||||
NSDictionary *args = [NSJSONSerialization JSONObjectWithData:self.requestBodyData options:0 error:&error];
|
||||
if (error || args.count == 0) {
|
||||
self.serverCompletionBlock(response);
|
||||
return;
|
||||
}
|
||||
|
||||
NSString *guid = [args objectForKey:@"guid"];
|
||||
NSString *messageBody = [args objectForKey:@"body"];
|
||||
BOOL result = [self _sendMessage:messageBody toChatWithGUID:guid];
|
||||
if (!guid || !messageBody) {
|
||||
self.serverCompletionBlock(response);
|
||||
return;
|
||||
}
|
||||
|
||||
GCDWebServerResponse *response = nil;
|
||||
BOOL result = [self _sendMessage:messageBody toChatWithGUID:guid];
|
||||
if (result) {
|
||||
response = [GCDWebServerDataResponse responseWithStatusCode:200];
|
||||
} else {
|
||||
response = [GCDWebServerDataResponse responseWithStatusCode:500];
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:200];
|
||||
}
|
||||
|
||||
self.serverCompletionBlock(response);
|
||||
|
||||
11
kordophone/Bridge/Operations/Utilities/MBIMHTTPUtilities.h
Normal file
11
kordophone/Bridge/Operations/Utilities/MBIMHTTPUtilities.h
Normal file
@@ -0,0 +1,11 @@
|
||||
//
|
||||
// MBIMHTTPUtilities.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/16/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NSString* MBIMWebServerFormatRFC822(NSDate *date);
|
||||
33
kordophone/Bridge/Operations/Utilities/MBIMHTTPUtilities.m
Normal file
33
kordophone/Bridge/Operations/Utilities/MBIMHTTPUtilities.m
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// MBIMHTTPUtilities.c
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/16/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#include "MBIMHTTPUtilities.h"
|
||||
|
||||
static NSDateFormatter* _dateFormatterRFC822 = nil;
|
||||
static dispatch_queue_t _dateFormatterQueue = NULL;
|
||||
|
||||
__attribute__((constructor))
|
||||
static void __InitializeDateFormatter()
|
||||
{
|
||||
_dateFormatterQueue = dispatch_queue_create("dateFormatter", DISPATCH_QUEUE_SERIAL);
|
||||
|
||||
_dateFormatterRFC822 = [[NSDateFormatter alloc] init];
|
||||
_dateFormatterRFC822.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
|
||||
_dateFormatterRFC822.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
|
||||
_dateFormatterRFC822.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
|
||||
}
|
||||
|
||||
NSString* MBIMWebServerFormatRFC822(NSDate *date)
|
||||
{
|
||||
__block NSString *string = nil;
|
||||
dispatch_sync(_dateFormatterQueue, ^{
|
||||
string = [_dateFormatterRFC822 stringFromDate:date];
|
||||
});
|
||||
|
||||
return string;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// MBIMJSONDataResponse.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/16/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <CocoaHTTPServer/CocoaHTTPServer.h>
|
||||
|
||||
@interface MBIMJSONDataResponse : HTTPDataResponse
|
||||
|
||||
+ (instancetype)responseWithJSONObject:(id)object;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// MBIMJSONDataResponse.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/16/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMJSONDataResponse.h"
|
||||
|
||||
@implementation MBIMJSONDataResponse
|
||||
|
||||
+ (instancetype)responseWithJSONObject:(id)object
|
||||
{
|
||||
NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:NULL];
|
||||
if (data == nil) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
MBIMJSONDataResponse *response = [[self alloc] initWithData:data];
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
- (NSDictionary *)httpHeaders
|
||||
{
|
||||
return @{
|
||||
@"Content-Type" : @"application/json; charset=utf-8"
|
||||
};
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -1,20 +0,0 @@
|
||||
//
|
||||
// GCDWebServerDataResponse+Crypto.h
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/15/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import <GCDWebServers/GCDWebServers.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface GCDWebServerDataResponse (Crypto)
|
||||
|
||||
+ (nullable instancetype)encryptedResponseWithJSONObject:(id)object;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
//
|
||||
// GCDWebServerDataResponse+Crypto.m
|
||||
// kordophoned
|
||||
//
|
||||
// Created by James Magahern on 11/15/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "GCDWebServerDataResponse+Crypto.h"
|
||||
|
||||
#import "NSData+AES.h"
|
||||
|
||||
// TEMP!!
|
||||
static NSString *const kSymmetricKey = @"axPy0nljtG/TOVJSVwVXag==";
|
||||
|
||||
@implementation GCDWebServerDataResponse (Crypto)
|
||||
|
||||
+ (nullable instancetype)encryptedResponseWithJSONObject:(id)object
|
||||
{
|
||||
NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:NULL];
|
||||
if (data == nil) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSError *error = nil;
|
||||
NSData *ivData = [[[NSUUID UUID] UUIDString] dataUsingEncoding:NSUTF8StringEncoding];
|
||||
NSData *keyData = [[NSData alloc] initWithBase64EncodedString:kSymmetricKey options:0];
|
||||
NSData *encryptedData = [data encryptedDataWithKey:keyData iv:ivData error:&error];
|
||||
if (error) {
|
||||
NSLog(@"Error encrypting response: %@", error);
|
||||
}
|
||||
|
||||
NSString *ivDataString = [ivData base64EncodedStringWithOptions:0];
|
||||
|
||||
GCDWebServerDataResponse *response = [[self alloc] initWithData:encryptedData contentType:@"application/octet-stream"];
|
||||
[response setValue:ivDataString forAdditionalHeader:@"X-KordophoneCrypto-IV"];
|
||||
|
||||
// TODO: is this the right way??
|
||||
[response setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[data length]] forAdditionalHeader:@"X-Decrypted-Content-Length"];
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -1,17 +0,0 @@
|
||||
//
|
||||
// NSData+AES.h
|
||||
// MessagesBridge
|
||||
//
|
||||
// Created by James Magahern on 11/15/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <CommonCrypto/CommonCrypto.h>
|
||||
|
||||
@interface NSData (AES)
|
||||
|
||||
- (NSData *)encryptedDataWithKey:(NSData *)key iv:(NSData *)iv error:(NSError **)error;
|
||||
- (NSData *)decryptedDataWithKey:(NSData *)key iv:(NSData *)iv error:(NSError **)error;
|
||||
|
||||
@end
|
||||
@@ -1,58 +0,0 @@
|
||||
//
|
||||
// NSData+AES.m
|
||||
// MessagesBridge
|
||||
//
|
||||
// Created by James Magahern on 11/15/18.
|
||||
// Copyright © 2018 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NSData+AES.h"
|
||||
|
||||
@implementation NSData (AES)
|
||||
|
||||
+ (NSData *)AES128Operation:(CCOperation)operation
|
||||
withInputData:(NSData *)inputData
|
||||
key:(NSData *)key
|
||||
iv:(NSData *)iv
|
||||
error:(NSError **)error
|
||||
{
|
||||
size_t dataMoved = 0;
|
||||
NSMutableData *outputData = [NSMutableData dataWithLength:(inputData.length + kCCBlockSizeAES128)];
|
||||
CCCryptorStatus status = CCCrypt(
|
||||
operation,
|
||||
kCCAlgorithmAES,
|
||||
kCCOptionPKCS7Padding,
|
||||
key.bytes,
|
||||
key.length,
|
||||
iv.bytes,
|
||||
inputData.bytes,
|
||||
inputData.length,
|
||||
outputData.mutableBytes,
|
||||
outputData.length,
|
||||
&dataMoved
|
||||
);
|
||||
|
||||
if (status == kCCSuccess) {
|
||||
outputData.length = dataMoved;
|
||||
} else {
|
||||
*error = [NSError errorWithDomain:@"CommonCryptoError"
|
||||
code:status
|
||||
userInfo:nil];
|
||||
|
||||
outputData = nil;
|
||||
}
|
||||
|
||||
return outputData;
|
||||
}
|
||||
|
||||
- (NSData *)encryptedDataWithKey:(NSData *)key iv:(NSData *)iv error:(NSError *__autoreleasing *)error
|
||||
{
|
||||
return [[self class] AES128Operation:kCCEncrypt withInputData:self key:key iv:iv error:error];
|
||||
}
|
||||
|
||||
- (NSData *)decryptedDataWithKey:(NSData *)key iv:(NSData *)iv error:(NSError *__autoreleasing *)error
|
||||
{
|
||||
return [[self class] AES128Operation:kCCDecrypt withInputData:self key:key iv:iv error:error];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user