wheresvic / mongoose-field-encryption

A simple symmetric encryption plugin for individual fields. Dependency free, only mongoose peer dependency.
MIT License
74 stars 32 forks source link

TypeError: saltGenerator is not a function #67

Closed Ayushk93 closed 2 years ago

Ayushk93 commented 3 years ago

Below is the code I'm trying for searching purpose but getting

const iv = saltGeneratorWrapper(saltGenerator(secret)); TypeError: saltGenerator is not a function

const fieldEncryption = require('mongoose-field-encryption') const encrypted = fieldEncryption.encrypt('some text', 'secret'); const decrypted = fieldEncryption.decrypt(encrypted, 'secret'); console.log('encrypted ', encrypted); console.log('decrypted ', decrypted);

Environment: Node.js v14.17.0 in Windows.

Please help.

wheresvic commented 3 years ago

Hi @Ayushk93

Sorry for the delay in getting back to you - somehow my notifications from github are landing up in Spam.

Anyways, what you want to do here is to pass the saltGenerator function. If you do not pass anything the default function is: const defaultSaltGenerator = (secret) => crypto.randomBytes(16);, so it is possible that you created your own function here.

We would need to look at more code to debug further,

obllivion commented 3 years ago

Maybe i can contribute - this happened to me too because i tried to use "encryptFieldsSync" - and then find did not work, from some other issues i saw here i concluded what is wrong: In readme.MD there is no example on how to use salt when generating schema, but that point is very important, because if you leave it out (like i did) - find will later use some different encryption - and the problems will occur - so all that is missing in order for all of this to work is to explicitly add salt, like so: saltGenerator: (secret) => secret.slice(0, 16) And then the searches will work. Hope it helps someone!

wheresvic commented 2 years ago

This is a good point and I will update the README regarding this!

wheresvic commented 2 years ago

Updated Readme, going to close this issue now!