最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

react native - Generate the PKCE code challenge and verifier - Stack Overflow

programmeradmin3浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论