keeweb/app/scripts/comp/browser/feature-tester.js

67 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-09-15 14:16:32 +02:00
import kdbxweb from 'kdbxweb';
import { Features } from 'util/features';
2018-08-30 22:16:31 +02:00
const FeatureTester = {
test() {
return Promise.resolve()
.then(() => this.checkWebAssembly())
.then(() => this.checkWebCrypto())
.then(() => this.checkLocalStorage());
},
checkWebAssembly() {
try {
2019-08-18 08:05:38 +02:00
const module = new global.WebAssembly.Module(
Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00)
);
2018-08-30 22:16:31 +02:00
return new global.WebAssembly.Instance(module) instanceof global.WebAssembly.Instance;
} catch (e) {
throw 'WebAssembly is not supported';
}
},
checkWebCrypto() {
return Promise.resolve().then(() => {
const aesCbc = kdbxweb.CryptoEngine.createAesCbc();
const key = '6b2796fa863a6552986c428528d053b76de7ba8e12f8c0e74edb5ed44da3f601';
const data = 'e567554429098a38d5f819115edffd39';
const iv = '4db46dff4add42cb813b98de98e627c4';
const exp = '46ab4c37d9ec594e5742971f76f7c1620bc29f2e0736b27832d6bcc5c1c39dc1';
2019-08-16 23:05:39 +02:00
return aesCbc
.importKey(kdbxweb.ByteUtils.hexToBytes(key))
.then(() => {
return aesCbc
2019-08-18 08:05:38 +02:00
.encrypt(
kdbxweb.ByteUtils.hexToBytes(data),
kdbxweb.ByteUtils.hexToBytes(iv)
)
2019-08-16 23:05:39 +02:00
.then(res => {
if (kdbxweb.ByteUtils.bytesToHex(res) !== exp) {
throw 'AES is not working properly';
}
if (kdbxweb.CryptoEngine.random(1).length !== 1) {
throw 'Random is not working';
}
});
})
.catch(e => {
throw 'WebCrypto is not supported: ' + e;
2018-08-30 22:16:31 +02:00
});
});
},
checkLocalStorage() {
2019-09-15 08:11:11 +02:00
if (Features.isDesktop) {
2018-08-30 22:21:57 +02:00
return;
}
2018-08-30 22:16:31 +02:00
try {
localStorage.setItem('_test', '1');
localStorage.removeItem('_test');
} catch (e) {
2019-09-15 12:37:11 +02:00
throw 'LocalStorage is not supported';
2018-08-30 22:16:31 +02:00
}
}
};
2019-09-15 14:16:32 +02:00
export { FeatureTester };