1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-22 11:45:59 +01:00

stub out crypto function service with pbkdf2

This commit is contained in:
Kyle Spearrin 2018-04-14 23:20:37 -04:00
parent 759ac04ee9
commit 4ad29e25f3
5 changed files with 68 additions and 5 deletions

10
package-lock.json generated
View File

@ -100,7 +100,7 @@
"integrity": "sha512-PlKJw6ujJXLJjbvB3T0UCbY3jibKM6/Ya5cc9j1q+mYDeK3aR4Dp+20ZwxSuvJr9mIoPxp7+IL4aMOEvsscRTA==",
"dev": true,
"requires": {
"@types/node": "8.5.7"
"@types/node": "8.0.19"
}
},
"@types/handlebars": {
@ -140,9 +140,9 @@
"dev": true
},
"@types/node": {
"version": "8.5.7",
"resolved": "https://registry.npmjs.org/@types/node/-/node-8.5.7.tgz",
"integrity": "sha512-+1ZfzGIq8Y3EV7hPF7bs3i+Gi2mqYOiEGGRxGYPrn+hTYLMmzg+/5TkMkCHiRtLB38XSNvr/43aQ9+cUq4BbBg==",
"version": "8.0.19",
"resolved": "https://registry.npmjs.org/@types/node/-/node-8.0.19.tgz",
"integrity": "sha512-VRQB+Q0L3YZWs45uRdpN9oWr82meL/8TrJ6faoKT5tp0uub2l/aRMhtm5fo68h7kjYKH60f9/bay1nF7ZpTW5g==",
"dev": true
},
"@types/node-forge": {
@ -163,7 +163,7 @@
"integrity": "sha1-IpwVfGvB5n1rmQ5sXhjb0v9Yz/A=",
"dev": true,
"requires": {
"@types/node": "8.5.7"
"@types/node": "8.0.19"
}
},
"@types/webcrypto": {

View File

@ -38,6 +38,7 @@
"@angular/router": "5.2.0",
"@angular/upgrade": "5.2.0",
"@types/lunr": "2.1.5",
"@types/node": "8.0.19",
"@types/node-forge": "0.7.1",
"@types/papaparse": "4.1.31",
"@types/webcrypto": "0.0.28",

View File

@ -0,0 +1,3 @@
export abstract class CryptoFunctionService {
pbkdf2: (password: Buffer, salt: Buffer, iterations: number, length: number) => Promise<ArrayBuffer>
}

View File

@ -0,0 +1,17 @@
import * as crypto from 'crypto';
import { CryptoFunctionService } from '../abstractions/cryptoFunction.service';
export class NodeCryptoFunctionService implements CryptoFunctionService {
async pbkdf2(password: Buffer, salt: Buffer, iterations: number, length: number): Promise<ArrayBuffer> {
return new Promise<ArrayBuffer>((resolve, reject) => {
crypto.pbkdf2(password, salt, iterations, length, 'sha256', (error, key) => {
if (error != null) {
reject(error);
} else {
resolve(key.buffer);
}
});
});
}
}

View File

@ -0,0 +1,42 @@
import * as forge from 'node-forge';
import { CryptoFunctionService } from '../abstractions/cryptoFunction.service';
import { PlatformUtilsService } from '../abstractions/platformUtils.service';
export class WebCryptoFunctionService implements CryptoFunctionService {
private crypto: Crypto;
private subtle: SubtleCrypto;
constructor(private win: Window, private platformUtilsService: PlatformUtilsService) {
this.crypto = win.crypto;
this.subtle = win.crypto.subtle;
}
async pbkdf2(password: Buffer, salt: Buffer, iterations: number, length: number): Promise<ArrayBuffer> {
const importedKey = await this.subtle.importKey('raw', password, { name: 'PBKDF2' },
false, ['deriveKey', 'deriveBits']);
const alg: Pbkdf2Params = {
name: 'PBKDF2',
salt: salt,
iterations: iterations,
hash: { name: 'SHA-256' },
};
const keyType: AesDerivedKeyParams = {
name: 'AES-CBC',
length: length,
};
const derivedKey = await this.subtle.deriveKey(alg, importedKey, keyType, true, ['encrypt', 'decrypt']);
return await this.subtle.exportKey('raw', derivedKey);
}
async sha1(value: Buffer): Promise<ArrayBuffer> {
if (this.platformUtilsService.isEdge()) {
return new Uint8Array([1]).buffer; // TODO: sha1 with forge
} else {
return await this.subtle.digest({ name: 'SHA-1' }, value);
}
}
}