1

I'm changing the code from the previous version of Swift to Swift5. And there is a warning message that this code is not available. I'd like to change this code, but I don't know how.

warning code

 func pbkdf2(hash: CCPBKDFAlgorithm, password: String, salt: Data, keyByteCount: Int, round: Int) -> Data? {
        let passwordData = password.data(using: .utf8)!
        let derivedKeyData = Data(count: keyByteCount)
        var localVariables = derivedKeyData
        let derivationStatus = localVariables.withUnsafeMutableBytes { derivedKeyBytes  in
            salt.withUnsafeBytes { saltBytes in
                CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2),
                                     password, passwordData.count, saltBytes, salt.count,
                                     hash, UInt32(round),
                                     derivedKeyBytes, derivedKeyData.count)
            }
        }

        if (derivationStatus != 0) {
            Log.Error("\(derivationStatus)")
            return nil;
        }

        return localVariables
    }

warning Message:

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

'withUnsafeBytes' is deprecated: use withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R instead

How do I change this code to delete warning message?

I tried many things, but the error changed.

func pbkdf2(hash: CCPBKDFAlgorithm, password: String, salt: Data, keyByteCount: Int, round: Int) -> Data? {
  let passwordData = password.data(using: .utf8)!
  let derivedKeyData = Data(count: keyByteCount)
  var localVariables = derivedKeyData
  let derivationStatus = localVariables.withUnsafeMutableBytes { derivedKeyBytes  in
     let Mutable: UnsafeMutableRawPointer? = derivedKeyBytes.baseAddress
     salt.withUnsafeBytes { saltBytes in
           let raw: UnsafeRawPointer? = saltBytes.baseAddress
              CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2),
                                     password, passwordData.count, raw?.assumingMemoryBound(to: UInt8.self), salt.count,
                                     hash, UInt32(round),
                                     Mutable?.assumingMemoryBound(to: UInt8.self) , derivedKeyData.count)
            }
        }

        if (derivationStatus != 0) {
            Log.Error("\(derivationStatus)")
            return nil;
        }

        return localVariables
    }

Error Message:

Binary operator '!=' cannot be applied to operands of type '()' and 'Int'

Warning Message:

Constant 'derivationStatus' inferred to have type '()', which may be unexpected

Am I right to change it? I think I need to correct the comparison, how am I supposed to correct it?

iosbegindevel
  • 307
  • 5
  • 20
  • Does this https://stackoverflow.com/a/55499502/1187415 help?  – And how is the question related to [r]? – Martin R Oct 18 '19 at 06:46
  • I added it because the alert had an R. And, I've looked at your link, but I don't know how to change my code... I don't know much about swift. Could you help me @MartinR ?? – iosbegindevel Oct 18 '19 at 06:55
  • The [tag:r] tag is for questions about [R](https://www.r-project.org), a “a free software environment for statistical computing and graphics.” – Martin R Oct 18 '19 at 06:57
  • Oh, @MartinR I didn't know if it was different from this. I'm sorry. – iosbegindevel Oct 18 '19 at 07:00
  • @MartinR How can I remove my warning? I need your help. – iosbegindevel Oct 18 '19 at 07:06
  • https://stackoverflow.com/a/58008707/1187415 should answer your question: Replace `saltBytes` by `saltBytes.bindMemory(to: UInt8.self).baseAddress`, and similarly for `derivedKeyBytes`. – Martin R Oct 18 '19 at 08:12
  • @MartinR Thank you. The warning has been lifted. I will fill out my changed code in the answer. Please check if it's correct. – iosbegindevel Oct 18 '19 at 08:20
  • @MartinR I posted my answer. Please see if I changed it correctly. Thank you again for your help. – iosbegindevel Oct 18 '19 at 08:24

1 Answers1

0

I solved this problem with the help of @MartinR. This is the link that @MartinR suggested as an answer.

Replace saltBytes by saltBytes.bindMemory(to: UInt8.self).baseAddress, and similarly for derivedKeyBytes.

Successful code with all warnings missing

func pbkdf2(hash: CCPBKDFAlgorithm, password: String, salt: Data, keyByteCount: Int, round: Int) -> Data? {
    let passwordData = password.data(using: .utf8)!
    let derivedKeyData = Data(count: keyByteCount)
    var localVariables = derivedKeyData
    let derivationStatus = localVariables.withUnsafeMutableBytes { derivedKeyBytes  in
      salt.withUnsafeBytes { saltBytes in

       CCKeyDerivationPBKDF(CCPBKDFAlgorithm(kCCPBKDF2),
                          password, passwordData.count, saltBytes.bindMemory(to: UInt8.self).baseAddress, salt.count,
                          hash, UInt32(round),
                          derivedKeyBytes.bindMemory(to: UInt8.self).baseAddress , derivedKeyData.count)
            }
        }

        if (derivationStatus != 0) {
                 Log.Error("\(derivationStatus)")
                 return nil;
        }

        return localVariables
    }
iosbegindevel
  • 307
  • 5
  • 20
  • @MartinR um.... I have one more question. UInt8 ranges from 0 to 255. I think this is a very small range. Is it less likely that UInt8 has duplicate values? – iosbegindevel Oct 18 '19 at 08:30