'withUnsafeBytes' is deprecated: use

I'm getting the warning: 'withUnsafeBytes' is deprecated: use withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R instead

for the following code:

func keyForPassword(_ inPassword:String, salt:Data, keySettings:SOSCryptorKeyDerivationSettings) -> Data {

	let size = Int(keySettings.keySize)
    var derivedKey = Data(count: size)
    let passwordLength = size_t(inPassword.lengthOfBytes(using: String.Encoding.utf8))
    
    derivedKey.withUnsafeMutableBytes { (ptr:UnsafeMutablePointer<UInt8>) -> Void in
		
		_ = CCKeyDerivationPBKDF(CCPBKDFAlgorithm(keySettings.PBKDFAlgorithm), NSString(string: inPassword).utf8String, size_t(passwordLength), (salt as NSData).bytes.bindMemory(to: UInt8.self, capacity: salt.count), size_t(salt.count), CCPseudoRandomAlgorithm(keySettings.PRF), uint(keySettings.rounds), ptr, size_t(size));
    }
    
    return derivedKey
}

I cannot figure out how to resolve this issue.

Change it to:

derivedKey.withUnsafeMutableBytes { (ptr: UnsafeMutableRawBufferPointer) -> Void in
  CCKeyDerivationPBKDF(
    CCPBKDFAlgorithm(keySettings.PBKDFAlgorithm), 
    NSString(string: inPassword).utf8String, 
    size_t(passwordLength), 
    (salt as NSData).bytes.bindMemory(to: UInt8.self, capacity: salt.count), 
    size_t(salt.count), 
    CCPseudoRandomAlgorithm(keySettings.PRF), 
    uint(keySettings.rounds), 
    ptr.baseAddress?.assumingMemoryBound(to: UInt8.self), 
    size_t(size)
  )
}

The code example worked as you new! Thank you. Never would have figured that out from the documentation.

Much appreciated.

Joe

Terms of Service

Privacy Policy

Cookie Policy