In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
SQLite is a lightweight, cross-platform, open-source database engine, and its advantages in read/write efficiency, overhead, latency, and overall simplicity make it the best solution for mobile databases (i.e. iOS, Android). However, the free version of SQLite has a fatal flaw: it does not support encryption. This results in data stored in SQLite being viewable by anyone with any text editor.
If we want to encrypt our database, the solution is to use SQL Cipher, another open source encryption database. SQL Cipher uses 256-bit AES encryption. Since it is based on the free version of SQLite, the main encryption interface is the same as SQLite, but of course it also adds some own interfaces, such as setting the secret key for the database when creating and opening the database.
FMDB is an open source class library, it has a very good sqlite database operation package, but also increased support for sqlcipher, that is, we do not directly use sqlcihper can also complete encryption and decryption operations, and FMDB in the operation of sqlite much more convenient. If the current APP development involves database operations, FMDB is basically the first choice.
The following content is mainly aimed at some in the early stage ignored database privacy, and to the middle of the need to upgrade the database encryption of the App simple scheme introduction and code implementation. If the reader does not use FMDB, directly using sqlite, then this article also has a certain reference, only about the configuration of SQLCipher, and did not introduce, because although FDMB encryption is also using SQLCipher, but do not need to be configured.
Version of FMDB with encryption
Encrypted FMDB is actually a branch, that is, if you need to replace FMDB. The installation of this branch on Github only provides the installation method for cocopod.
Address of FMDB on Github: github.com/ccgus/fmdb
For example, if you used to write
1pod 'FMDB'
If you want to switch to encryption, change to encryption
1pod 'FMDB/SQLCipher'
Upgrade Database Code Implementation
Once we have the correct version, we need to do something in the code to upgrade the old database. The upgrade here includes two points:
1 is what we said earlier, encrypt the database.
Migrate tables and data from the old database to the new database.
The use of sqlcipher is not much different from sqlite. It is worth noting that every time [dp open] succeeds, you need to assign a password to the database, that is,[db setKey:DB_SECRETKEY]. DB_SECRETKEY is one of our custom macros. After that, we can read the contents of the database.
1234567FMDatabase *_db = [FMDatabase databaseWithPath:[cachePath stringByAppendingString:dbFileName]];if (! [_db open]) { _db = nil; return;}else{ [_db setKey:DB_SECRETKEY];}
Then we need to determine whether the database needs to be upgraded. When using sqlchipher to open the source database generated with sqlite,[dp goodConnection] is NO, even if the above [db open] operation is YES.
123if(! [_db goodConnection]){ //invalid connection [self upgradeDatabase:dbpath];}
Next, it is data migration, path is our source database path, assuming that our source database name is DBName, changeDatabasePath means that our database A is renamed DBName.tmp, and then it is to create a new database B, because A has been renamed DBName.tmp before, and B is the database we want to replace A in the future, so the name of B here is DBName. Finally, we copied the DBName.tmp data into DBName, deleted DBName.tmp, and our database upgrade was complete.
After the above steps, we know that although sqlchipher is based on sqlite, it is still different. We cannot directly upgrade sqlite database to sqlchipher. We can only use sqlchipher to create a new database and then rewrite the data.
The above code is just an example, the data storage model of each App is different, and I can't give a template here. Although the code is not necessarily applicable, when upgrading the database, the order of code execution is positive: open the database open -> set the secret key setkey -> view the connection goodConnection -> new database and migrate the data upgrade.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.