-
Notifications
You must be signed in to change notification settings - Fork 29
/
SQLCipherManager.h
184 lines (153 loc) · 8.69 KB
/
SQLCipherManager.h
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// SQLCipherManager.h
// Strip
//
// Created by Billy Gray on 12/30/09.
// Copyright 2009 Zetetic LLC. All rights reserved.
//
#import <Foundation/Foundation.h>
#ifdef ZETETIC_SQLCIPHER_MANAGER_IN_FRAMEWORK
#import <ZTDataModel/sqlite3.h>
#else
#import "sqlite3.h"
#endif
#define ERR_SQLCIPHER_COMMAND_FAILED 1
extern NSString *_Nonnull const SQLCipherManagerCommandException;
extern NSString *_Nonnull const SQLCipherManagerUserInfoQueryKey;
@class SQLCipherManager;
typedef enum : NSUInteger {
PBKDF2_HMAC_ALGORITHM_DEFAULT,
PBKDF2_HMAC_ALGORITHM_SHA1,
PBKDF2_HMAC_ALGORITHM_SHA256,
PBKDF2_HMAC_ALGORITHM_SHA512,
} PBKDF2_HMAC_ALGORITHM;
typedef enum : NSUInteger {
HMAC_ALGORITHM_DEFAULT,
HMAC_ALGORITHM_SHA1,
HMAC_ALGORITHM_SHA256,
HMAC_ALGORITHM_SHA512,
} HMAC_ALGORITHM;
NS_ASSUME_NONNULL_BEGIN
@protocol SQLCipherManagerDelegate <NSObject>
@optional
- (void)didOpenDatabase:(SQLCipherManager *)manager;
- (void)didCreateDatabase:(SQLCipherManager *)manager;
- (void)didEncounterRekeyError;
- (void)didEncounterDatabaseError:(NSString *_Nullable)error;
- (void)sqlCipherManagerWillRekeyDatabase;
- (void)sqlCipherManagerDidRekeyDatabase;
@end
@interface SQLCipherManager : NSObject
@property (nonatomic, nullable) sqlite3 *database;
@property (nonatomic) BOOL inTransaction;
@property (nonatomic, weak, nullable) id<SQLCipherManagerDelegate> delegate;
@property (nonatomic, strong, nullable) NSString *cachedPassword;
@property (nonatomic, strong, nullable) NSString *databasePath;
@property (nonatomic, strong, nullable) NSURL *databaseUrl;
@property (nonatomic) BOOL useHMACPageProtection;
@property (nonatomic) NSInteger schemaVersion;
@property (nonatomic, readonly) BOOL isDatabaseUnlocked;
@property (nonatomic) NSInteger kdfIterations;
@property (nonatomic, readonly, nullable) dispatch_queue_t serialQueue;
@property (weak, nonatomic, readonly, nullable) NSString *cipherVersion;
@property (weak, nonatomic, readonly, nullable) NSString *cipherProvider;
@property (readonly) NSInteger freeListCount;
@property (readonly) NSInteger pageCount;
@property (readonly) float freeListRatio;
- (instancetype)initWithURL:(NSURL *)absoluteUrl;
- (instancetype)initWithPath:(NSString *)path; // DEPRECATED
+ (instancetype)sharedManager;
+ (void)setSharedManager:(SQLCipherManager *)manager;
+ (void)clearSharedManager;
+ (BOOL)passwordIsValid:(NSString *)password;
- (NSNumber *_Nullable)databaseSize;
// Open, Close, and Re-Key methods
- (void)createDatabaseWithPassword:(NSString *)password;
- (void)createDatabaseWithPassword:(NSString *)password license:(NSString *_Nullable)licenseKey;
- (BOOL)openDatabaseWithPassword:(NSString *)password;
- (BOOL)openDatabaseWithPassword:(NSString *)password license:(NSString *_Nullable)licenseKey;
- (BOOL)openDatabaseWithCachedPassword;
- (BOOL)openDatabaseWithCachedPasswordAndLicense:(NSString *_Nullable)licenseKey;
- (BOOL)openDatabaseWithOptions:(NSString *)password cipher:(NSString *)cipher iterations:(NSInteger)iterations;
- (BOOL)openDatabaseWithOptions:(NSString *)password cipher:(NSString *)cipher iterations:(NSInteger)iterations withHMAC:(BOOL)useHMAC;
- (BOOL)openDatabaseWithOptions:(NSString *)password cipher:(NSString *)cipher iterations:(NSInteger)iterations withHMAC:(BOOL)useHMAC license:(NSString *_Nullable)licenseKey;
- (BOOL)openDatabaseWithOptions:(NSString *)password cipher:(NSString *)cipher iterations:(NSInteger)iterations withHMAC:(BOOL)useHMAC pageSize:(NSInteger)pageSize license:(NSString *_Nullable)license;
- (BOOL)openDatabaseWithOptions:(NSString *)password cipher:(NSString *)cipher iterations:(NSInteger)iterations withHMAC:(BOOL)useHMAC pageSize:(NSInteger)pageSize kdfAlgo:(PBKDF2_HMAC_ALGORITHM)kdfAlgo license:(NSString *_Nullable)license;
- (BOOL)openDatabaseWithOptions:(NSString *)password cipher:(NSString *)cipher iterations:(NSInteger)iterations withHMAC:(BOOL)useHMAC pageSize:(NSInteger)pageSize kdfAlgo:(PBKDF2_HMAC_ALGORITHM)kdfAlgo hmacAlgo:(HMAC_ALGORITHM)hmacAlgo license:(NSString *_Nullable)license;
- (BOOL)openAndRekeyCFBDatabaseWithPassword:(NSString *)password __attribute__((deprecated));
- (BOOL)rekeyDatabaseWithPassword:(NSString *)password;
- (BOOL)rekeyDatabaseWithOptions:(NSString *)password
cipher:(NSString *_Nullable)cipher
iterations:(NSInteger)iterations
error:(NSError *_Nullable*_Nullable)error;
- (void)closeDatabase;
- (void)reallyCloseDatabase;
- (BOOL)reopenDatabase:(NSError *_Nullable*_Nullable)error;
// Open, Close, and Re-Key using Raw Data
- (void)createDatabaseWithRawData:(NSString *)rawHexKey;
- (void)createDatabaseWithRawData:(NSString *)rawHexKey license:(NSString *_Nullable)licenseKey;
- (BOOL)openDatabaseWithRawData:(NSString *)rawHexKey;
- (BOOL)openDatabaseWithRawData:(NSString *)rawHexKey license:(NSString *_Nullable)licenseKey;
- (BOOL)openDatabaseWithRawData:(NSString *)rawHexKey cipher:(NSString *)cipher withHMAC:(BOOL)useHMAC;
- (BOOL)openDatabaseWithRawData:(NSString *)rawHexKey cipher:(NSString *)cipher withHMAC:(BOOL)useHMAC license:(NSString *_Nullable)licenseKey;
- (BOOL)rekeyDatabaseWithRawData:(NSString *)rawHexKey;
- (BOOL)rekeyDatabaseRawDataWithOptions:(NSString *)rawHexKey cipher:(NSString *)cipher iterations:(NSInteger)iterations error:(NSError *_Nullable*_Nullable)error;
// Backup and File Location methods
- (NSString *)databaseDirectory;
- (BOOL)databaseExists;
- (NSString *)pathToDatabase;
- (NSString *)pathToRollbackDatabase;
- (NSString *)pathToRekeyDatabase;
- (BOOL)restoreDatabaseFromRollback:(NSError *_Nullable*_Nullable)error;
- (BOOL)removeRollbackDatabase:(NSError *_Nullable*_Nullable)error;
- (BOOL)restoreDatabaseFromFileAtPath:(NSString *)path error:(NSError *_Nullable*_Nullable)error;
- (BOOL)restoreDatabaseFromFileAtPath:(NSString *)path error:(NSError *_Nullable*_Nullable)error requiresMainDbFilePresent:(BOOL)requiresMainDbPresent;
- (BOOL)createReplicaAtPath:(NSString *)path;
- (BOOL)createRollbackDatabase:(NSError *_Nullable*_Nullable)error;
- (BOOL)copyDatabaseToPath:(NSString *)path error:(NSError *_Nullable*_Nullable)error;
// Schema methods
- (NSInteger)getSchemaVersion __attribute__((deprecated)); // DEPRECATED, use schemaVersion dynamic property
// Query / Transaction methods
- (void)execute:(NSString *)sqlCommand; // throws an NSException on command failure
- (BOOL)execute:(NSString *)sqlCommand error:(NSError *_Nullable*_Nullable)error;
- (void)execute:(NSString *)query withBlock:(void (^)(sqlite3_stmt *stmt))block;
- (void)execute:(NSString *)sqlCommand withParams:(NSArray *_Nullable)params;
- (BOOL)execute:(NSString *)sqlCommand error:(NSError *_Nullable*_Nullable)error withParams:(NSArray *_Nullable)params;
- (BOOL)execute:(NSString *)sqlCommand error:(NSError *_Nullable*_Nullable)error withArguments:(NSArray *_Nullable)arguments;
- (void)beginTransaction;
- (void)commitTransaction;
- (void)rollbackTransaction;
- (void)transactionWithBlock:(void(^)(void))block;
- (NSString *_Nullable)getScalar:(NSString *)query;
- (NSString *_Nullable)getScalarWith:(NSString *)query __attribute__((deprecated));
- (NSString *_Nullable)getScalar:(NSString *)query with:(NSArray *_Nullable)params;
- (NSData *_Nullable)getBlobWith:(NSString *)query;
- (NSInteger)countForSQL:(NSString *)countSQL;
- (NSInteger)countForSQL:(NSString *)countSQL with:(NSArray *)params;
- (NSInteger)countForTable:(NSString *)tableName;
- (dispatch_queue_t)serialQueue;
- (void)inQueue:(void (^)(SQLCipherManager *manager))block;
- (void)inQueueAsync:(void (^)(SQLCipherManager *manager))block;
- (BOOL)tableExists:(NSString *)tableName;
/**
Runs a SQL query and returns the results as an NSArray, each item of which is an NSArray representing a result row.
@param SQL Any SQL query
@param params Optional bind parameters for `SQL` param (supports NSString, NSData, and NSNumber types only)
@param error If an error occurs it will be supplied to this parameter by reference
@return An NSArray of rows (NSArray). Array will be empty if no rows are returned, and nil if an error occurs
*/
- (NSArray<NSArray *> *_Nullable)rowsFor:(NSString *)SQL
with:(NSArray *_Nullable)params
error:(NSError *_Nullable*_Nullable)error;
+ (NSError *)errorForResultCode:(NSInteger)resultCode;
+ (NSError *)errorForResultCode:(NSInteger)resultCode reason:(NSString * _Nullable)localizedReason;
+ (NSError *)errorWithDescription:(NSString *)localizedDescription reason:(NSString * _Nullable)localizedReason;
- (BOOL)isDatabaseAttachedNamed:(NSString *)databaseName;
- (NSInteger)numberOfRowsChangedByLastCommand;
/// Vacuums the database.
///
/// @Warning Must not be called in a transaction.
/// @See `freeListCount`, `pageCount`, `freeListRatio`.
- (void)vacuum;
@end
NS_ASSUME_NONNULL_END