1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-20 02:03:39 +00:00

determine length based on alg. fix 512 wc pbkdf2

This commit is contained in:
Kyle Spearrin
2018-04-17 21:18:47 -04:00
parent 81f7bd7b76
commit 3ca8716fc6
5 changed files with 35 additions and 39 deletions

View File

@@ -12,15 +12,18 @@ describe('WebCrypto Function Service', () => {
const utf8256Key = 'yqvoFXgMRmHR3QPYr5pyR4uVuoHkltv9aHUP63p8n7I=';
const unicode256Key = 'ZdeOata6xoRpB4DLp8zHhXz5kLmkWtX5pd+TdRH8w8w=';
const regular512Key = 'liTi/Ke8LPU1Qv+Vl7NGEVt/XMbsBVJ2kQxtVG/Z1/I=';
const utf8512Key = 'df0KdvIBeCzD/kyXptwQohaqUa4e7IyFUyhFQjXCANs=';
const unicode512Key = 'FE+AnUJaxv8jh+zUDtZz4mjjcYk0/PZDZm+SLJe3Xtw=';
const regular512Key = 'liTi/Ke8LPU1Qv+Vl7NGEVt/XMbsBVJ2kQxtVG/Z1/JFHFKQW3ZkI81qVlwTiCpb+cFXzs+57' +
'eyhhx5wfKo5Cg==';
const utf8512Key = 'df0KdvIBeCzD/kyXptwQohaqUa4e7IyFUyhFQjXCANu5T+scq55hCcE4dG4T/MhAk2exw8j7ixRN' +
'zXANiVZpnw==';
const unicode512Key = 'FE+AnUJaxv8jh+zUDtZz4mjjcYk0/PZDZm+SLJe3XtxtnpdqqpblX6JjuMZt/dYYNMOrb2+mD' +
'L3FiQDTROh1lg==';
testPbkdf2ValidKey(false, 'sha256', regular256Key, utf8256Key, unicode256Key);
testPbkdf2ValidKey(false, 'sha512', regular512Key, utf8512Key, unicode512Key);
testPbkdf2(false, 'sha256', regular256Key, utf8256Key, unicode256Key);
testPbkdf2(false, 'sha512', regular512Key, utf8512Key, unicode512Key);
testPbkdf2ValidKey(true, 'sha256', regular256Key, utf8256Key, unicode256Key);
testPbkdf2ValidKey(true, 'sha512', regular512Key, utf8512Key, unicode512Key);
testPbkdf2(true, 'sha256', regular256Key, utf8256Key, unicode256Key);
testPbkdf2(true, 'sha512', regular512Key, utf8512Key, unicode512Key);
});
describe('hash', () => {
@@ -64,7 +67,7 @@ describe('WebCrypto Function Service', () => {
});
});
function testPbkdf2ValidKey(edge: boolean, algorithm: 'sha256' | 'sha512', regularKey: string,
function testPbkdf2(edge: boolean, algorithm: 'sha256' | 'sha512', regularKey: string,
utf8Key: string, unicodeKey: string) {
const forEdge = edge ? ' for edge' : '';
const regularEmail = 'user@example.com';
@@ -76,26 +79,26 @@ function testPbkdf2ValidKey(edge: boolean, algorithm: 'sha256' | 'sha512', regul
it('should create valid ' + algorithm + ' key from regular input' + forEdge, async () => {
const webCryptoFunctionService = getWebCryptoFunctionService(edge);
const key = await webCryptoFunctionService.pbkdf2(regularPassword, regularEmail, algorithm, 5000, 256);
const key = await webCryptoFunctionService.pbkdf2(regularPassword, regularEmail, algorithm, 5000);
expect(UtilsService.fromBufferToB64(key)).toBe(regularKey);
});
it('should create valid ' + algorithm + ' key from utf8 input' + forEdge, async () => {
const webCryptoFunctionService = getWebCryptoFunctionService(edge);
const key = await webCryptoFunctionService.pbkdf2(utf8Password, utf8Email, algorithm, 5000, 256);
const key = await webCryptoFunctionService.pbkdf2(utf8Password, utf8Email, algorithm, 5000);
expect(UtilsService.fromBufferToB64(key)).toBe(utf8Key);
});
it('should create valid ' + algorithm + ' key from unicode input' + forEdge, async () => {
const webCryptoFunctionService = getWebCryptoFunctionService(edge);
const key = await webCryptoFunctionService.pbkdf2(unicodePassword, regularEmail, algorithm, 5000, 256);
const key = await webCryptoFunctionService.pbkdf2(unicodePassword, regularEmail, algorithm, 5000);
expect(UtilsService.fromBufferToB64(key)).toBe(unicodeKey);
});
it('should create valid ' + algorithm + ' key from array buffer input' + forEdge, async () => {
const webCryptoFunctionService = getWebCryptoFunctionService(edge);
const key = await webCryptoFunctionService.pbkdf2(UtilsService.fromUtf8ToArray(regularPassword).buffer,
UtilsService.fromUtf8ToArray(regularEmail).buffer, algorithm, 5000, 256);
UtilsService.fromUtf8ToArray(regularEmail).buffer, algorithm, 5000);
expect(UtilsService.fromBufferToB64(key)).toBe(regularKey);
});
}
@@ -147,12 +150,6 @@ function getWebCryptoFunctionService(edge = false) {
}
class BrowserPlatformUtilsService implements PlatformUtilsService {
constructor(private edge: boolean) { }
isEdge() {
return this.edge;
}
identityClientId: string;
getDevice: () => DeviceType;
getDeviceString: () => string;
@@ -173,4 +170,10 @@ class BrowserPlatformUtilsService implements PlatformUtilsService {
type?: string) => Promise<boolean>;
isDev: () => boolean;
copyToClipboard: (text: string, options?: any) => void;
constructor(private edge: boolean) { }
isEdge() {
return this.edge;
}
}