Skip to content

Commit

Permalink
update to @StableLib instead of tweetnacl-util
Browse files Browse the repository at this point in the history
  • Loading branch information
devgeeks committed Aug 23, 2021
1 parent ee1770b commit 5bde9db
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 57 deletions.
15 changes: 10 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
"main": "lib/index.js",
"dependencies": {
"@babel/polyfill": "^7.12.1",
"@stablelib/base64": "^1.0.1",
"@stablelib/utf8": "^1.0.1",
"core-js": "^3.16.2",
"fast-sha256": "^1.3.0",
"pouchdb": "^7.2.2",
"pouchdb-find": "^7.2.2",
"transform-pouch": "^1.1.5",
"tweetnacl": "^1.0.3",
"tweetnacl-util": "^0.15.1"
"tweetnacl": "^1.0.3"
},
"devDependencies": {
"@babel/cli": "^7.14.8",
Expand Down
24 changes: 13 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
import nacl from 'tweetnacl';
import naclUtil from 'tweetnacl-util';
// import naclUtil from 'tweetnacl-util';
import {encode as encodeUTF8, decode as decodeUTF8} from '@stablelib/utf8';
import {decode as decodeBase64, encode as encodeBase64} from '@stablelib/base64';
import sha256 from 'fast-sha256';
import TransformPouch from 'transform-pouch';
import PouchDB from 'pouchdb';
import PouchDBFind from 'pouchdb-find';

nacl.util = naclUtil;
// nacl.util = naclUtil;

function encrypt(text, nonce, key) {
const encText = nacl.secretbox(nacl.util.decodeUTF8(text), nonce, key);
return nacl.util.encodeBase64(encText);
const encText = nacl.secretbox(encodeUTF8(text), nonce, key);
return encodeBase64(encText);
}

function decrypt(text, nonce, key) {
const decText = nacl.secretbox.open(nacl.util.decodeBase64(text), nonce, key);
const decText = nacl.secretbox.open(decodeBase64(text), nonce, key);
if (decText === null) {
return undefined;
}
return nacl.util.encodeUTF8(decText);
return decodeUTF8(decText);
}

export function generateNonce() {
return nacl.randomBytes(24);
}

export function keyFromPassphrase(passphrase, salt, rounds = 100000) {
const key = sha256.pbkdf2(nacl.util.decodeUTF8(passphrase), nacl.util.decodeUTF8(salt), rounds, 32);
const key = sha256.pbkdf2(encodeUTF8(passphrase), encodeUTF8(salt), rounds, 32);
return key;
}

Expand All @@ -50,7 +52,7 @@ export default class Echidnajs {
newDoc[field] = encrypt(doc[field], nonce, key);
}
});
newDoc.nonce = nacl.util.encodeBase64(nonce);
newDoc.nonce = encodeBase64(nonce);
return Object.assign(doc, newDoc);
},
outgoing(doc) {
Expand All @@ -59,7 +61,7 @@ export default class Echidnajs {
let error;
keys.forEach((field) => {
if (field !== '_id' && field !== '_rev' && field !== 'nonce') {
newDoc[field] = decrypt(doc[field], nacl.util.decodeBase64(doc.nonce), key);
newDoc[field] = decrypt(doc[field], decodeBase64(doc.nonce), key);
if (newDoc[field] === undefined) {
error = new Error('Failed to decrypt');
}
Expand All @@ -76,8 +78,8 @@ export default class Echidnajs {
}

hash(text) {
const hash = nacl.hash(nacl.util.decodeUTF8(text));
return nacl.util.encodeBase64(hash);
const hash = nacl.hash(encodeUTF8(text));
return encodeBase64(hash);
}

close() {
Expand Down
47 changes: 27 additions & 20 deletions test/node-test/setup.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
var EchidnaDB = require('../../lib').default;
var book = require('../data').book;

function Setup() {
return new Promise((resolve, reject) => {
var options = {
username: 'myusername',
passphrase: 'mypassphrase',
salt: 'saltysaltysalty'
};
var echidna = new EchidnaDB(options);
return new Promise((resolve, reject) => {
var options = {
username: 'myusername',
passphrase: 'mypassphrase',
salt: 'saltysaltysalty',
};
var echidna = new EchidnaDB(options);

echidna.pouch.info()
.then(result => {
console.log(result);
echidna.pouch.put({
_id: 'test',
test: 'testdata'
})
.then(res => resolve(res))
.catch(error => reject(error));
})
.catch(error => reject(error));
});
echidna.pouch
.info()
.then((result) => {
console.log(result);
console.time('encrypting book');
echidna.pouch
.put({
_id: 'test',
test: book,
})
.then((res) => {
console.timeEnd('encrypting book');
return resolve(res);
})
.catch((error) => reject(error));
})
.catch((error) => reject(error));
});
}

module.exports = Setup;
module.exports = Setup;
40 changes: 21 additions & 19 deletions test/node-test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@ const rimraf = require('rimraf');
const EchidnaDB = require('../../lib').default;
const setup = require('./setup');

function error (error) {
console.log(error);
console.log('<< REMOVING TEST POUCH >>');
rimraf('echidnadb-myusername', () => console.log('complete'));
function error(error) {
console.log(error);
console.log('<< REMOVING TEST POUCH >>');
rimraf('echidnadb-myusername', () => console.log('complete'));
}

setup()
.then(res => {
.then((res) => {
const options = {
username: 'myusername',
passphrase: 'mypassphrase',
salt: 'saltysaltysalty'
username: 'myusername',
passphrase: 'mypassphrase',
salt: 'saltysaltysalty',
};
console.log('<< CREATING TEST POUCH >>');
const echidna = new EchidnaDB(options);

echidna.pouch.info()
.then(result => {
console.log(result);
echidna.pouch.get('test')
.then(res => {
console.log(res);
console.log('<< REMOVING TEST POUCH >>');
rimraf('echidnadb-myusername', () => console.log('complete'));
});
echidna.pouch.info().then((result) => {
console.time('pouch info');
console.log(result);
console.timeEnd('pouch info');
console.time('decrypting book');
echidna.pouch.get('test').then((res) => {
// console.log(res);
console.timeEnd('decrypting book');
console.log('<< REMOVING TEST POUCH >>');
rimraf('echidnadb-myusername', () => console.log('complete'));
});
});
})
.catch(error);
})
.catch(error);

0 comments on commit 5bde9db

Please sign in to comment.