Browse Source

Merge pull request #768 from niveus/Check-for-SQLCipher

Check that we are actually connected to an SQLCipher DB
August "Gus" Mueller 6 years ago
parent
commit
9a98636848
2 changed files with 18 additions and 1 deletions
  1. 1 1
      FMDB.podspec
  2. 17 0
      src/fmdb/FMDatabase.m

+ 1 - 1
FMDB.podspec

@@ -43,7 +43,7 @@ Pod::Spec.new do |s|
     ss.dependency 'SQLCipher', '~> 4.0'
     ss.source_files = 'src/fmdb/FM*.{h,m}'
     ss.exclude_files = 'src/fmdb.m'
-    ss.xcconfig = { 'OTHER_CFLAGS' => '$(inherited) -DSQLITE_HAS_CODEC -DHAVE_USLEEP=1', 'HEADER_SEARCH_PATHS' => 'SQLCipher' }
+    ss.xcconfig = { 'OTHER_CFLAGS' => '$(inherited) -DSQLITE_HAS_CODEC -DHAVE_USLEEP=1 -DSQLCIPHER_CRYPTO', 'HEADER_SEARCH_PATHS' => 'SQLCipher' }
   end
   
 end

+ 17 - 0
src/fmdb/FMDatabase.m

@@ -497,12 +497,29 @@ - (BOOL)goodConnection {
         return NO;
     }
     
+#ifdef SQLCIPHER_CRYPTO
+    // Starting with Xcode8 / iOS 10 we check to make sure we really are linked with
+    // SQLCipher because there is no longer a linker error if we accidently link
+    // with unencrypted sqlite library.
+    //
+    // https://discuss.zetetic.net/t/important-advisory-sqlcipher-with-xcode-8-and-new-sdks/1688
+    
+    FMResultSet *rs = [self executeQuery:@"PRAGMA cipher_version"];
+
+    if ([rs next]) {
+        NSLog(@"SQLCipher version: %@", rs.resultDictionary[@"cipher_version"]);
+        
+        [rs close];
+        return YES;
+    }
+#else
     FMResultSet *rs = [self executeQuery:@"select name from sqlite_master where type='table'"];
     
     if (rs) {
         [rs close];
         return YES;
     }
+#endif
     
     return NO;
 }