self::MAX_VERIFIER_LENGTH ) { return false; } return 1 === preg_match( self::CHARSET_PATTERN, $verifier ); } /** * Whether $verifier's S256 transform matches $challenge, in constant time. * * @param string $verifier Presented code_verifier. * @param string $challenge Stored code_challenge. * @return bool */ public static function matches( string $verifier, string $challenge ): bool { if ( ! self::isValidVerifierFormat( $verifier ) || '' === $challenge ) { return false; } return hash_equals( $challenge, self::transform( $verifier ) ); } /** * The S256 transform: BASE64URL-ENCODE(SHA256(ASCII(code_verifier))), no padding. * * @param string $verifier code_verifier. * @return string */ public static function transform( string $verifier ): string { return rtrim( strtr( base64_encode( hash( 'sha256', $verifier, true ) ), '+/', '-_' ), '=' ); } }