0

I have 3 places (in total 7 warnings) in the project where XCode shows the next warning:

withUnsafeMutableBytes' is deprecated

I tried on my own to resolves these warnings, but with no luck. Can someone help me?

1. let status = cryptData.withUnsafeMutableBytes {ivBytes in
        SecRandomCopyBytes(kSecRandomDefault, kCCBlockSizeAES128, ivBytes)
    }

2. let cryptStatus = cryptData.withUnsafeMutableBytes {cryptBytes in
        data.withUnsafeBytes {dataBytes in
            keyData.withUnsafeBytes {keyBytes in
                CCCrypt(CCOperation(kCCEncrypt),
                        CCAlgorithm(kCCAlgorithmAES),
                        options,
                        keyBytes,
                        keyLength,
                        cryptBytes,
                        dataBytes,
                        data.count,
                        cryptBytes + kCCBlockSizeAES128,
                        cryptLength,
                        &numBytesEncrypted)
            }
        }
    }

3. let cryptStatus = clearData.withUnsafeMutableBytes {cryptBytes in
        data.withUnsafeBytes {dataBytes in
            keyData.withUnsafeBytes {keyBytes in
                CCCrypt(CCOperation(kCCDecrypt),
                        CCAlgorithm(kCCAlgorithmAES128),
                        options,
                        keyBytes,
                        keyLength,
                        dataBytes,
                        dataBytes + kCCBlockSizeAES128,
                        clearLength,
                        cryptBytes,
                        clearLength,
                        &numBytesDecrypted)
            }
        }
    }
Oleshko
  • 2,923
  • 4
  • 17
  • 25
  • For the first one see https://stackoverflow.com/q/39820602/1187415 – Martin R Mar 22 '20 at 19:51
  • @MartinR As I mentioned, I tried to resolve these warnings (including the one you posted a link), but without success. – Oleshko Mar 23 '20 at 06:41
  • Maybe [this helps](https://stackoverflow.com/questions/55484384/swift-5-kccdecrypt-commoncrypto-failing-to-decrypt) – vadian Mar 25 '20 at 08:51

2 Answers2

3

As @Enricoza points out, the now-deprecated parameter given to the withUnsafeMutableBytes closure is of type UnsafeMutablePointer, and the new one is UnsafeMutableRawBufferPointer. In order to get the UnsafeMutablePointer or UnsafeMutableRawPointer that CommonCrypto can accept, then you need to get the .baseAddress optional property on the *Bytes objects you get from your closures.

Here's a library that uses the new closure parameters: https://github.com/backslash-f/aescryptable/blob/master/Sources/AESCryptable/AESCryptable.swift

You'll note that the main difference is that it unwraps the baseAddresses in a guard clause, throws an error if any fail, then uses those baseAddresses in place of the old parameters to CommonCrypto functions.

Brock Batsell
  • 5,786
  • 1
  • 25
  • 27
2

If you command+click on your method's call you'll see that you are using a deprecated method that unfortunately has the same name of the new method, but has different param types:

    /// Mutate the bytes in the data.
    ///
    /// This function assumes that you are mutating the contents.
    /// - warning: The byte pointer argument should not be stored and used outside of the lifetime of the call to the closure.
    @available(swift, deprecated: 5, message: "use `withUnsafeMutableBytes<R>(_: (UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R` instead")
    public mutating func withUnsafeMutableBytes<ResultType, ContentType>(_ body: (UnsafeMutablePointer<ContentType>) throws -> ResultType) rethrows -> ResultType

    @inlinable public mutating func withUnsafeMutableBytes<ResultType>(_ body: (UnsafeMutableRawBufferPointer) throws -> ResultType) rethrows -> ResultType

To avoid the warning you should use the second method and (of course) change the implementation of the closure (since it now has another type of parameter).

To enforce the calling of the non-deprecated method you could simply force the type of the parameter in the closure like this:

let status = cryptData.withUnsafeMutableBytes { (ivBufferPointer: UnsafeMutableRawBufferPointer) in
        // Do your logic here with the UnsafeMutableRawBufferPointer
    }
Enricoza
  • 1,101
  • 6
  • 18
  • "change the implementation of the closure" - that's the problem, I don't know how to change that properly – Oleshko Mar 26 '20 at 07:20
  • @Oleshko maybe you should mention that in your question, so people know what your problem is and don't have to guess with some random answer you already know about... Also you could post a simple, complete, usage of what you wanted to do (in a replicable piece of code) and ask how to change it with the new undeprecated, method... (instead of posting 3 incomplete and non-minimal examples). (btw I'm just trying to help you improve your question so you're more likely to receive a good answer, not trying to be rude) – Enricoza Mar 26 '20 at 10:09
  • As I mentioned "I tried on my own to resolves these warnings, but with no luck.". I don't know what add more. Usually, when you have warning, you want that warning to be resolved and code must function the same as before, as for me it's obvious – Oleshko Mar 26 '20 at 12:20
  • @Oleshko you could improve your question.You want for some1 to fix your code, but you didn't even mention what your code actually does. If you ask for problems with warnings, I can point to the problem that caused them. If you want to know how to do something, than you should: first say what you want to do (so some1 doesn't have to guess it based on some random code), than post a simple, runnable, piece of code such that someone can take it and paste it to try on their own. That is if you want help. Feel free to keep asking vague questions if you don't want to maximize the chance to be helped. – Enricoza Mar 26 '20 at 14:21
  • This code is pretty self-explainable. These pieces of code are from encrypting/decrypting methods. And variables are all pretty self-explainable: cryptData - data to crypt keyData - data representation of a key. I don't know how to make it more obvious – Oleshko Mar 26 '20 at 22:30
  • @Oleshko I wish you good luck. While you wait for someone to answer you might wanna take a look at this: https://stackoverflow.com/help/how-to-ask – Enricoza Mar 27 '20 at 08:21
  • 1
    Thanks for the answer! Also In hope that this will help others, here we have a link that shows how to extract from UnsafeMutableRawBufferPointer and UnsafeRawBufferPointer. https://developer.apple.com/forums/thread/115227?answerId=355132022#355132022 – Antonio Carlos May 27 '21 at 14:36