36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { EncryptionAlgorithm } from '../core/domain/username.types';
|
|
import * as bcrypt from 'bcrypt';
|
|
import * as argon2 from 'argon2';
|
|
import { PasswordEncrypterPort } from '../core/application/ports/password-encrypter.port';
|
|
|
|
@Injectable()
|
|
export class PasswordEncrypter extends PasswordEncrypterPort {
|
|
encrypt = async (plainPassword: string): Promise<string> => {
|
|
switch (this.encryptionAlgorithm) {
|
|
case EncryptionAlgorithm.BCRYPT:
|
|
return await bcrypt.hash(plainPassword, 10);
|
|
case EncryptionAlgorithm.ARGON2D:
|
|
case EncryptionAlgorithm.ARGON2I:
|
|
case EncryptionAlgorithm.ARGON2ID:
|
|
return await argon2.hash(plainPassword, {
|
|
type: this.argonType(),
|
|
});
|
|
}
|
|
};
|
|
|
|
private argonType = ():
|
|
| typeof argon2.argon2d
|
|
| typeof argon2.argon2i
|
|
| typeof argon2.argon2id => {
|
|
switch (this.encryptionAlgorithm) {
|
|
case EncryptionAlgorithm.ARGON2D:
|
|
return argon2.argon2d;
|
|
case EncryptionAlgorithm.ARGON2I:
|
|
return argon2.argon2i;
|
|
default:
|
|
return argon2.argon2id;
|
|
}
|
|
};
|
|
}
|