Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Created January 6, 2021 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukeredpath/eb9fe9fa1c59b5ab8a2057a366332c34 to your computer and use it in GitHub Desktop.
Save lukeredpath/eb9fe9fa1c59b5ab8a2057a366332c34 to your computer and use it in GitHub Desktop.
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