Last active
February 21, 2023 11:29
-
-
Save KenjiOhtsuka/766e756ea8988267c55ebd72201e3f73 to your computer and use it in GitHub Desktop.
Code Challenge in PKCE
This file contains 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
// This code works on the Web Browser, because it uses Web Crypto API. | |
const codeVerifierLength = 43; | |
let codeVerifier = ''; | |
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~'; | |
for (let i = 0; i < codeVerifierLength; i++) { | |
codeVerifier += characters.charAt(Math.floor(Math.random() * characters.length)); | |
} | |
async function generateCodeChallenge(codeVerifier) { | |
let digest = await crypto.subtle.digest( | |
"SHA-256", new TextEncoder().encode(codeVerifier) | |
); | |
return btoa(String.fromCharCode(...new Uint8Array(digest))). | |
replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_'); | |
} | |
const codeChallenge = await generateCodeChallenge(codeVerifier); | |
console.log(`Code Verifier : ${codeVerifier}`); | |
console.log(`Code Challenge: ${codeChallenge}`); |
This file contains 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
open System | |
open System.Linq | |
let codeVerifierLength = 43 | |
let random = Random() | |
let chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~" | |
let codeVerifier = | |
Enumerable.Repeat(chars, codeVerifierLength). | |
Select(fun s -> s.[random.Next(s.Length)]) |> | |
String.Concat | |
let codeChallenge = | |
codeVerifier |> | |
System.Text.Encoding.UTF8.GetBytes |> | |
System.Security.Cryptography.SHA256.Create().ComputeHash |> | |
System.Convert.ToBase64String |> | |
fun x -> x.Replace("=", "").Replace("+", "-").Replace("/", "_") | |
printfn "Code Verifier : %s" codeVerifier | |
printfn "Code Challenge: %s" codeChallenge |
This file contains 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
import base64 | |
import hashlib | |
import random | |
import string | |
code_verifier_length = 43 | |
code_verifier = ''.join(random.choice(string.ascii_letters + string.digits + '-._~')\ | |
for i in range(code_verifier_length)) | |
code_challenge = base64.urlsafe_b64encode(hashlib.sha256(code_verifier.encode()).digest()).\ | |
decode().replace("=", "").replace("+", "-").replace("/", "_") | |
print("Code Verifier :", code_verifier) | |
print("Code Challenge:", code_challenge) |
This file contains 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
require 'digest' | |
require 'base64' | |
code_verifier_length = 43 | |
o = ([('a'..'z'), ('A'..'Z'), ('0'..'9')].map(&:to_a) + %w[- _ . ~]).flatten | |
code_verifier = (0...code_verifier_length).map { o[rand(o.length)] }.join | |
code_challenge = Base64.urlsafe_encode64(Digest::SHA256.digest(code_verifier)). | |
delete('=').gsub('+', '-').gsub('/', '_') | |
puts "Code Verifier : #{code_verifier}" | |
puts "Code Challenge: #{code_challenge}" |
This file contains 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
CODE_VERIFIER_LENGTH=43 | |
CODE_VERIFIER=`cat /dev/urandom | LC_CTYPE=C tr -dc 'a-zA-Z0-9\-._~' | fold -w $CODE_VERIFIER_LENGTH | head -n 1` | |
CODE_CHALLENGE=`echo -n $CODE_VERIFIER | openssl dgst -sha256 -binary | openssl base64 -A | tr -d '=' | tr '/+' '_-'` | |
echo "Code Verifier :" $CODE_VERIFIER | |
echo "Code Challenge:" $CODE_CHALLENGE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment