-2

I am getting deprecated warning for below code,

_ = data.withUnsafeBytes {
    _ = CC_SHA256($0, CC_LONG(data.count), &digest)
}

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

How can I deal with this?

Update

As suggested by Martin, I used below code for Swift-5,

func sha256(data : Data) -> Data {
    var hash = [UInt8](repeating: 0,  count: Int(CC_SHA256_DIGEST_LENGTH))
    data.withUnsafeBytes {
        _ = CC_SHA256($0.baseAddress, CC_LONG(data.count), &hash)
    }
    return Data(hash)
}

That's even giving comilers error,

Value of type 'UnsafePointer<_>' has no member 'baseAddress'

Update2

Martins solution works fine in Xcode 11.3.1. It does not work in Xcode 11 though.

Community
  • 1
  • 1
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256

1 Answers1

6

Martin's solutions worked fine for Xcode 11.3.1+,

func sha256(data : Data) -> Data {
    var hash = [UInt8](repeating: 0,  count: Int(CC_SHA256_DIGEST_LENGTH))
    data.withUnsafeBytes {
        _ = CC_SHA256($0.baseAddress, CC_LONG(data.count), &hash)
    }
    return Data(hash)
}
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256