m4heshd / better-sqlite3-multiple-ciphers

better-sqlite3 with multiple-cipher encryption support šŸ”’
MIT License
140 stars 27 forks source link

How change encryption method? #4

Closed yolopunk closed 2 years ago

yolopunk commented 2 years ago

Encryption using default method sqleet. I want to know how change encryption method using sqlcipher.

But I want to switch sqlcipher. Due I am using typeorm. And I tried compile sqlcipher for better-sqlite3 but I failed.

Typeorm has an option named key for sqlcipher.

m4heshd commented 2 years ago

You can use a PRAGMA statement.

const db = require('better-sqlite3-multiple-ciphers')('foobar.db', options);
db.pragma("cipher='sqlcipher'");
yolopunk commented 2 years ago
// encrypt db
const db = require('better-sqlite3-multiple-ciphers')('foobar.db', { verbose: console.log })
db.pragma("cipher='sqlcipher'")
db.pragma(`rekey='secret-key'`)
db.prepare(`CREATE TABLE "post" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "text" varchar NOT NULL)`).run()
const stmt = db.prepare('INSERT INTO post (title, text) VALUES (?, ?)')
const info = stmt.run('Joey', 'my homie')
db.close()

// decrypt db
const db = require('better-sqlite3-multiple-ciphers')('foobar.db', { verbose: console.log });

db.pragma(`cipher='sqlcipher'`)
db.pragma("key='secret-key'");
const stmt = db.prepare("SELECT * FROM post")
console.log(stmt.get()); // { id: 1, title: 'Joey', text: 'my homie' }
import { createConnection } from 'typeorm'
import { BetterSqlite3ConnectionOptions } from 'typeorm/driver/better-sqlite3/BetterSqlite3ConnectionOptions'
import { Post } from './entity/post'

const config: BetterSqlite3ConnectionOptions = {
  type: 'better-sqlite3',
  key: 'secret-key',
  database: 'foobar.db',
  driver: require('better-sqlite3-multiple-ciphers'),
  entities: ['entity/*.ts'],
  logging: true,
  verbose: console.log,
  prepareDatabase: db => {
    db.pragma(`cipher='sqlcipher'`)
  }
}

const start = async () => {
  const conn = await createConnection(config)
  const posts = await conn.manager.find(Post)
  console.log(posts)
}

start()  // SqliteError: file is not a database

I donā€™t know whatā€™s wrong with my options for typeorm. Can you help me please?

@m4heshd

m4heshd commented 2 years ago

Instead of using the key field, can you try the following?

prepareDatabase: db => {
    db.pragma(`cipher='sqlcipher'`)
    db.pragma(`key='secret-key'`)
}
yolopunk commented 2 years ago

I tried. The same mistakeļ¼ @m4heshd

m4heshd commented 2 years ago

Hmm that's unfortunate. I think it would be beneficial to move this question to typeorm issue tracker. People with more experience related to typeorm would be able to diagnose this much quicker. Unfortunately I lack the time to go through typeorm's internals at this moment. Make sure to post the complete findings including the above code. Also make sure to tag this issue in your new issue so I can assist there too.

m4heshd commented 2 years ago

One more thing. It would really help if you can create a reproducible repo of this test project with the above code so maybe we can take a look at that.

yolopunk commented 2 years ago

Below is the reproducible repo link

https://github.com/yolopunk/typeorm-better-sqlite-sqlcipher

m4heshd commented 2 years ago

This issue is confirmed to be third-party and being tracked at https://github.com/typeorm/typeorm/issues/8475.

yolopunk commented 2 years ago

Now that found the problem.I'll turn this issue off. Thank you again