Created
January 6, 2021 12:42
-
-
Save lukeredpath/eb9fe9fa1c59b5ab8a2057a366332c34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Data { | |
// Returns a base64 encoded string, replacing reserved characters | |
// as per https://tools.ietf.org/html/rfc7636#section-4.2 | |
func pkce_base64EncodedString() -> String { | |
base64EncodedString() | |
.replacingOccurrences(of: "+", with: "-") | |
.replacingOccurrences(of: "/", with: "_") | |
.replacingOccurrences(of: "=", with: "") | |
.trimmingCharacters(in: .whitespaces) | |
} | |
} | |
func generateCodeChallenge(from verifier: String) -> String { | |
let data = verifier.data(using: .utf8)! | |
let hash = SHA256.hash(data: data) | |
return Data(hash).pkce_base64EncodedString() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment