Reputation: 65
I'm trying to encrypt my data in mongodb. I'm using mongoose-encryption plugin, but i have an error like this :
"throw new Error('must provide either options.secret or both options.encryptionKey and options.signingKey');"
here's my code:
var UserSchema = new mongoose.Schema({
profile: ProfileSchema,
timeStamp: Date,
created: Date,
email: {
type: String,
sparse: true
},
username: {
type: String,
},
password: {
type: String,
}
})
var encKey = process.env.SOME_32BYTE_BASE64_STRING
var sigKey = process.env.SOME_64BYTE_BASE64_STRING
UserSchema.plugin(encrypt, { encryptionKey: encKey, signingKey: sigKey , encryptedFields: ['email']})
so, what i'm missing in my code? i have no idea with this error. because i just following steps in here but failed. i'm new in node and mongoose. please help me... thanks anyway...
Upvotes: 2
Views: 7143
Reputation: 1
In My case i was using ES6 version meaning when i was using import statement to import module which i think is not compatible with This mongoose-encryption when i turned everything to a format like
require()
everything just worked fine . And one more thing use it at the top of your .js file.
Upvotes: 0
Reputation: 1
First you delete last 3 line then go with this
const userSchema = new mongoose.Schema({
email: String,
password: String,
});
const secret = "ILoveMyCountryVeryMuch";
userSchema.plugin(encrypt, { secret: secret, encryptedFields: ['password'] });
const User = mongoose. Model("User", userSchema);
You can encrypt by using this code.
Upvotes: 0
Reputation: 1
In your .env
file, ensure that the values of SOME_32BYTE_BASE64_STRING
and SOME_64BYTE_BASE64_STRING
are enclosed inside a ""
For example:
SOME_32BYTE_BASE64_STRING="ThisIsMyLittlesecret"
Upvotes: 0
Reputation: 11
You can use a variable called secret = "set it to any string you want"
then you can add {secret:secret}
to your UserSchema.plugin();
You can find this in the mongoose-encryption
package docs
Upvotes: 0
Reputation: 111
check your required name variables: like: It should be exactly like this:
const encrypt = require("mongoose-encryption");
So that it matches with the name:
userSchema.plugin(encrypt, { secret: secret });
Your const encrypt name should match userSchema.plugin(encrypt, {}..) here I hope it was helpful.
Upvotes: 0
Reputation: 1
Install dotenv via npm :-
npm i dotenv
Require it in .js file :-
require('dotenv').config();
create .env file in your project folder:-
Paste below text in .env file:-
SOME_LONG_UNGUESSABLE_STRING=Thisisa32bytebasestring
write below code in .js file above mongoose.model() and eplace encryptingItem with your item :-
var secret = process.env.SOME_LONG_UNGUESSABLE_STRING;
userSchema.plugin(encrypt, { secret: secret , encryptedFields: ['encryptingItem']});
Upvotes: 0
Reputation: 11
This error occurs because you haven't installed the .env
package of npm.
you should first install the dotenv
package and require it in your app.js at the beginning
npm install dotenv
require('dotenv').config();
Now you can custom keys in your .env file.
E.g
require("dotenv").config();
const { ApolloServer, gql } = require("apollo-server");
Upvotes: 1
Reputation: 11
This error occurs because you havent installed .env package of npm. you should first install the dotenv package and require it in your app.js at the beginning
Upvotes: 0
Reputation: 189
For local testing (do not commit to git), you need to pass in a variable:
var encKey = "Thisisa32bytebasestring";
var sigKey = "Thisisa64bytebasestring";
For remote commit, you need to pass a String variable for SOME_32BYTE_BASE64_STRING
and SOME_32BYTE_BASE64_STRING
in the .env
file:
var encKey = process.env.SOME_32BYTE_BASE64_STRING
var sigKey = process.env.SOME_64BYTE_BASE64_STRING
You .env
file should contain:
SOME_32BYTE_BASE64_STRING=Thisisa32bytebasestring
SOME_64BYTE_BASE64_STRING=Thisisa64bytebasestring
Upvotes: 0
Reputation: 1
Nothing but your javascript file should be presented along with .env file you create.
This worked for me.
Upvotes: 0
Reputation: 274
Your original error is because process.env.SOME_32BYTE_BASE64_STRING
and process.env.SOME_64BYTE_BASE64_STRING
is not set.
Your second error is because
var encKey = 'a2V5YWxpYXNpc3RoZWJlc3R3b3cqweda'
and var sigKey = 'a2V5YWxpYXNpc3RoZWJlc3R3b3cqwedaa2V5YWxpYXNpc3RoZWJlc3R3b3cqweda'
is not valid 32BYTE_BASE64_STRING and 64BYTE_BASE64_STRING respectively.
To generate valid strings, you can use crypto module:
//32 bytes
require('crypto').randomBytes(32, function(err, buffer) {
var token = buffer.toString('base64');
});
//64 bytes
require('crypto').randomBytes(64, function(err, buffer) {
var token = buffer.toString('base64');
});
Upvotes: 4