// SHA function sha256(plain: any) { // returns promise ArrayBuffer const encoder = new TextEncoder(); const data = encoder.encode(plain); return window.crypto.subtle.digest('SHA-256', data); } function base64urlencode(a: any) { // Convert the ArrayBuffer to string using Uint8 array. // btoa takes chars from 0-255 and base64 encodes. // Then convert the base64 encoded to base64url encoded. // (replace + with -, replace / with _, trim trailing =) // @ts-ignore return btoa(String.fromCharCode.apply(null, new Uint8Array(a))) .replace(/\+/g, '-') .replace(/\//g, '_') .replace(/=+$/, ''); } export async function pkceChallengeFromVerifier(v: any) { const hashed = await sha256(v); return base64urlencode(hashed); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter