2

I have a code that is generating challenge string.

    private func codeChallenge(for verifier: String) -> String {
       guard let data = verifier.data(using: .utf8) else { fatalError() }
       var buffer = [UInt8](repeating: 0,  count: Int(CC_SHA256_DIGEST_LENGTH))
       data.withUnsafeBytes {
          _ = CC_SHA256($0, CC_LONG(data.count), &buffer)
       }
       let hash = Data(buffer)
       return hash.base64EncodedString()
        .replacingOccurrences(of: "+", with: "-")
        .replacingOccurrences(of: "/", with: "_")
        .replacingOccurrences(of: "=", with: "")
        .trimmingCharacters(in: .whitespaces)
    }

It works as expected, however it generates warning:

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

I am not sure if I am crazy but warning is again referencing deprecated method as a solution. Am I missing something?

matt
  • 515,959
  • 87
  • 875
  • 1,141
Juraj Antas
  • 3,059
  • 27
  • 37
  • Does this answer your question? [Swift 5.0: 'withUnsafeBytes' is deprecated: use \`withUnsafeBytes(...)](https://stackoverflow.com/questions/55378409/swift-5-0-withunsafebytes-is-deprecated-use-withunsafebytesr) – Itai Ferber Mar 26 '22 at 16:47
  • unfortunately no. But I think I found a solution. Only thing that was needed was to realize that I need to convert UnsafeRawBufferPointer to UnsafeRawPointer. So it might be that all is needed is adding $0.baseAddress to the SHA first param and it is done. – Juraj Antas Mar 26 '22 at 19:01
  • Do not add a solution in the Question field. A solution is an Answer, not a Question. – matt Mar 26 '22 at 19:34
  • @JurajAntas Please think about how you asked the question. You did _not_ ask how to adjust your code. You asked whether the replacement method in the deprecation warning was the same as the deprecated method. It isn't. How to _call_ the replacement method is a different matter. – matt Mar 26 '22 at 19:38
  • Got it. Even if I didn't explicitly ask for help with updating the code to call right non deprecated function, it does have value for reader to know how to adjust the code to call the right one. Do you mind to add that to your answer? – Juraj Antas Mar 27 '22 at 20:29

1 Answers1

2

Am I missing something?

Yes. The one that is deprecated is

func withUnsafeBytes<ResultType, ContentType>(_ body: (UnsafePointer<ContentType>) throws -> ResultType) rethrows -> ResultType

The replacement is

func withUnsafeBytes<ResultType>(_ body: (UnsafeRawBufferPointer) throws -> ResultType) rethrows -> ResultType

Those two methods may both have the phrase "withUnsafeBytes" in them, but that is basically irrelevant; they are completely different methods with completely different signatures.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Yes, you are right, new method is slightly different, but not the same. Problem is how to convert from UnsafeRawBufferPointer to UnsafeRawPointer that is needed for SHA function? – Juraj Antas Mar 26 '22 at 18:55
  • 2
    maybe like this: data.withUnsafeBytes { _ = CC_SHA256($0.baseAddress, CC_LONG(data.count), &buffer) } – Juraj Antas Mar 26 '22 at 18:58