市面上雖有一些不錯的加密相冊 App,但往往內置廣告,或對上傳張數有所限制。本文介紹一款加密相冊的製作過程,它將具備多密碼(輸入不同密碼進入不同空間,可掩人耳目)、WiFi 傳圖、照片檔案加密等功能。專案與文章同步推進,原始碼可在 GitHub 下載。
本篇介紹加密相冊的登錄驗證與註冊模組。註冊時只需密碼,每個密碼對應一個獨立儲存空間;登錄時透過 Touch ID 或密碼驗證。若有多套密碼,Touch ID 會綁定到一個主密碼上(可更改)。
由於加密相冊只用於本地、尚未考慮密碼找回,帳戶只需密碼欄位;為統計現有帳戶數量,再加一個 id 欄位。帳戶類 SGAccount 設計如下。
@interface SGAccount : NSObject
@property (nonatomic, assign) NSInteger accountId;
@property (nonatomic, copy) NSString *password;
@end
為進行歸檔存儲,需實現 NSCoding 相關方法:
#import "SGAccount.h"
NSString * const kSGAccountId = @"kSGAccountId";
NSString * const kSGAccountPwd = @"kSGAccountPwd";
@implementation SGAccount
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeInteger:self.accountId forKey:kSGAccountId];
[encoder encodeObject:self.password forKey:kSGAccountPwd];
}
- (instancetype)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
self.accountId = [decoder decodeIntegerForKey:kSGAccountId];
self.password = [decoder decodeObjectForKey:kSGAccountPwd];
}
return self;
}
+ (BOOL)supportsSecureCoding { return YES; }
@end
對於多個帳戶,用一個集合類管理。由於登錄驗證時需查詢密碼對應的帳戶是否存在,為高效查找,應以密碼為 key 的 Map(即 NSDictionary)存儲。此外還需記錄 Touch ID 對應的密碼,設計如下。
@interface SGAccountSet : NSObject
@property (nonatomic, strong) NSMutableDictionary *accountMap;
@property (nonatomic, copy) NSString *touchIDPassword;
@end
#import "SGAccountSet.h"
NSString * const kSGAccountSetAccountMap = @"kSGAccountSetAccountMap";
NSString * const kSGAccountSetTouchIDPassword = @"kSGAccountSetTouchIDPassword";
@implementation SGAccountSet
+ (BOOL)supportsSecureCoding { return YES; }
- (instancetype)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
self.accountMap = [decoder decodeObjectForKey:kSGAccountSetAccountMap];
self.touchIDPassword = [decoder decodeObjectForKey:kSGAccountSetTouchIDPassword];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:self.accountMap forKey:kSGAccountSetAccountMap];
[encoder encodeObject:self.touchIDPassword forKey:kSGAccountSetTouchIDPassword];
}
- (NSMutableDictionary *)accountMap {
if (_accountMap == nil) { _accountMap = @{}.mutableCopy; }
return _accountMap;
}
@end
accountMap 的懶加載可保證在沒有帳戶數據時拿到的字典不為空。
帳戶管理類對外主要提供註冊與驗證,作為單例使用。註冊只需密碼;驗證分兩種:密碼驗證與 Touch ID 驗證,成功時直接回傳帳戶類。此外還有一個 currentAccount 屬性記錄當前驗證成功的帳戶。
@interface SGAccountManager : NSObject
+ (instancetype)sharedManager;
- (void)registerAccountWithPassword:(NSString *)password errorMessage:(NSString * __autoreleasing *)errorMessage;
- (SGAccount *)getAccountByPwd:(NSString *)pwd;
- (SGAccount *)getTouchIDAccount;
/* 用於 AppDelegate 獲取窗口根控制器:未註冊則進註冊頁,已註冊則進登錄驗證頁 */
- (UIViewController *)getRootViewController;
@property (nonatomic, strong) SGAccount *currentAccount;
@end
私有接口用於內部邏輯,其中 accountSet 存儲所有用戶數據,accountPath 存儲數據保存與加載的路徑。
@interface SGAccountManager ()
@property (nonatomic, strong) SGAccountSet *accountSet;
@property (nonatomic, copy) NSString *accountPath;
@end
accountSet 的懶加載:先從硬碟加載,若無則初始化一個。之所以拆成兩個方法,是因為 loadAccountSet 會在別處被呼叫。
- (SGAccountSet *)accountSet {
if (_accountSet == nil) { [self loadAccountSet]; }
return _accountSet;
}
- (void)loadAccountSet {
SGAccountSet *set = [NSKeyedUnarchiver unarchiveObjectWithFile:self.accountPath];
if (!set) { set = [SGAccountSet new]; }
_accountSet = set;
}
accountPath 的懶加載:數據存儲路徑會在加載與寫入時使用。
- (NSString *)accountPath {
if (_accountPath == nil) {
_accountPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.agony"];
}
return _accountPath;
}
註冊時傳入密碼,加密後先判斷是否已存在(因密碼與儲存空間一一對應,不可重複),重複則透過字串指針回傳錯誤。第一次註冊的密碼會綁定到 Touch ID。
- (void)registerAccountWithPassword:(NSString *)password errorMessage:(NSString * __autoreleasing *)errorMessage {
NSAssert(password != nil, @"password cannot be nil");
password = [self encryptString:password]; // MD5+鹽加密
SGAccount *account = self.accountSet.accountMap[password];
if (account != nil) { *errorMessage = @"Account Already Exists"; return; }
account = [SGAccount new];
NSInteger accountid = self.accountSet.accountMap.allKeys.count + 1;
account.accountId = accountid;
account.password = password;
self.accountSet.accountMap[password] = account;
if (accountid == 1) { // 第一次註冊則綁定到 Touch ID
self.accountSet.touchIDPassword = password;
}
[self saveAccountSet]; // 同步到硬碟
}
加密方法:
- (NSString *)encryptString:(NSString *)string {
return [[[[NSString stringWithFormat:@"allowsad12345%@62232",string] MD5] MD5] MD5];
}
MD5 透過分類加在 NSString 上:
#import "NSString+MD5.h"
#import
@implementation NSString (MD5)
- (NSString *)MD5 {
const char *cStr = [self UTF8String];
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5(cStr, (CC_LONG)strlen(cStr), digest);
NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
[result appendFormat:@"%02x",digest[i]];
}
return result;
}
@end
寫入硬碟:
- (void)saveAccountSet {
[NSKeyedArchiver archiveRootObject:self.accountSet toFile:self.accountPath];
}
密碼驗證:先加密再與集合比對,匹配則成功。
- (SGAccount *)getAccountByPwd:(NSString *)pwd {
pwd = [self encryptString:pwd];
return self.accountSet.accountMap[pwd];
}
Touch ID 驗證:在指紋驗證成功後呼叫,用 Touch ID 對應的密碼驗證。
- (SGAccount *)getTouchIDAccount {
NSString *pwd = self.accountSet.touchIDPassword;
return self.accountSet.accountMap[pwd];
}
若已有帳戶,回傳導航控制器包裹的驗證控制器 SGWelcomeViewController;若未註冊,則初始化導航控制器並 push 一個註冊控制器 SGRegisterViewController,以保證註冊完成後能返回驗證控制器,並與從驗證頁進入的註冊保持相同邏輯。
- (UIViewController *)getRootViewController {
if ([self hasAccount]) {
return [[UINavigationController alloc] initWithRootViewController:[SGWelcomeViewController new]];
}
SGWelcomeViewController *welcomeVc = [SGWelcomeViewController new];
SGRegisterViewController *registerVc = [SGRegisterViewController new];
UINavigationController *nav = [UINavigationController new];
nav.viewControllers = @[welcomeVc, registerVc];
return nav;
}
本篇介紹了與註冊、登錄驗證相關的數據類與管理類的接口與實現。後續的註冊與登錄驗證視圖設計只需使用這些工具類即可。歡迎關注專案後續,下載地址見文章開頭。