keeweb / kdbxweb

Web Kdbx library
https://app.keeweb.info
MIT License
413 stars 57 forks source link

Can't open an empty database? #15

Closed expiron closed 7 years ago

expiron commented 7 years ago

Hello. Today I created an empty kdbx file using code:

let credentials = new kdbxweb.Credentials(kdbxweb.ProtectedValue.fromString('password'));
let db = kdbxweb.Kdbx.create(credentials, 'emptydb');
let group = db.createGroup(db.getDefaultGroup(), '1stgroup');
db.save().then(dataAsArrayBuffer => {
    fs.writeFileSync('./empty.kdbx', Buffer.from(dataAsArrayBuffer));
})

And then I opened it using code:

let dataAsArrayBuffer = fs.readFileSync('./empty.kdbx').buffer;
let credentials = new kdbxweb.Credentials(kdbxweb.ProtectedValue.fromString('password'));
kdbxweb.Kdbx.load(dataAsArrayBuffer, credentials).then(db => {
    console.log(db);
}).catch(err => {
    console.log(err);
});

But it threw the error:

{ [KdbxError: Error BadSignature]
  name: 'KdbxError',
  code: 'BadSignature',
  message: 'Error BadSignature' }

Using KeePass Password Manager or KeeWeb Online, it (kdbx file I created) can be opened normally. Also, I have tried to create a kdbx file using KeePass whose structure like this:

emptydb
  |--1stgroup
  |--Recycle Bin

And I opened it using the code as mentioned but got the same error. I want to know what's the meaning of 'BadSignature' and how to solve my problem. My environment is Node.js v8.5.0 and Windows 10.

antelle commented 7 years ago

You cannot convert node.js Buffer to ArrayBuffer this way: when you call .buffer, it can return more bytes than included in node Buffer (which is a slice of the originally allocated ArrayBuffer), and this is what happens in your case as well. To convert between them, you should use an array, for example. So, if you replace

let dataAsArrayBuffer = fs.readFileSync('./empty.kdbx').buffer;

with

let dataAsArrayBuffer = new Uint8Array(fs.readFileSync('./empty.kdbx')).buffer;

it will work. https://nodejs.org/api/buffer.html

expiron commented 7 years ago

Thanks!