Schneejäger
Schneejäger

Reputation: 231

Inserting a password in MongoDB

Is there a method to transform the string into a hash from mongo command line when inserting data? I want to insert account details by hand and make as many users as there is need.

Upvotes: 0

Views: 2945

Answers (1)

Daniele Tassone
Daniele Tassone

Reputation: 2174

If i correctly understood your problem, you want to hash a value from the shell. I don't have understood why you need this, but i will try to give you some ways to resolve the scenario.

Custom made solution:

mongoDB has Stored Procedures, that you can write in JS and you can call from any context. This will give you the 100% ownership to write you Stored Procedure and call it from the shell. In this case I'm calling "fromStringToHash" function that i have previously created...

db.loadServerScripts();
db.PasswordTest.insert({"name":"Daniele", "password": "notSecure"});
db.PasswordTest.insert({"name":"Daniele", "password": fromStringToHash("theSecretPassword")});

https://docs.mongodb.com/manual/tutorial/store-javascript-function-on-server/

MD5

If you just need a way to compute an hash, take a look

db.PasswordTest.insert({"name":"Daniele", "password": hex_md5("theSecretPassword")});

You can also use both solutions (a Stored Procedures that internally call hex_md5 in order to be free to change that function in the future, maybe). This function is available from MongoDB shell context.

Upvotes: 1

Related Questions