Browse Source

[LOG] Database encryption key (write function for simulator)

marinofaggiana 6 năm trước cách đây
mục cha
commit
6fc7aefe4b

+ 5 - 1
iOSClient/Database/NCManageDatabase.swift

@@ -36,7 +36,7 @@ class NCManageDatabase: NSObject {
         let databaseFilePath = dirGroup?.appendingPathComponent("\(k_appDatabaseNextcloud)/\(k_databaseDefault)")
         let databaseEncryptedFilePath = dirGroup?.appendingPathComponent("\(k_appDatabaseNextcloud)/\(k_databaseEncryptedDefault)")
 
-        // Migrate "unencrypted" database to encrypted datadase
+        // Migrate unencrypted database to encrypted datadase
         
         if FileManager.default.fileExists(atPath: databaseFilePath!.path) {
         
@@ -138,6 +138,10 @@ class NCManageDatabase: NSObject {
 
         // Encrypting the database file on disk with AES-256+SHA2 by supplying a 64-byte encryption key
         config.encryptionKey = CCUtility.getDatabaseEncryptionKey()
+
+        #if targetEnvironment(simulator)
+        print("[LOG] Database encryption key:\n" + CCUtility.hexRepresentation(config.encryptionKey, spaces: false))
+        #endif
         
         Realm.Configuration.defaultConfiguration = config
         do {

+ 1 - 0
iOSClient/Utility/CCUtility.h

@@ -247,5 +247,6 @@
 + (NSDate *)datetimeWithOutDate:(NSDate *)datDate;
 + (BOOL)isValidEmail:(NSString *)checkString;
 + (NSString *)URLEncodeStringFromString:(NSString *)string;
++ (NSString*)hexRepresentation:(NSData *)data spaces:(BOOL)spaces;
 
 @end

+ 25 - 0
iOSClient/Utility/CCUtility.m

@@ -1557,4 +1557,29 @@
     return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, str, NULL, charset, encoding));
 }
 
++ (NSString*)hexRepresentation:(NSData *)data spaces:(BOOL)spaces
+{
+    const unsigned char* bytes = (const unsigned char*)[data bytes];
+    NSUInteger nbBytes = [data length];
+    //If spaces is true, insert a space every this many input bytes (twice this many output characters).
+    static const NSUInteger spaceEveryThisManyBytes = 4UL;
+    //If spaces is true, insert a line-break instead of a space every this many spaces.
+    static const NSUInteger lineBreakEveryThisManySpaces = 4UL;
+    const NSUInteger lineBreakEveryThisManyBytes = spaceEveryThisManyBytes * lineBreakEveryThisManySpaces;
+    NSUInteger strLen = 2*nbBytes + (spaces ? nbBytes/spaceEveryThisManyBytes : 0);
+    
+    NSMutableString* hex = [[NSMutableString alloc] initWithCapacity:strLen];
+    for(NSUInteger i=0; i<nbBytes; ) {
+        [hex appendFormat:@"%02X", bytes[i]];
+        //We need to increment here so that the every-n-bytes computations are right.
+        ++i;
+        
+        if (spaces) {
+            if (i % lineBreakEveryThisManyBytes == 0) [hex appendString:@"\n"];
+            else if (i % spaceEveryThisManyBytes == 0) [hex appendString:@" "];
+        }
+    }
+    return hex;
+}
+
 @end