44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
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;
|
|
}
|
|
}
|