Skip to content

Commit 87cf8c9

Browse files
committed
bcrypt note
1 parent ea6ed53 commit 87cf8c9

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
### How to insert data into a mongodb collection directly from the Terminal
2+
3+
Say my user scheme is as below
4+
5+
```js
6+
var UserSchema = new Schema({
7+
username: {
8+
type: String,
9+
unique: true,
10+
required: true
11+
},
12+
password: {
13+
type: String,
14+
required: true
15+
}
16+
});
17+
18+
```
19+
20+
And running ``db.users.find().pretty()`` gives me the below result in terminal
21+
22+
```
23+
{
24+
"_id" : ObjectId("5ba8da81e3a25304ea9ce7ad"),
25+
"username" : "rohanpaul4@gmail.com",
26+
"password" : "$2a$10$7qIhU76cIf7rlvWCGu7bZONAuuumxRpUKxj4.nh8AdghYWlYxCzGO",
27+
"__v" : 0
28+
}
29+
{
30+
"_id" : ObjectId("5bab490ef58260136c4dc6fe"),
31+
"username" : "r@gmail.com",
32+
"password" : "$2a$10$HyXCD5.4U/0CvZHq9SDQ0uxD12BQ46yVAHu18lRRVEQZB3uyHXgy.",
33+
"__v" : 0
34+
}
35+
36+
```
37+
38+
### Now to add a new document (i.e. record ) into this users collection run the below in terminal
39+
40+
```
41+
db.users.insert(
42+
{
43+
"username" : "p@gmail.com",
44+
"password" : "$2a$10$HyXCD5.4U/0CvZHq9SDQ0uxD12BQ46yVAHu18lRRVEQZB3uyHXgy.",
45+
"__v" : 0
46+
}
47+
)
48+
```
49+
50+
#### Note, I dont have to provide a separate _id field as mongodb will generate that by itself
51+
52+
And the part "$2a$10$HyXCD5.4U/0CvZHq9SDQ0uxD12BQ46yVAHu18lRRVEQZB3uyHXgy." is what I generated a hashed version of a plaintext password using the online tool [https://bcrypt-generator.com/](https://bcrypt-generator.com/) . Because in my actual app (where this was implemented) I used bcrypt to hash the password.
53+
54+
But I could very well used "123" in the mongo shell.

Node-Express/bcrypt-How-it-works.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Fist, What is password hashing?
1+
## Fist, What is password hashing - its the below kind of transforming.
22

33
hash("hello") = 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
44
hash("hbllo") = 58756879c05c68dfac9866712fad6a93f8146f337a69afe7dd238f3364946366
@@ -190,5 +190,22 @@ bcrypt.compare = function(s, hash, callback, progressCallback) {
190190
});
191191
});
192192
};
193-
```
193+
194+
```
195+
196+
### Online bcrypt hashing and de-hashing generator and checker
197+
198+
[https://bcrypt-generator.com/](https://bcrypt-generator.com/)
199+
200+
Just put the rounds (which is the salt length to generate, i.e. the function wheere I am hashing the plain-text password )
201+
202+
``bcrypt.hashSync(plainTextPassword, 10)`` So the number 10 is the rounds in the above online tool
203+
204+
After hashing a plaintext password, for checking I will just put the hashed password from the mongo database - i.e. after running terminal command something like ``db.users.find()`` which will give all the users saved in the mongo database.
205+
206+
So an example is this hashed password - ``$2a$10$m0mq4PYOOvm74Gukml4FN.T0Ntobhzi42T6b5v1WIsJ5aZkVzJz3a`` And then put the round as 10 and I will get ``123`` which was my plaintext password in this case.
207+
208+
209+
210+
194211

0 commit comments

Comments
 (0)