Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
soffes committed Apr 6, 2010
0 parents commit 4248f17
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
.DS_Store
build
*.mode1v3
*.pbxuser
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2010 Tasteful Works, Inc.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions Readme.markdown
@@ -0,0 +1,3 @@
# SSKeychain

This was inspired by [EMKeychain](http://github.com/maccheck/EMKeychain) and [SDKeychain](http://github.com/sdegutis/The-Gist/blob/master/External/SDKeychain). Thanks to the authors.
18 changes: 18 additions & 0 deletions SSKeychain.h
@@ -0,0 +1,18 @@
//
// SSKeychain.h
// SSKeychain
//
// Created by Sam Soffes on 4/6/10.
// Copyright Sam Soffes 2010 . All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import <Security/Security.h>

@interface SSKeychain : NSObject {
}

+ (NSString *)securePasswordForIdentifier:(NSString *)username;
+ (BOOL)setSecurePassword:(NSString *)somePassword forIdentifier:(NSString *)username;

@end
86 changes: 86 additions & 0 deletions SSKeychain.m
@@ -0,0 +1,86 @@
//
// SSKeychain.m
// SSKeychain
//
// Created by Sam Soffes on 4/6/10.
// Copyright Sam Soffes 2010 . All rights reserved.
//


#import "SSKeychain.h"

@interface SSKeychain (PrivateMethods)
+ (SecKeychainItemRef) itemForKeychainUsername:(NSString*)username;
+ (NSString*) _uniqueServiceNameForApp;
@end


@implementation SSKeychain

+ (NSString *)_uniqueServiceNameForApp {
return [[NSBundle mainBundle] bundleIdentifier];
}


+ (SecKeychainItemRef)itemForKeychainUsername:(NSString *)username {
SecKeychainItemRef item = NULL;
NSString *serviceName = [self _uniqueServiceNameForApp];

OSErr err = SecKeychainFindGenericPassword(NULL, [serviceName length], [serviceName UTF8String],
[username length], [username UTF8String], NULL,
NULL, &item);

if (err == noErr || item != NULL) {
return item;
}

return NULL;
}


+ (NSString *)securePasswordForIdentifier:(NSString *)username {
SecKeychainItemRef item = [self itemForKeychainUsername:username];

if (item == NULL) {
return nil;
}

UInt32 passwordLength;
char* password;
OSErr err = SecKeychainItemCopyAttributesAndData(item, NULL, NULL, NULL, &passwordLength, (void**)&password);

if (err != noErr) {
CFStringRef errMsg = SecCopyErrorMessageString(err, NULL);
CFShow(errMsg);
CFRelease(errMsg);
return nil;
}

NSString *passwordString = [[NSString alloc] initWithBytes:password length:passwordLength encoding:NSUTF8StringEncoding];
SecKeychainItemFreeContent(NULL, password);
return [passwordString autorelease];
}


+ (BOOL)setSecurePassword:(NSString *)newPasswordString forIdentifier:(NSString *)username {
if (!newPasswordString) {
newPasswordString = @"";
}

SecKeychainItemRef item = [self itemForKeychainUsername:username];

if (item == NULL) {
NSString *serviceName = [self _uniqueServiceNameForApp];

OSErr err = SecKeychainAddGenericPassword(NULL, [serviceName length], [serviceName UTF8String],
[username length], [username UTF8String], [newPasswordString length],
[newPasswordString UTF8String], &item);
return (err == noErr && item != NULL);
} else {
const char *newPassword = [newPasswordString UTF8String];
OSStatus err = SecKeychainItemModifyAttributesAndData(item, NULL, strlen(newPassword), (void *)newPassword);
return (err == noErr && item != NULL);
}
}

@end

0 comments on commit 4248f17

Please sign in to comment.