1

I have just started on a project and have come across a deprecated function. After googling around I've found several solutions but only for the first part of the error. My issue is i dont know how to deal with the nested parts that are also deprecated. The function is as follows:

private static func compute(data: Data, operation: Int) -> Data {
    let cryptLength = size_t(data.count + kCCBlockSizeAES128)
    var cryptData = Data(count:cryptLength)

    let keyLength = size_t(kCCKeySizeAES128)
    let options = CCOptions(kCCOptionPKCS7Padding)

    var numBytesEncrypted: size_t = 0

    let cryptStatus = cryptData.withUnsafeMutableBytes { cryptBytes in  <--- Warning 1
        data.withUnsafeBytes { dataBytes in  <--- Warning 2
            Data(Encoded.iv).withUnsafeBytes { ivBytes in  <--- Warning 3
                Data(Encoded.key).withUnsafeBytes { keyBytes in  <--- Warning 4
                    CCCrypt(CCOperation(operation),
                            CCAlgorithm(kCCAlgorithmAES),
                            options,
                            keyBytes, keyLength,
                            ivBytes,
                            dataBytes, data.count,
                            cryptBytes, cryptLength,
                            &numBytesEncrypted)
                }
            }
        }
    }

    guard case UInt32(cryptStatus) == UInt32(kCCSuccess) = true else {
        print("Something went wrong: \(cryptStatus)")
        return cryptData
    }

    cryptData.removeSubrange(numBytesEncrypted..<cryptData.count)

    return cryptData
}

Warning 1: 'withUnsafeMutableBytes' is deprecated: use `withUnsafeMutableBytes<R>(_: (UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R` instead

Warning 2,3 + 4: 'withUnsafeBytes' is deprecated: use `withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R` instead

I've tried doing cryptData.withUnsafeMutableBytes { (cryptBytes: UnsafeMutablePointer) in or adding try around each but either get new errors or the warning stays.

Can anyone help?

Wazza
  • 1,725
  • 2
  • 17
  • 49
  • Unrelated, but `guard case UInt32(cryptStatus) == UInt32(kCCSuccess) = true` is a really complicated way of saying `guard cryptStatus == kCCSuccess` lol – Alexander Aug 10 '19 at 02:05
  • I think the error is trying to steer you towards you using the free functions, instead of the instance methods on `Data`. – Alexander Aug 10 '19 at 02:10
  • I use CocoaSecurity for AES 255 CBC. try it – Masoud Roosta Aug 10 '19 at 08:05
  • i cant add more dependencies, the project takes too long to build already – Wazza Aug 10 '19 at 08:57
  • @Alexander what do you mean, `free functions`? – Wazza Aug 10 '19 at 08:57
  • @Wazza Oops, that's the python terminology. In Swift, they're generally called "global" functions, i.e. functions that aren't within a `struct`/`class`/`enum` namespace. In particular, see the "pointers to values" section of https://developer.apple.com/documentation/swift/swift_standard_library/c_interoperability – Alexander Aug 10 '19 at 15:33
  • Here is the way how I fixed it: https://stackoverflow.com/a/58008707/1151916 – Ramis Sep 19 '19 at 10:17

0 Answers0