UNPKG

@react-native-oh-tpl/react-native-get-random-values

Version:

A small implementation of `crypto.getRandomValues` for React Native. This is useful to polyfill for libraries like [uuid](https://www.npmjs.com/package/uuid) that depend on it.

34 lines (22 loc) 900 B
import { TurboModule } from '@rnoh/react-native-openharmony/ts'; import { TM } from '@rnoh/react-native-openharmony/generated/ts'; import window from '@ohos.window'; import { util } from '@kit.ArkTS'; export class RNGetRandomValuesTurboModule extends TurboModule implements TM.GetRandomValuesNativeModule.Spec { windowClass:window.Window| undefined = undefined; isKeepScreenOn: boolean = true; unisKeepScreenOn: boolean = false; constructor(ctx) { super(ctx); } getRandomBase64(byteLength:number){ const randomBytes = new Uint8Array(byteLength) for(var i = 0 ; i<byteLength;i++){ randomBytes[i] = Math.floor(Math.random()*256) } const that = new util.Base64Helper() const base64String = that.encodeToStringSync(randomBytes); //将随机字节数组转换为Base64编码的字符串 return base64String } }