【问题标题】:Crypto js r.randomBytes is not a functionCrypto js r.randomBytes 不是函数
【发布时间】:2018-09-07 04:56:17
【问题描述】:

我在尝试使用 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 不是函数

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(

科尔多瓦 Auth0

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;
        }

【问题讨论】:

  • @RobC 已经是了,我发的时候好像修改了

标签: javascript angular webpack auth0 cryptojs


【解决方案1】:

在这个site看到:

// 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)

【讨论】:

  • @gbgbg 现在不是同一个问题了,不是关于加密 JS。
  • @gbgbg 我不习惯使用导出。但例如,您可以使用静态函数创建助手。看这里:stackoverflow.com/a/40547841/5049472
  • 感谢@Powkachu ,现在似乎是另一个问题 c 不是函数 p.prototype.authorize = function(t, n) { if (!n || "function" != typeof n ) throw new Error("回调未指定或不是函数");变量 e = 这个; i(function(l, i) { if (l) return n(l); var o = c() , a = e.client , f = e.redirectUri , p = t.state || h();
【解决方案2】:

这里你可以改用:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-02
    • 2016-09-08
    • 1970-01-01
    • 2021-09-22
    • 2016-06-19
    • 2021-09-22
    • 2018-05-11
    • 2021-03-14
    相关资源
    最近更新 更多