Is it have any possible option to encrypt user email in nodejs (for security issue) and save it crypted in database(postgress). After that i use email for login and need to find user email in db. I try to encrypt with crypto, but every time hash is diffrent and I can't check it in db.
import crypto from 'crypto'
import fs from 'fs'
import { encryptText } from "./encrypt.mjs";
// The `generateKeyPairSync` method accepts two arguments:
// 1. The type ok keys we want, which in this case is "rsa"
// 2. An object with the properties of the key
const { publicKey, privateKey } = crypto.generateKeyPairSync("rsa", {
// The standard secure default length for RSA keys is 2048 bits
modulusLength: 2048,
})
const plainText = "[email protected]";
const encryptedText1 = encryptText(plainText)
// encryptedText will be returned as Buffer
// in order to see it in more readble form, convert it to base64
console.log('encrypted text: 1 ', encryptedText1.toString('base64'))
import fs from 'fs'
import crypto from 'crypto'
export function encryptText (plainText) {
return crypto.publicEncrypt({
key: fs.readFileSync('public_key.pem', 'utf8'),
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: 'sha256'
},
// We convert the data string to a buffer`your text`
Buffer.from(plainText)
)
}