crypto.createCipheriv is not a function

邱秋 • 2024年07月15日 • 阅读:94 • vue javascript

就是一个aes 加密,大概是这样的

const crypto = require('crypto');

const secretKey = '.'
function encryptCredentials(username, password, total) {
  const combined = username + ':' + password + ':' + total;
  const cipher = crypto.createCipher('aes-256-ctr', secretKey);
  let encrypted = cipher.update(combined, 'utf8', 'hex');
  encrypted += cipher.final('hex');
  return encrypted;
}

报错.

crypto.createCipheriv is not a function

一顿猛操作 基本解决了一堆错误

// ...省略
configureWebpack: {
    resolve: {
      fallback: {
        path: require.resolve("path-browserify"),
        crypto: require.resolve("crypto-browserify"),
        buffer: require.resolve("buffer/"),
        stream: require.resolve("stream-browserify"),
        vm: require.resolve("vm-browserify"),
      },
    },
}

卧槽 接着又出现 process is undefined .

加上这个

const webpack = require('webpack');
configureWebpack: {
   resolve :{
        alias: {
            process: "process/browser"
        }
    },
   plugins: [
      new webpack.ProvidePlugin({
        process: 'process/browser',
        Buffer: ['buffer', 'Buffer']
      }),
    ]
}
//你还可以 把
const crypto = require('crypto');
//替换成
const crypto = require('crypto-browserify');

OK. 错误解决,

我,秦始皇,打钱!