Skip to content

Commit 9b4f6b6

Browse files
author
Darcy Liu
committed
add CryptoExercise
1 parent 76dcb93 commit 9b4f6b6

28 files changed

Lines changed: 4721 additions & 1 deletion

CryptoExercise.zip

61.9 KB
Binary file not shown.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
3+
File: AppDelegate.h
4+
Abstract: Main controller that houses the operation queue and
5+
initializes the LocalBonjour Controller.
6+
7+
Version: 1.2
8+
9+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
10+
("Apple") in consideration of your agreement to the following terms, and your
11+
use, installation, modification or redistribution of this Apple software
12+
constitutes acceptance of these terms. If you do not agree with these terms,
13+
please do not use, install, modify or redistribute this Apple software.
14+
15+
In consideration of your agreement to abide by the following terms, and subject
16+
to these terms, Apple grants you a personal, non-exclusive license, under
17+
Apple's copyrights in this original Apple software (the "Apple Software"), to
18+
use, reproduce, modify and redistribute the Apple Software, with or without
19+
modifications, in source and/or binary forms; provided that if you redistribute
20+
the Apple Software in its entirety and without modifications, you must retain
21+
this notice and the following text and disclaimers in all such redistributions
22+
of the Apple Software.
23+
Neither the name, trademarks, service marks or logos of Apple Inc. may be used
24+
to endorse or promote products derived from the Apple Software without specific
25+
prior written permission from Apple. Except as expressly stated in this notice,
26+
no other rights or licenses, express or implied, are granted by Apple herein,
27+
including but not limited to any patent rights that may be infringed by your
28+
derivative works or by other works in which the Apple Software may be
29+
incorporated.
30+
31+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
32+
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
33+
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34+
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
35+
COMBINATION WITH YOUR PRODUCTS.
36+
37+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
38+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
39+
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40+
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
41+
DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
42+
CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
43+
APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44+
45+
Copyright (C) 2008-2009 Apple Inc. All Rights Reserved.
46+
47+
*/
48+
49+
#import <UIKit/UIKit.h>
50+
51+
@interface AppDelegate : NSObject {
52+
IBOutlet UIWindow * window;
53+
IBOutlet UINavigationController * navController;
54+
NSOperationQueue * cryptoQueue;
55+
}
56+
57+
@property (nonatomic, retain) UIWindow * window;
58+
@property (nonatomic, retain) UINavigationController * navController;
59+
@property (retain) NSOperationQueue * cryptoQueue;
60+
61+
@end
62+
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
3+
File: AppDelegate.m
4+
Abstract: Main controller that houses the operation queue and
5+
initializes the LocalBonjour Controller.
6+
7+
Version: 1.2
8+
9+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
10+
("Apple") in consideration of your agreement to the following terms, and your
11+
use, installation, modification or redistribution of this Apple software
12+
constitutes acceptance of these terms. If you do not agree with these terms,
13+
please do not use, install, modify or redistribute this Apple software.
14+
15+
In consideration of your agreement to abide by the following terms, and subject
16+
to these terms, Apple grants you a personal, non-exclusive license, under
17+
Apple's copyrights in this original Apple software (the "Apple Software"), to
18+
use, reproduce, modify and redistribute the Apple Software, with or without
19+
modifications, in source and/or binary forms; provided that if you redistribute
20+
the Apple Software in its entirety and without modifications, you must retain
21+
this notice and the following text and disclaimers in all such redistributions
22+
of the Apple Software.
23+
Neither the name, trademarks, service marks or logos of Apple Inc. may be used
24+
to endorse or promote products derived from the Apple Software without specific
25+
prior written permission from Apple. Except as expressly stated in this notice,
26+
no other rights or licenses, express or implied, are granted by Apple herein,
27+
including but not limited to any patent rights that may be infringed by your
28+
derivative works or by other works in which the Apple Software may be
29+
incorporated.
30+
31+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
32+
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
33+
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34+
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
35+
COMBINATION WITH YOUR PRODUCTS.
36+
37+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
38+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
39+
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40+
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
41+
DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
42+
CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
43+
APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44+
45+
Copyright (C) 2008-2009 Apple Inc. All Rights Reserved.
46+
47+
*/
48+
49+
#import "AppDelegate.h"
50+
#import <SystemConfiguration/SystemConfiguration.h>
51+
#import <netinet/in.h>
52+
53+
@implementation AppDelegate
54+
55+
@synthesize window, navController, cryptoQueue;
56+
57+
- (BOOL)isNetworkAvailableFlags:(SCNetworkReachabilityFlags *)outFlags {
58+
SCNetworkReachabilityRef defaultRouteReachability;
59+
struct sockaddr_in zeroAddress;
60+
61+
bzero(&zeroAddress, sizeof(zeroAddress));
62+
zeroAddress.sin_len = sizeof(zeroAddress);
63+
zeroAddress.sin_family = AF_INET;
64+
65+
defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
66+
67+
SCNetworkReachabilityFlags flags;
68+
BOOL gotFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
69+
if (!gotFlags) {
70+
return NO;
71+
}
72+
73+
// kSCNetworkReachabilityFlagsReachable indicates that the specified nodename or address can
74+
// be reached using the current network configuration.
75+
BOOL isReachable = flags & kSCNetworkReachabilityFlagsReachable;
76+
77+
// This flag indicates that the specified nodename or address can
78+
// be reached using the current network configuration, but a
79+
// connection must first be established.
80+
//
81+
// If the flag is false, we don't have a connection. But because CFNetwork
82+
// automatically attempts to bring up a WWAN connection, if the WWAN reachability
83+
// flag is present, a connection is not required.
84+
BOOL noConnectionRequired = !(flags & kSCNetworkReachabilityFlagsConnectionRequired);
85+
if ((flags & kSCNetworkReachabilityFlagsIsWWAN)) {
86+
noConnectionRequired = YES;
87+
}
88+
89+
// Callers of this method might want to use the reachability flags, so if an 'out' parameter
90+
// was passed in, assign the reachability flags to it.
91+
if (outFlags) {
92+
*outFlags = flags;
93+
}
94+
95+
return isReachable && noConnectionRequired;
96+
}
97+
98+
- (void)applicationDidFinishLaunching:(UIApplication *)application {
99+
// Is WiFi network available? That's necessary to use Bonjour.
100+
if ([self isNetworkAvailableFlags:NULL] == NO) {
101+
// open an alert with just an OK button
102+
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No WiFi network available."
103+
message:@"Exit this app and enable WiFi using the Settings application."
104+
delegate:self
105+
cancelButtonTitle:@"OK"
106+
otherButtonTitles: nil];
107+
[alert show];
108+
[alert release];
109+
}
110+
else {
111+
// Add the controller's view as a subview of the window
112+
[self.window addSubview:navController.view];
113+
114+
NSOperationQueue * theQueue = [[NSOperationQueue alloc] init];
115+
self.cryptoQueue = theQueue;
116+
[theQueue release];
117+
}
118+
}
119+
120+
- (void)dealloc {
121+
[cryptoQueue release];
122+
[navController release];
123+
[window release];
124+
[super dealloc];
125+
}
126+
127+
@end
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
3+
File: CryptoClient.h
4+
Abstract: Contains the client networking and cryptographic operations. It
5+
gets invoked by the ServiceController class when the connect button is
6+
pressed.
7+
8+
Version: 1.2
9+
10+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
11+
("Apple") in consideration of your agreement to the following terms, and your
12+
use, installation, modification or redistribution of this Apple software
13+
constitutes acceptance of these terms. If you do not agree with these terms,
14+
please do not use, install, modify or redistribute this Apple software.
15+
16+
In consideration of your agreement to abide by the following terms, and subject
17+
to these terms, Apple grants you a personal, non-exclusive license, under
18+
Apple's copyrights in this original Apple software (the "Apple Software"), to
19+
use, reproduce, modify and redistribute the Apple Software, with or without
20+
modifications, in source and/or binary forms; provided that if you redistribute
21+
the Apple Software in its entirety and without modifications, you must retain
22+
this notice and the following text and disclaimers in all such redistributions
23+
of the Apple Software.
24+
Neither the name, trademarks, service marks or logos of Apple Inc. may be used
25+
to endorse or promote products derived from the Apple Software without specific
26+
prior written permission from Apple. Except as expressly stated in this notice,
27+
no other rights or licenses, express or implied, are granted by Apple herein,
28+
including but not limited to any patent rights that may be infringed by your
29+
derivative works or by other works in which the Apple Software may be
30+
incorporated.
31+
32+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
33+
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
34+
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
35+
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
36+
COMBINATION WITH YOUR PRODUCTS.
37+
38+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
39+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
40+
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41+
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
42+
DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
43+
CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
44+
APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45+
46+
Copyright (C) 2008-2009 Apple Inc. All Rights Reserved.
47+
48+
*/
49+
50+
#import <UIKit/UIKit.h>
51+
52+
@class CryptoClient;
53+
54+
@protocol CryptoClientDelegate
55+
56+
- (void)cryptoClientDidCompleteConnection:(CryptoClient *)cryptoClient;
57+
- (void)cryptoClientDidReceiveError:(CryptoClient *)cryptoClient;
58+
- (void)cryptoClientWillBeginReceivingData:(CryptoClient *)cryptClient;
59+
- (void)cryptoClientDidFinishReceivingData:(CryptoClient *)cryptClient;
60+
- (void)cryptoClientWillBeginVerifyingData:(CryptoClient *)cryptClient;
61+
- (void)cryptoClientDidFinishVerifyingData:(CryptoClient *)cryptClient verified:(BOOL)verified;
62+
63+
@end
64+
65+
@interface CryptoClient : NSObject {
66+
NSNetService * service;
67+
NSInputStream * istr;
68+
NSOutputStream * ostr;
69+
NSObject <CryptoClientDelegate, NSObject> * delegate;
70+
BOOL isConnected;
71+
}
72+
73+
@property (nonatomic, retain) NSNetService * service;
74+
@property (nonatomic, assign) NSObject <CryptoClientDelegate, NSObject> * delegate;
75+
@property (nonatomic, retain) NSInputStream * istr;
76+
@property (nonatomic, retain) NSOutputStream * ostr;
77+
@property (nonatomic) BOOL isConnected;
78+
79+
- (id)initWithService:(NSNetService *)serviceInstance delegate:(NSObject <CryptoClientDelegate, NSObject> *)anObject;
80+
- (NSData *)receiveData;
81+
- (NSUInteger)sendData:(NSData *)outData;
82+
- (void)runConnection;
83+
- (BOOL)verifyBlob:(NSData *)blob;
84+
- (void)forwardVerificationToDelegate:(NSNumber *)verified;
85+
86+
@end

0 commit comments

Comments
 (0)