簡體   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