Closed yolopunk closed 2 years ago
You can use a PRAGMA
statement.
const db = require('better-sqlite3-multiple-ciphers')('foobar.db', options);
db.pragma("cipher='sqlcipher'");
better-sqlite3-multiple-ciphers
)// 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' }
typeorm
)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
Instead of using the key
field, can you try the following?
prepareDatabase: db => {
db.pragma(`cipher='sqlcipher'`)
db.pragma(`key='secret-key'`)
}
I tried. The same mistakeļ¼ @m4heshd
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.
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.
Below is the reproducible repo link
This issue is confirmed to be third-party and being tracked at https://github.com/typeorm/typeorm/issues/8475.
Now that found the problem.I'll turn this issue off. Thank you again
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.