Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 16, 2016
0 parents commit 355d706
Show file tree
Hide file tree
Showing 375 changed files with 40,930 additions and 0 deletions.
Binary file added 9781430243687.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Chapter5/EncryptingOutputStream/.DS_Store
Binary file not shown.

Large diffs are not rendered by default.

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

Binary file not shown.
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "1.0">
<FileBreakpoints>
<FileBreakpoint
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "EncryptingOutputStream/EncryptingOutputStream.m"
timestampString = "360534567.306057"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "30"
endingLineNumber = "30"
landmarkName = "-initWithFileURL:passPhrase:"
landmarkType = "5">
</FileBreakpoint>
</FileBreakpoints>
</Bucket>
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0440"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "AB0EF855157BDDD200370583"
BuildableName = "EncryptingOutputStream.framework"
BlueprintName = "EncryptingOutputStream"
ReferencedContainer = "container:EncryptingOutputStream.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "AB0EF86D157BDDD200370583"
BuildableName = "EncryptingOutputStreamTests.octest"
BlueprintName = "EncryptingOutputStreamTests"
ReferencedContainer = "container:EncryptingOutputStream.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>EncryptingOutputStream.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>AB0EF855157BDDD200370583</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>AB0EF86D157BDDD200370583</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
Binary file not shown.
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>com.apress.BeginningObjectiveC.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2012 Apress. All rights reserved.</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
@@ -0,0 +1,7 @@
//
// Prefix header for all source files of the 'EncryptingOutputStream' target in the 'EncryptingOutputStream' project
//

#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#endif
@@ -0,0 +1,13 @@
//
// EncryptingOutputStream.h
// EncryptingOutputStream
//
// Created by Jim Dovey on 12-06-03.
// Copyright (c) 2012 Apress. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface EncryptingOutputStream : NSOutputStream
- (id) initWithFileURL: (NSURL *) fileURL passPhrase: (NSString *) passPhrase;
@end
@@ -0,0 +1,154 @@
//
// EncryptingOutputStream.m
// EncryptingOutputStream
//
// Created by Jim Dovey on 12-06-03.
// Copyright (c) 2012 Apress. All rights reserved.
//

#import "EncryptingOutputStream.h"
#import "SimpleRunLoopSource.h"
#import <CommonCrypto/CommonCryptor.h>

@implementation EncryptingOutputStream
{
CCCryptorRef _cryptor;
NSFileHandle * _output;
SimpleRunLoopSource * _source;
id<NSStreamDelegate> _delegate __weak;
NSStreamEvent _currentEvent;
NSStreamStatus _status;
NSError * _error;
}

- (id) initWithFileURL: (NSURL *) fileURL passPhrase: (NSString *) passPhrase
{
NSParameterAssert([fileURL isFileURL]);
NSParameterAssert([passPhrase length] != 0);

self = [super init];
if ( self == nil )
return ( nil );

// get the password as data, padded/truncated to the AES256 key size
NSMutableData * passData = [[passPhrase dataUsingEncoding: NSUTF8StringEncoding] mutableCopy];
[passData setLength: kCCKeySizeAES256];

if ( CCCryptorCreate(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
[passData bytes], [passData length], NULL, &_cryptor) != kCCSuccess )
{
return ( nil );
}

// NSFileHandle can't open a file that isn't there. Ensure the destination exists.
if ( [[NSFileManager defaultManager] fileExistsAtPath: [fileURL path]] == NO )
[[NSData data] writeToURL: fileURL options: 0 error: NULL];

// open the output file
_output = [NSFileHandle fileHandleForWritingToURL: fileURL error: NULL];

// create the runloop source, with a simple handler
_source = [[SimpleRunLoopSource alloc] initWithSourceHandler: ^{
if ( _currentEvent == NSStreamEventOpenCompleted )
_status = NSStreamStatusOpen;

[_delegate stream: self handleEvent: _currentEvent];

// always signal space-available for NSFileHandle-based streams, since NSFileHandles can always be written to
_currentEvent = NSStreamEventHasSpaceAvailable;
[_source signal];
}];

// the initial status of the stream
_status = NSStreamStatusNotOpen;

return ( self );
}

- (void) dealloc
{
if ( _cryptor != NULL )
CCCryptorRelease(_cryptor);
}

- (void) open
{
_status = NSStreamStatusOpening;
_currentEvent = NSStreamEventOpenCompleted;
[_source signal];
}

- (NSInteger) write: (const uint8_t *) buffer maxLength: (NSUInteger) len
{
// update our status to indicate we're in the middle of a write operation
_status = NSStreamStatusWriting;

// encrypt the data
NSMutableData * encrypted = [[NSMutableData alloc] initWithLength: len];
size_t numWritten = 0;
CCCryptorStatus status = CCCryptorUpdate(_cryptor, buffer, len, [encrypted mutableBytes], len, &numWritten);
if ( status != kCCSuccess )
{
// an error occurred-- note it, signal the condition, and return -1
_error = [NSError errorWithDomain: @"CoreCryptoErrorDomain" code: status userInfo: nil];
_status = NSStreamStatusError;
_currentEvent = NSStreamEventErrorOccurred;
[_source signal]; // tell the delegate about it
return ( -1 );
}

// write this data out via the superclass
[_output writeData: encrypted];

// reset our status and return the length of the data we wrote (we wrote all of it)
_status = NSStreamStatusOpen;
return ( len );
}

- (void) close
{
// write any final data first
NSMutableData * final = [[NSMutableData alloc] initWithLength: kCCBlockSizeAES128];
size_t numWritten = 0;
if ( CCCryptorFinal(_cryptor, [final mutableBytes], [final length], &numWritten) == kCCSuccess && numWritten != 0 )
[_output writeData: final];

// flush any filesystem buffers to the disk and close the file
[_output synchronizeFile];
[_output closeFile];

// update our status
_status = NSStreamStatusClosed;
}

- (void) setDelegate: (id<NSStreamDelegate>) delegate
{
_delegate = delegate;
}

- (id<NSStreamDelegate>) delegate
{
return ( _delegate );
}

- (void) scheduleInRunLoop: (NSRunLoop *) aRunLoop forMode: (NSString *) mode
{
[_source addToRunLoop: aRunLoop forMode: mode];
}

- (void) removeFromRunLoop: (NSRunLoop *) aRunLoop forMode: (NSString *) mode
{
[_source removeFromRunLoop: aRunLoop forMode: mode];
}

- (NSStreamStatus) streamStatus
{
return ( _status );
}

- (NSError *) streamError
{
return ( _error );
}

@end
@@ -0,0 +1,21 @@
//
// SimpleRunLoopSource.h
// EncryptingOutputStream
//
// Created by Jim Dovey on 12-06-03.
// Copyright (c) 2012 Apress. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface SimpleRunLoopSource : NSObject

- (id) initWithSourceHandler: (void (^)(void)) handler;

- (void) signal;
- (void) invalidate;

- (void) addToRunLoop: (NSRunLoop *) runLoop forMode: (NSString *) mode;
- (void) removeFromRunLoop: (NSRunLoop *) runLoop forMode: (NSString *) mode;

@end

0 comments on commit 355d706

Please sign in to comment.