1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-13 23:03:32 +00:00

implement crypto functions in more places

This commit is contained in:
Kyle Spearrin
2018-04-21 23:55:21 -04:00
parent fc1114a6bd
commit 5e7115f78d
6 changed files with 26 additions and 43 deletions

View File

@@ -1,19 +1,20 @@
import { AuditService as AuditServiceAbstraction } from '../abstractions/audit.service';
import { CryptoService } from '../abstractions/crypto.service';
import { CryptoFunctionService } from '../abstractions/cryptoFunction.service';
import { Utils } from '../misc/utils';
const PwnedPasswordsApi = 'https://api.pwnedpasswords.com/range/';
export class AuditService implements AuditServiceAbstraction {
constructor(private cryptoService: CryptoService) { }
constructor(private cryptoFunctionService: CryptoFunctionService) { }
async passwordLeaked(password: string): Promise<number> {
const hash = (await this.cryptoService.sha1(password)).toUpperCase();
const hashBytes = await this.cryptoFunctionService.hash(password, 'sha1');
const hash = Utils.fromBufferToHex(hashBytes).toUpperCase();
const hashStart = hash.substr(0, 5);
const hashEnding = hash.substr(5);
const response = await fetch(PwnedPasswordsApi + hashStart);
const leakedHashes = await response.text();
const match = leakedHashes.split(/\r?\n/).find((v) => {
return v.split(':')[0] === hashEnding;
});