Using an encryption key in Javascript


#1

I am having trouble encrypting my Realm data store. I’ve been trying to leverage the documentation and examples but haven’t had any luck yet. Below is the simplest code I’ve tried:

const Realm = require(‘realm’);
const minimal = async () => {
const key = new Int8Array(64);
key.fill(7);
const user = await Realm.Sync.User.login({AUTH_URL}, {USERNAME}, {PSWD});
const realmConfig = user.createConfiguration({ encryptionKey: key });
return Realm.open(realmConfig);
}
minimal()
.then(realm => {
console.log(‘done: %j’, realm);
process.exit(0);
})
.catch(err => {
console.log(err.stack);
process.exit(1);
});

This results in the following error:

Error: encryptionKey must be of type ‘binary’, got ([object Object])
at Promise (/myproject/node_modules/realm/lib/extensions.js:85:29)
at new Promise ()
at Function.open (/myproject/node_modules/realm/lib/extensions.js:84:27)
at minimal (/myproject/util/enc.js:7:16)
at
at process._tickCallback (internal/process/next_tick.js:188:7)

Any tips? What am I doing wrong? Thanks in advance for your help!!!


#2

You can use ArrayBuffer instead of Int8Array.


#3

Thanks for the reply, @kneth. I appreciate your help.

I tried changing:

const key = new Int8Array(64);
key.fill(7);
const user = await Realm.Sync.User.login({AUTH_URL}, {USERNAME}, {PSWD});
const realmConfig = user.createConfiguration({ encryptionKey: key });

to

const buffer = new ArrayBuffer(64);
const view = new Int8Array(buffer);
view.fill(7);
const user = await Realm.Sync.User.login({AUTH_URL}, {USERNAME}, {PSWD});
const realmConfig = user.createConfiguration({ encryptionKey: buffer });

but the results were the same: Error: encryptionKey must be of type ‘binary’, got ([object Object]) with the same stack trace.

Did I correctly understand the change you recommended? Any other suggestions?

I tried to look for clues in the source code but it seems like the root of the problem is in the C++ code. My C++ is a bit rusty, to put it mildly… I was having trouble making heads or tails of it.

Also, I didn’t mention that I am working with Node.js 8.11.2 on macOS 10.13.5. Not sure if that offers any clues…

Thanks again for the help!


#4

You should be able to use Int8Array(64) as a key: https://github.com/realm/realm-js/blob/master/tests/js/encryption-tests.js#L80