Skip to content

Commit 0400847

Browse files
committed
update crypto createCipher to createCipheriv
1 parent d9d6e19 commit 0400847

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
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
}

0 commit comments

Comments
 (0)