以原生的方式生成JWT

我正在尝试在React Native中生成JWT。 react-native-jwt模块基于Node的jwt-simple 。 它使用react-native-crypto ,它是crypto-browserify一个部分克隆,有一些解决方法可以让crypto在web浏览器之外工作。

问题是randombytesreact-native-jwt依赖)找不到crypto

browser.js

 'use strict' function oldBrowser () { throw new Error('secure random number generation not supported by this browser\nuse chrome, FireFox or Internet Explorer 11') } var crypto = global.crypto || global.msCrypto // GETS SET TO 'UNDEFINED' if (crypto && crypto.getRandomValues) { module.exports = randomBytes } else { module.exports = oldBrowser } function randomBytes (size, cb) { // phantomjs needs to throw if (size > 65536) throw new Error('requested too many random bytes') // in case browserify isn't using the Uint8Array version var rawBytes = new global.Uint8Array(size) // This will not work in older browsers. // See https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues if (size > 0) { // getRandomValues fails on IE if size == 0 crypto.getRandomValues(rawBytes) } // phantomjs doesn't like a buffer being passed here var bytes = new Buffer(rawBytes.buffer) if (typeof cb === 'function') { return process.nextTick(function () { cb(null, bytes) }) } return bytes } 

注意:看起来encryption只在Android上可用,我需要在iOS上使用它。

    Interesting Posts