blob: e37a7695892490b43183ee0bd7e097253275252c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
import {NativeModules} from 'react-native'
const {AppSecureRandomModule} = NativeModules
import {toByteArray} from 'base64-js'
// @ts-ignore we dont have types for this -prf
import crypto from '../third-party/msrcrypto'
import '@zxing/text-encoding' // TextEncoder / TextDecoder
async function generateSecureRandom(bytes: number) {
return toByteArray(
await AppSecureRandomModule.generateSecureRandomAsBase64(bytes),
)
}
export const whenWebCrypto = new Promise(async (resolve, reject) => {
try {
const bytes = await generateSecureRandom(48)
crypto.initPrng(Array.from(bytes))
// @ts-ignore global.window exists -prf
if (!global.window.crypto) {
// @ts-ignore global.window exists -prf
global.window.crypto = crypto
}
resolve(true)
} catch (e: any) {
reject(e)
}
})
export const webcrypto = crypto
|