Skip to content

Commit

Permalink
Create function to change password and algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
jayralencar committed Apr 28, 2016
1 parent a1ecb25 commit 7f6b08c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This change log is started in 0.3.2;
## 0.3.4 (2016-04-28)
- Using algorithm and password in pvDecrypt
- Using algorithm and password in pvEncrypt
- Create function to change password and algorithm.
```js
sqlite.change(filePath, oldPassword, newPassword, oldAlgorithm, newAlgorithm);
```

## 0.3.3 (2016-04-26)
- Show error if password is invalid - review
Expand Down
15 changes: 12 additions & 3 deletions sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ sqlite.prototype.pvDecrypt = function(buffer, algorithm, password){
* Buffer encryption
*
* @param {Object} buffer - buffer
* @param {String} algorithm - Algorithm
* @param {String} password - Password
* @return {Object} - Encrypted buffer
*/
sqlite.prototype.pvEncrypt = function(buffer, algorithm, password){
Expand Down Expand Up @@ -193,10 +195,9 @@ sqlite.prototype.encrypt = function(from, to, password, algorithm, options){
* @param {String} newPassword - password
* @param {String} algorithm - algorithm
* @param {String} newAlgorithm - newAlgorithm
* @param {Function} callback - callback function
*/

sqlite.prototype.changePassword = function(file, oldPassword, newPassword, algorithm, newAlgorithm, callback) {
sqlite.prototype.change = function(file, oldPassword, newPassword, algorithm, newAlgorithm) {
if(!file){
throw "Please inform a file!";
}else{
Expand All @@ -220,7 +221,15 @@ sqlite.prototype.changePassword = function(file, oldPassword, newPassword, algor
if(this.algorithms.indexOf(newAlgorithm)==-1){
throw "Your new algorithm is not supported";
}else{

try{
this.connect(file, oldPassword, algorithm);
var decrypted = this.pvDecrypt(fs.readFileSync(file), algorithm, oldPassword);
var encrypted = this.pvEncrypt(decrypted,newAlgorithm, newPassword);
var buffer = new Buffer(encrypted);
fs.writeFileSync(file, buffer);
}catch(x){
throw x;
}
}
}
}
Expand Down
60 changes: 30 additions & 30 deletions test/app.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
var sqlite = require('../sqlite.js'); //requiring

// Change password
sqlite.changePassword('test/Database.rec','myPass');
// sqlite.change('test/Database.rec','88560848','myPass','aes-256-ctr','camellia-256-cfb1');

// To use initialization vector
// sqlite.iv = '13ewr3iJ';
sqlite.iv = '13ewr3iJ';

// //Connecting - (databaseFile, [password], [algorithm])
// try{
// sqlite.connect('test/Database.rec','myPass','aes-256-ctr');
// }catch(x){
// console.log(x)
// }
//Connecting - (databaseFile, [password], [algorithm])
try{
sqlite.connect('test/Database.rec','myPass','camellia-256-cfb1');
}catch(x){
console.log(x)
}

// // Creating Table - you can run any command
// sqlite.run("CREATE TABLE COMPANYS(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL);");
// Creating Table - you can run any command
sqlite.run("CREATE TABLE COMPANYS(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL);");

// //Inserting - this function can be sync to, look the wiki
// sqlite.insert("COMPANYS",{NAME:"COMPANY"}, function(inserid){
// console.log(inserid);
// });
//Inserting - this function can be sync to, look the wiki
sqlite.insert("COMPANYS",{NAME:"COMPANY"}, function(inserid){
console.log(inserid);
});

// //Updating - returns the number of rows modified - can be async too
// var rows_modified = sqlite.update("COMPANYS",{NAME:"TESTING UPDATE"},{ID:1});
//Updating - returns the number of rows modified - can be async too
var rows_modified = sqlite.update("COMPANYS",{NAME:"TESTING UPDATE"},{ID:1});

// //Create your function
// function concat(a,b){
// return a+b;
// }
//Create your function
function concat(a,b){
return a+b;
}

// //Add your function to connection
// sqlite.create_function(concat);
//Add your function to connection
sqlite.create_function(concat);

// // Use your function in the SQL
// console.log(sqlite.run("SELECT ID , concat(ID, NAME) as concat FROM COMPANYS;"));
// Use your function in the SQL
console.log(sqlite.run("SELECT ID , concat(ID, NAME) as concat FROM COMPANYS;"));

// //Decrypting database file
// sqlite.decrypt("test/Database.rec","test/decrypted.db", 'myPass');
//Decrypting database file
sqlite.decrypt("test/Database.rec","test/decrypted.db", 'myPass');

// //Encrypting database file
// sqlite.encrypt("test/decrypted.db","test/reencrypted.rec", 'myPass',"bf",{iv: "ads343ef"});
//Encrypting database file
sqlite.encrypt("test/decrypted.db","test/reencrypted.rec", 'myPass',"bf",{iv: "ads343ef"});

// // Closing connection
// sqlite.close();
// Closing connection
sqlite.close();

0 comments on commit 7f6b08c

Please sign in to comment.