cleanup auditservice

This commit is contained in:
Kyle Spearrin 2018-02-28 10:58:34 -05:00
parent 1aabb42e47
commit e1041e9b5b
1 changed files with 8 additions and 11 deletions

View File

@ -3,24 +3,21 @@ import { CryptoService } from '../abstractions/crypto.service';
const PwnedPasswordsApi = 'https://api.pwnedpasswords.com/range/'; const PwnedPasswordsApi = 'https://api.pwnedpasswords.com/range/';
export class AuditService { export class AuditService {
constructor(private cryptoService: CryptoService) { constructor(private cryptoService: CryptoService) {
} }
async passwordLeaked(password: string): Promise<number> { async passwordLeaked(password: string): Promise<number> {
const hash = (await this.cryptoService.sha1(password)).toUpperCase(); const hash = (await this.cryptoService.sha1(password)).toUpperCase();
const hashStart = hash.substr(0, 5);
const response = await fetch(PwnedPasswordsApi + hash.substr(0, 5));
const leakedHashes = await response.text();
const hashEnding = hash.substr(5); const hashEnding = hash.substr(5);
const match = leakedHashes const response = await fetch(PwnedPasswordsApi + hashStart);
.split(/\r?\n/) const leakedHashes = await response.text();
.find((v) => {
return v.split(':')[0] === hashEnding;
});
return match ? parseInt(match.split(':')[1], 10) : 0; const match = leakedHashes.split(/\r?\n/).find((v) => {
return v.split(':')[0] === hashEnding;
});
return match != null ? parseInt(match.split(':')[1], 10) : 0;
} }
} }