繁体   English   中英

加密 js r.randomBytes 不是 function

[英]Crypto js r.randomBytes is not a function

尝试使用 ionic 设置 auth0 时遇到问题。

但是,我认为它与它无关,它更像是一个加密 js/config 问题。

这是我得到的错误:

main.7c4444f936ddaf5620f8.bundle.js:formatted:15919 ERROR Error:

Uncaught (in promise): TypeError: r.randomBytes is not a function

TypeError: r.randomBytes is not a function at n.generateProofKey (main.7c4444f936ddaf5620f8.bundle.js:formatted:844)

下面是我的加密 js 文件:

import * as crypto from 'crypto-js';

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

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

exports.generateProofKey = function generateProofKey() {
  var codeVerifier = base64UrlSafeEncode(crypto.randomBytes(32)); 
  var codeChallenge = base64UrlSafeEncode(sha256(codeVerifier)); 
  return { codeVerifier: codeVerifier, codeChallenge: codeChallenge };
};

exports.generateState = function generateState() {
  return base64UrlSafeEncode(crypto.randomBytes(32));
}

编辑:现在用 crypto.lib.WordArray.random(32) 说

c 不是 function

p.prototype.authorize = function(t, n) {
            if (!n || "function" != typeof n)
                throw new Error("callback not specified or is not a function");
            var e = this;
            i(function(l, i) {
                if (l)
                    return n(l);
                var o = c()
                  , a = e.client
                  , f = e.redirectUri
                  , p = t.state || h(

Cordova 授权0

callbackFromNative: function (callbackId, isSuccess, status, args, keepCallback) {
        try {
            var callback = cordova.callbacks[callbackId];
            if (callback) {
                if (isSuccess && status === cordova.callbackStatus.OK) {
                    callback.success && callback.success.apply(null, args);
                } else if (!isSuccess) {
                    callback.fail && callback.fail.apply(null, args);
                }
                /*
                else
                    Note, this case is intentionally not caught.
                    this can happen if isSuccess is true, but callbackStatus is NO_RESULT
                    which is used to remove a callback from the list without calling the callbacks
                    typically keepCallback is false in this case
                */
                // Clear callback if not expecting any more results
                if (!keepCallback) {
                    delete cordova.callbacks[callbackId];
                }
            }
        } catch (err) {
            var msg = 'Error in ' + (isSuccess ? 'Success' : 'Error') + ' callbackId: ' + callbackId + ' : ' + err;
            console && console.log && console.log(msg);
            cordova.fireWindowEvent('cordovacallbackerror', { 'message': msg });
            throw err;
        }

在这个网站上看到:

 // Creates a word array filled with random bytes. // @param {number} nBytes The number of random bytes to generate. var wordArray = CryptoJS.lib.WordArray.random(16);

所以,我认为你应该使用crypto.lib.WordArray.random(32)来代替。

您可以在这里使用:

import { nanoid } from "nanoid";
const token = nanoid(64); //instead of crypto.randomBytes(64).toString('hex')

这个问题似乎是重复的。 我在这里写得更广泛: https://stackoverflow.com/a/71106157/828184

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM