rono23

PKCEのcode_challenge生成

code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
https://tools.ietf.org/html/rfc7636

Swift

import CryptoKit

let codeVerifier = "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
let data = Data(codeVerifier.utf8)
let hash = SHA256.hash(data: data)
let codeChallenge = Data(hash).base64EncodedString()
    .replacingOccurrences(of: "=", with: "")
    .replacingOccurrences(of: "+", with: "-")
    .replacingOccurrences(of: "/", with: "_")

Ruby

require 'base64'
require 'openssl'

code_verifier = "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
code_challenge = Base64.urlsafe_encode64(OpenSSL::Digest::SHA256.digest(code_verifier), padding: false)

最初、ハッシュ値を文字列で扱って少しハマったけど CommonCrypto を使わずにできてよかった 🎉

Post a comment on GitHub