admin管理员组

文章数量:1391937

I'm working on an OAuth2 implementation in a TypeScript project with React Native, and I need to generate the PKCE (Proof Key for Code Exchange) code challenge and the verifier. Here’s the snippet I have:

I assumed there’s a built-in crypto module in React Native, but when trying to use it, I encountered compilation issues.

Is there a built-in library or vanilla code I can get, in React Native for generating PKCE code challenge and verifier? Or am I forced to rely on third-party libraries like react-native-crypto?

import { randomBytes, createHash } from 'crypto';

function base64URLEncode(buffer: Buffer): string {
  return buffer.toString('base64')
    .replace(/\+/g, '-')
    .replace(/\//g, '_')
    .replace(/=/g, '');
}

function sha256(buffer: Buffer): Buffer {
  return createHash('sha256').update(buffer).digest();
}

const verifier: string = base64URLEncode(randomBytes(32));
const challenge: string = base64URLEncode(sha256(Buffer.from(verifier)));

There's no direct error in the code, but the nested imports stream from the above import.

本文标签: react nativeGenerate the PKCE code challenge and verifierStack Overflow