3

I'm storing some key/value-pairs as Strings in CoreData which - in a newer version - should now be encrypted. To not alone relying on Apples DataProtection I now want to encrypt the data before storing with RNCryptor and with the help of the ValueTransformer class.

However, the transform-functions are not called, neither debug-outputs nor breakpoints are triggered. The strings are now stored as data objects, but can be read in plain text in binary representation - so they are obviously not encrypted.

DB Browser

Here is what I changed:

  • Added and activated a new migration/database scheme
  • Changed key and value column from Type String to Transformable
  • Set the Value Transformer to »EncryptedStringTransformer« and the Custom Class to »String«

Data model

  • and finally I added a file Encryption.swift with the following implementation:
import Foundation
import RNCryptor

class EncryptedStringTransformer : ValueTransformer {
    let password = "SuperSecurePassword"

    override class func allowsReverseTransformation() -> Bool{
        return true
    }

    func transformedValue(value: String?) -> NSData? {

        guard let data = value else {
            return nil
        }
        let encryptData = Data(data.utf8)
        let ciphertext = RNCryptor.encrypt(data: encryptData, withPassword: password)
        return ciphertext as NSData
    }

    func reverseTransformedValue(value: NSData?) -> String? {
        guard value != nil else {
            return "nil"
        }
        do {
            let originalData = try RNCryptor.decrypt(data: (value! as Data), withPassword: password)
            return String(data: originalData, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!

        } catch {
            print(error)
            return "nil"
        }
    }
}

So, the app continues to work flawlessly, and all database objects can be stored and retrieved (with the difference that they are now stored as a Data object rather than a String). I'm checking the SQLite database directly with »DB Browser for SQLite«.

The expected behavior would be encrypted entries in CoreData. Can someone tell me what I am missing? Some tutorials I found don't do any additional implementations and the few articles here on StackOverflow aren't helping either with this issue.

I tried to change the output data from the transform-functions from Data to NSData, with no result. Am I missing something, so that the ValueTransformer is actually called? Any hint would be highly appreciated!

Marc
  • 73
  • 1
  • 5
  • Set a breakpoint in `transformedValue` to make sure the method is called and the encryption is happening. See also https://stackoverflow.com/questions/1598600/why-is-my-transformable-core-data-attribute-not-using-my-custom-nsvaluetransforme/1601546#1601546. There is a suggestion that the transformer class must be registered before it will be called. I don't know if this is true or not, but it is worth checking out. – Mike Taverne Nov 24 '18 at 00:13

1 Answers1

2

You haven't overridden the correct methods of ValueTransformer. Your methods are:

func transformedValue(value: String?) -> NSData?
func reverseTransformedValue(value: NSData?) -> String?

The correct methods are:

func transformedValue(_ value: Any?) -> Any?
func reverseTransformedValue(_ value: Any?) -> Any?

The big hint that you're implementing the wrong methods is that you didn't need to add the override keyword.

BTW, this expression:

encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!

can be replaced with:

encoding: .uf8

It would also likely be better to replace your return "nil" with return nil; it's a String?, so it can be nil if things go wrong.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Thanks rob and sorry for the late reply. Your hint with the missing override statement was very helpful and instructive! The utf8-part was not final but it's corrected now. However, the correct ValueTransformer-methods still won't kick in. breakpoints within those two functions aren't triggered and I'm completely clueless on which part I should have a look at… Do you have an additional hint what could be missing? – Marc Jan 04 '19 at 12:34
  • 2
    I believe you still need to mark EncryptedStringTransformer as @objc, even though it's a subclass of ValueTransformer. A good way to explore that is to change the name of the transformer to something that you know doesn't exist, to see whether you get errors or not. Swift types do not always have exactly the name you expect (it's usually Module.ClassName, for example). You may want to use the `@objc(NameForObjC)` syntax to set the name precisely the way you want it. – Rob Napier Jan 04 '19 at 14:12
  • 1
    Yes, setting the `@objc(ClassName)` explicitly did work! Now the ValueTransformer is called correctly. I got some _SwiftStringStorage/NSData type casting problems now on the inside, but I guess I will manage that… :) Thanks again, Rob. That's been a great help! – Marc Jan 04 '19 at 15:07
  • @RobNapier would you please look on this https://stackoverflow.com/questions/61276524/transformable-attribute-in-core-data-not-sync-with-icloud – Shourob Datta Apr 17 '20 at 17:54