You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to use crypto library but facing various issues when importing crypto and building vue with vite.
Issue 1: const crypto = require('crypto') Not working
constcrypto=require('crypto')constENCRYPTION_KEY=import.meta.VITE_REQUEST_ENCRYPT_KEY;// Must be 256 bits (32 characters)constIV_LENGTH=16;// For AES, this is always 16exportfunctionencrypt(text){letiv=crypto.randomBytes(IV_LENGTH);letcipher=crypto.createCipheriv('aes-256-cbc',Buffer.from(ENCRYPTION_KEY),iv);letencrypted=cipher.update(text);encrypted=Buffer.concat([encrypted,cipher.final()]);returniv.toString('hex')+':'+encrypted.toString('hex');}exportfunctiondecrypt(text){lettextParts=text.split(':');letiv=Buffer.from(textParts.shift(),'hex');letencryptedText=Buffer.from(textParts.join(':'),'hex');letdecipher=crypto.createDecipheriv('aes-256-cbc',Buffer.from(ENCRYPTION_KEY),iv);letdecrypted=decipher.update(encryptedText);decrypted=Buffer.concat([decrypted,decipher.final()]);returndecrypted.toString();}
This piece of code generates: Uncaught ReferenceError: require is not defined
I'm trying to use crypto library but facing various issues when importing crypto and building vue with vite.
Issue 1:
const crypto = require('crypto')
Not workingThis piece of code generates:
Uncaught ReferenceError: require is not defined
Issue 2:
import crypto from 'crypto'
Not workingReplacing
with
Generates following errors:
When vite.config.js
Error:
[vite] Failed to resolve module import "crypto/index.js". (imported by /src/js/encrypt.js)
Also due to above errors I can't make it work with crypto-js
What would be the possible solution to this issue?
The text was updated successfully, but these errors were encountered: