Some basic crypto laid down and tests
This commit is contained in:
17
kordophone/Crypto/NSData+AES.h
Normal file
17
kordophone/Crypto/NSData+AES.h
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// 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
|
||||
58
kordophone/Crypto/NSData+AES.m
Normal file
58
kordophone/Crypto/NSData+AES.m
Normal file
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// 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