-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSqliteHelper.m
137 lines (123 loc) · 4.21 KB
/
SqliteHelper.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// SqliteHelper.m
// PasswordSaver
//
// Created by GuoJing on 10-5-3.
// Copyright 2010 GuoJingMe. All rights reserved.
//
#import "SqliteHelper.h"
#import "Consts.h"
#import "lib/db/FMDatabase.h"
#import "lib/db/FMDatabaseAdditions.h"
#import "lib/db/FMResultSet.h"
#import "lib/crypto/crypto.h"
@implementation SqliteHelper
@synthesize database;
@synthesize db;
@synthesize prefs;
-(NSString*)getFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *path = [docsPath stringByAppendingPathComponent:kFileName];
return path;
}
-(BOOL)connect{
NSString *path = [self getFilePath];
[path retain];
prefs = [NSUserDefaults standardUserDefaults];
db = [FMDatabase databaseWithPath:path];
if (![db open]) {
[db release];
NSLog(@"Open db error!");
return NO;
}
NSLog(@"Open db success!");
if (![db tableExists:kDbName]) {
NSLog(@"create table");
[db executeUpdate:@"create table if not exists password_saver (key text not null,pwd,desc text)"];
};
[path release];
return YES;
}
-(BOOL)disconnect{
[db close];
return YES;
}
-(BOOL)insertKey:(NSString*) key insertPwd:(NSString*) password insertDesc:(NSString*) description{
if ([key isEqualToString:(@"")]) {
return NO;
};
NSData * nkey = [password dataUsingEncoding:NSUTF8StringEncoding];
NSData *code = [self stringWithEncode:nkey];
[db executeUpdate:@"insert into password_saver (key,pwd,desc) values (?,?,?)", key, code, description];
[db commit];
return YES;
}
-(BOOL)removeKey:(NSString*) key {
[db executeUpdate:@"delete from password_saver where key = ?", key];
[db commit];
return YES;
}
-(BOOL)checkKey:(NSString*) key{
FMResultSet *results = [db executeQuery:@"select * from password_saver where key=?", key];
if([results next]){
return YES;
}
return NO;
}
-(NSString*)checkPwd:(NSString*) key{
FMResultSet *results = [db executeQuery:@"select * from password_saver where key=?", key];
NSLog(@"Search password by key %@", key);
while ([results next]) {
NSData *code = [results dataForColumn:@"pwd"];
NSLog(@"password is %@", code);
code = [self stringWithDecode:code];
NSString* pwd = [[NSString alloc] initWithData:code encoding:NSUTF8StringEncoding];
NSLog(@"password is %@", pwd);
[results close];
return pwd;
};
return @"N";
}
-(NSMutableArray*)getValues:(NSString*) key{
NSMutableArray *array = [[NSMutableArray alloc] init];
NSString *sql = [[NSString alloc] initWithFormat:@"select key,desc from password_saver where key like '%%%@%%'",key];
if ([key isEqualToString:@""]) {
sql = [[NSString alloc] initWithFormat:@"select key,desc from password_saver"];
}
FMResultSet *results = [db executeQuery:sql];
while ([results next]){
NSString *keyValue = @"Secret";
NSString * pwdValue = @"";
NSString *descValue = [results stringForColumn:@"desc"];
KeyModel *keyModel = [[KeyModel alloc] initWithKey:keyValue initWithPassword:pwdValue initWithDescription:descValue];
[array addObject:keyModel];
};
return array;
}
-(NSData*)stringWithEncode:(NSData*) nkey{
CCAlgorithm algo = kCCAlgorithmAES128;
CCOptions opts = kCCOptionPKCS7Padding;
NSString * iv = nil;
CCCryptorStatus status = kCCSuccess;
nkey = [nkey dataEncryptedUsingAlgorithm: algo
key: kKey
initializationVector: iv
options: opts
error: &status];
NSLog(@"Encode to %@", nkey);
return nkey;
}
-(NSData*)stringWithDecode:(NSData*) nkey{
CCAlgorithm algo = kCCAlgorithmAES128;
CCOptions opts = kCCOptionPKCS7Padding;
NSString * iv = nil;
CCCryptorStatus status = kCCSuccess;
nkey = [nkey decryptedDataUsingAlgorithm: algo
key: kKey
initializationVector: iv
options: opts
error: &status];
return nkey;
}
@end