Move ssl functions to new class

This commit is contained in:
Felix Schulze 2013-03-16 21:01:07 +01:00
parent cac1dcf74c
commit dac442d3ce
4 changed files with 96 additions and 40 deletions

View File

@ -22,6 +22,7 @@
533ED3281528C5140005C6FA /* Icon~iPad@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 533ED3271528C5140005C6FA /* Icon~iPad@2x.png */; };
533ED32C1528C53B0005C6FA /* libcrypto.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 533ED32A1528C53B0005C6FA /* libcrypto.a */; };
533ED32D1528C53B0005C6FA /* libssl.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 533ED32B1528C53B0005C6FA /* libssl.a */; };
D1E978031547EE765CD39AD2 /* FSOpenSSL.m in Sources */ = {isa = PBXBuildFile; fileRef = D1E97EE2A904D58DAE4231E2 /* FSOpenSSL.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@ -45,6 +46,8 @@
533ED3271528C5140005C6FA /* Icon~iPad@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "Icon~iPad@2x.png"; path = "../Icon~iPad@2x.png"; sourceTree = "<group>"; };
533ED32A1528C53B0005C6FA /* libcrypto.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libcrypto.a; path = lib/libcrypto.a; sourceTree = "<group>"; };
533ED32B1528C53B0005C6FA /* libssl.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libssl.a; path = lib/libssl.a; sourceTree = "<group>"; };
D1E979D803ACC127411934DC /* FSOpenSSL.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FSOpenSSL.h; sourceTree = "<group>"; };
D1E97EE2A904D58DAE4231E2 /* FSOpenSSL.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FSOpenSSL.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -103,6 +106,8 @@
533ED3141528C4840005C6FA /* ViewController_iPhone.xib */,
533ED3171528C4840005C6FA /* ViewController_iPad.xib */,
533ED3061528C4840005C6FA /* Supporting Files */,
D1E97EE2A904D58DAE4231E2 /* FSOpenSSL.m */,
D1E979D803ACC127411934DC /* FSOpenSSL.h */,
);
path = "OpenSSL-for-iOS";
sourceTree = "<group>";
@ -200,6 +205,7 @@
533ED30C1528C4840005C6FA /* main.m in Sources */,
533ED3101528C4840005C6FA /* AppDelegate.m in Sources */,
533ED3131528C4840005C6FA /* ViewController.m in Sources */,
D1E978031547EE765CD39AD2 /* FSOpenSSL.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -0,0 +1,26 @@
//
// FSOpenSSL.h
// OpenSSL-for-iOS
//
// Created by Felix Schulze on 16.03.2013.
// Copyright 2013 Felix Schulze. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import <Foundation/Foundation.h>
@interface FSOpenSSL : NSObject
+ (NSString *)md5FromString:(NSString *)string;
+ (NSString *)sha256FromString:(NSString *)string;
@end

View File

@ -0,0 +1,59 @@
//
// FSOpenSSL.m
// OpenSSL-for-iOS
//
// Created by Felix Schulze on 16.03.2013.
// Copyright 2013 Felix Schulze. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "FSOpenSSL.h"
#include <openssl/md5.h>
#include <openssl/sha.h>
@implementation FSOpenSSL
+ (NSString *)md5FromString:(NSString *)string {
unsigned char *inStrg = (unsigned char *) [[string dataUsingEncoding:NSASCIIStringEncoding] bytes];
unsigned long lngth = [string length];
unsigned char result[MD5_DIGEST_LENGTH];
NSMutableString *outStrg = [NSMutableString string];
MD5(inStrg, lngth, result);
unsigned int i;
for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
[outStrg appendFormat:@"%02x", result[i]];
}
return [outStrg copy];
}
+ (NSString *)sha256FromString:(NSString *)string {
unsigned char *inStrg = (unsigned char *) [[string dataUsingEncoding:NSASCIIStringEncoding] bytes];
unsigned long lngth = [string length];
unsigned char result[SHA256_DIGEST_LENGTH];
NSMutableString *outStrg = [NSMutableString string];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, inStrg, lngth);
SHA256_Final(result, &sha256);
unsigned int i;
for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
[outStrg appendFormat:@"%02x", result[i]];
}
return [outStrg copy];
}
@end

View File

@ -9,8 +9,7 @@
//
#import "ViewController.h"
#include <openssl/md5.h>
#include <openssl/sha.h>
#import "FSOpenSSL.h"
#include <openssl/opensslv.h>
@implementation ViewController
@ -24,55 +23,21 @@
- (IBAction)calculateMD5:(id)sender
{
/** Calculate MD5*/
NSString *string = textField.text;
unsigned char *inStrg = (unsigned char*)[[string dataUsingEncoding:NSASCIIStringEncoding] bytes];
unsigned long lngth = [string length];
unsigned char result[MD5_DIGEST_LENGTH];
NSMutableString *outStrg = [NSMutableString string];
MD5(inStrg, lngth, result);
unsigned int i;
for (i = 0; i < MD5_DIGEST_LENGTH; i++)
{
[outStrg appendFormat:@"%02x", result[i]];
}
md5TextField.text = outStrg;
md5TextField.text = [FSOpenSSL md5FromString:textField.text];
//Hide Keyboard after calculation
[textField resignFirstResponder];
}
- (IBAction)calculateSHA256:(id)sender
{
/* Calculate SHA256 */
NSString *string = textField.text;
unsigned char *inStrg = (unsigned char*)[[string dataUsingEncoding:NSASCIIStringEncoding] bytes];
unsigned long lngth = [string length];
unsigned char result[SHA256_DIGEST_LENGTH];
NSMutableString *outStrg = [NSMutableString string];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, inStrg, lngth);
SHA256_Final(result, &sha256);
unsigned int i;
for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
[outStrg appendFormat:@"%02x", result[i]];
}
sha256TextField.text = outStrg;
{
sha256TextField.text = [FSOpenSSL sha256FromString:textField.text];
//Hide Keyboard after calculation
[textField resignFirstResponder];
}
- (IBAction)showInfo
{
NSString *version = [NSString stringWithCString:OPENSSL_VERSION_TEXT encoding:NSUTF8StringEncoding];
NSString *message = [NSString stringWithFormat:@"OpenSSL-Version: %@\nLicense: See include/LICENSE\n\nCopyright 2010-2012 by Felix Schulze\n http://www.x2on.de", version];
NSString *message = [NSString stringWithFormat:@"OpenSSL-Version: %@\nLicense: See include/LICENSE\n\nCopyright 2010-2013 by Felix Schulze\n http://www.felixschulze.de", @OPENSSL_VERSION_TEXT];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OpenSSL-for-iOS" message:message delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
[alert show];
}