Skip to content

Commit b31f243

Browse files
authored
Merge pull request #191 from Antibioticvz/update-crypto-bcrypt
Update crypto and bcrypt
2 parents 59bfd33 + d511e64 commit b31f243

File tree

4 files changed

+1434
-1405
lines changed

4 files changed

+1434
-1405
lines changed

app/middleware/auth.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const crypto = require('crypto')
2-
const algorithm = 'aes-256-ecb'
3-
const secret = process.env.JWT_SECRET
2+
3+
const password = process.env.JWT_SECRET
4+
const algorithm = 'aes-192-cbc'
5+
// Key length is dependent on the algorithm. In this case for aes192, it is
6+
// 24 bytes (192 bits).
7+
const key = crypto.scryptSync(password, 'salt', 24)
8+
const iv = Buffer.alloc(16, 0) // Initialization crypto vector
49

510
module.exports = {
611
/**
@@ -27,23 +32,28 @@ module.exports = {
2732
* Encrypts text
2833
* @param {string} text - text to encrypt
2934
*/
35+
3036
encrypt(text) {
31-
const cipher = crypto.createCipher(algorithm, secret)
32-
let crypted = cipher.update(text, 'utf8', 'hex')
33-
crypted += cipher.final('hex')
34-
return crypted
37+
const cipher = crypto.createCipheriv(algorithm, key, iv)
38+
39+
let encrypted = cipher.update(text, 'utf8', 'hex')
40+
encrypted += cipher.final('hex')
41+
42+
return encrypted
3543
},
3644

3745
/**
3846
* Decrypts text
3947
* @param {string} text - text to decrypt
4048
*/
49+
4150
decrypt(text) {
42-
const decipher = crypto.createDecipher(algorithm, secret)
51+
const decipher = crypto.createDecipheriv(algorithm, key, iv)
52+
4353
try {
44-
let dec = decipher.update(text, 'hex', 'utf8')
45-
dec += decipher.final('utf8')
46-
return dec
54+
let decrypted = decipher.update(text, 'hex', 'utf8')
55+
decrypted += decipher.final('utf8')
56+
return decrypted
4757
} catch (err) {
4858
return err
4959
}

app/models/user.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const mongoose = require('mongoose')
2-
const bcrypt = require('bcrypt-nodejs')
2+
const bcrypt = require('bcrypt')
33
const validator = require('validator')
44
const mongoosePaginate = require('mongoose-paginate-v2')
55

@@ -83,7 +83,7 @@ const UserSchema = new mongoose.Schema(
8383
)
8484

8585
const hash = (user, salt, next) => {
86-
bcrypt.hash(user.password, salt, null, (error, newHash) => {
86+
bcrypt.hash(user.password, salt, (error, newHash) => {
8787
if (error) {
8888
return next(error)
8989
}

0 commit comments

Comments
 (0)