vite+vue3使用aes-交叉报告: TypeError: crypto.createCipheriv不是函数版本:"vue":"^3.2.23","vite":"^2.6.4",“aes-交叉”:"^1.0.9",
发布于 2021-12-25 12:02:05
要让crypto在浏览器中工作,请按照下面的说明操作
yarn add stream-browserify browserify-zlib events process util buffer
vite.config.jsimport { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
process: "process/browser",
stream: "stream-browserify",
zlib: "browserify-zlib",
util: 'util'
}
}
})index.html<!-- [...] -->
<div id="root"></div>
<!-- node // crypto -->
<script>window.global = window;</script>
<script type="module">
import { Buffer } from "buffer/"; // <-- no typo here ("/")
import process from "process";
import EventEmitter from "events";
window.Buffer = Buffer;
window.process = process;
window.EventEmitter = EventEmitter;
</script>
<!-- [...] -->App.vue,并进行测试在App.vue中
import crypto from 'crypto-browserify'
import { Buffer } from 'buffer/' // <-- no typo here ("/")
// test crypto
function crypt() {
// Node.js program to demonstrate the
// crypto.createCipheriv() method
const iv = crypto.randomBytes(16);
const key = crypto.pbkdf2Sync("foobar", "salt", 1000, 32, 'sha512');
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
var text = crypto.randomBytes(200)
var ourCipherText = Buffer.concat([cipher.update(text), cipher.final()])
console.log("ciphered text:", ourCipherText.toString('hex'))
return ourCipherText.toString('hex');
}
[...]结果✅

https://stackoverflow.com/questions/70478728
复制相似问题