forked from gitroomhq/postiz-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.service.ts
44 lines (36 loc) Β· 1.2 KB
/
auth.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { sign, verify } from 'jsonwebtoken';
import { hashSync, compareSync } from 'bcrypt';
import bcrypt from 'bcrypt';
import crypto from 'crypto';
export class AuthService {
static hashPassword(password: string) {
return hashSync(password, 10);
}
static comparePassword(password: string, hash: string) {
return compareSync(password, hash);
}
static signJWT(value: object) {
return sign(value, process.env.JWT_SECRET!);
}
static verifyJWT(token: string) {
return verify(token, process.env.JWT_SECRET!);
}
static fixedEncryption(value: string) {
// encryption algorithm
const algorithm = 'aes-256-cbc';
// create a cipher object
const cipher = crypto.createCipher(algorithm, process.env.JWT_SECRET);
// encrypt the plain text
let encrypted = cipher.update(value, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}
static fixedDecryption(hash: string) {
const algorithm = 'aes-256-cbc';
const decipher = crypto.createDecipher(algorithm, process.env.JWT_SECRET);
// decrypt the encrypted text
let decrypted = decipher.update(hash, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
}