keeweb/app/scripts/util/kdbxweb-init.js

43 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-01-30 21:26:31 +01:00
'use strict';
const kdbxweb = require('kdbxweb');
const KdbxwebInit = {
init() {
kdbxweb.CryptoEngine.argon2 = this.argon2;
},
argon2(password, salt, memory, iterations, length, parallelism, type, version) {
const Module = require('argon2');
2017-01-31 07:50:28 +01:00
const passwordLen = password.byteLength;
2017-01-30 21:26:31 +01:00
password = Module.allocate(new Uint8Array(password), 'i8', Module.ALLOC_NORMAL);
2017-01-31 07:50:28 +01:00
const saltLen = salt.byteLength;
2017-01-30 21:26:31 +01:00
salt = Module.allocate(new Uint8Array(salt), 'i8', Module.ALLOC_NORMAL);
2017-01-31 07:50:28 +01:00
const hash = Module.allocate(new Array(length), 'i8', Module.ALLOC_NORMAL);
const encodedLen = 512;
const encoded = Module.allocate(new Array(encodedLen), 'i8', Module.ALLOC_NORMAL);
2017-01-30 21:26:31 +01:00
// jshint camelcase:false
try {
2017-01-31 07:50:28 +01:00
const res = Module._argon2_hash(iterations, memory, parallelism,
2017-01-30 21:26:31 +01:00
password, passwordLen, salt, saltLen,
hash, length, encoded, encodedLen, type, version);
if (res) {
return Promise.reject('Argon2 error ' + res);
}
2017-01-31 07:50:28 +01:00
const hashArr = new Uint8Array(length);
2017-01-30 21:26:31 +01:00
for (let i = 0; i < length; i++) {
hashArr[i] = Module.HEAP8[hash + i];
}
Module._free(password);
Module._free(salt);
Module._free(hash);
Module._free(encoded);
return Promise.resolve(hashArr);
} catch (e) {
return Promise.reject(e);
}
}
};
module.exports = KdbxwebInit;