Olá,
Estou tentando fazer uma api que realiza a criptografia de uma string. Porém, quando tento utilizar var crypto = require('crypto')
, dá o erro abaixo.
Mais abaixo, inclui o meu código.
Obrigada.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>ReferenceError: require is not defined<br> at criptografarSenha (file:///C:/git/CriptografiaOrbitall/src/app.js:34:18)<br> at file:///C:/git/CriptografiaOrbitall/src/app.js:22:5<br> at Layer.handle [as handle_request] (C:\git\CriptografiaOrbitall\node_modules\express\lib\router\layer.js:95:5)<br> at next (C:\git\CriptografiaOrbitall\node_modules\express\lib\router\route.js:137:13)<br> at Route.dispatch (C:\git\CriptografiaOrbitall\node_modules\express\lib\router\route.js:112:3)<br> at Layer.handle [as handle_request] (C:\git\CriptografiaOrbitall\node_modules\express\lib\router\layer.js:95:5)<br> at C:\git\CriptografiaOrbitall\node_modules\express\lib\router\index.js:281:22<br> at param (C:\git\CriptografiaOrbitall\node_modules\express\lib\router\index.js:360:14)<br> at param (C:\git\CriptografiaOrbitall\node_modules\express\lib\router\index.js:371:14)<br> at Function.process_params (C:\git\CriptografiaOrbitall\node_modules\express\lib\router\index.js:416:3)</pre>
</body>
</html>
import express from "express";
const app = express();
app.use(express.json());
app.get('/criptografar/:senha', (req, res) => {
criptografarSenha(req.params.senha)
})
function criptografarSenha(senha) {
var crypto = require('crypto')
// Chave pública
const publicKey = `AQUI FICARÁ MINHA CHAVE PUBLICA`
const plaintext = senha
const encrypt = (publicKey, plaintext) => {
const buffer = Buffer.from(plaintext, 'utf8')
const encrypted = crypto.publicEncrypt(publicKey, buffer)
return encrypted.toString('base64')
}
const hash = encrypt(publicKey, plaintext)
console.log(res.json(hash));
}
export default app;