test.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * test.c
  3. *
  4. * Copyright (C) 2018 bzt (bztsrc@gitlab)
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * @brief Test a database encrypted by sqlite3aes
  27. *
  28. * Compile with
  29. * gcc -ldl -lpthread test.c -o test
  30. *
  31. * Or if you want OpenSSL libcrypto,
  32. * gcc -ldl -lpthread -lcrypto test.c -o test
  33. *
  34. */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. /**
  38. * Add these includes if you want to use OpenSSL's hash and cipher.
  39. */
  40. /*
  41. #include <openssl/sha.h>
  42. #include <openssl/aes.h>
  43. */
  44. /**
  45. * Get SQLite3 and our encryption codec implementation
  46. */
  47. #define SQLITE3AES_SALT "MyTestApplication"
  48. #include "sqlite3aes.h"
  49. /**
  50. * Main function
  51. */
  52. int main(int argc, char **argv)
  53. {
  54. char *errstr=NULL, *password="My Very Secret Passphrase";
  55. sqlite3 *db=NULL;
  56. sqlite3_stmt *s;
  57. int rc;
  58. /* open the database, create a table and put some data in it */
  59. rc=sqlite3_open("testenc.db", &db);
  60. if(rc!=SQLITE_OK) {
  61. fprintf(stderr, "Unable to open database. rc=%d\n",rc);
  62. return 1;
  63. }
  64. rc=sqlite3_key_v2(db, NULL, password, strlen(password));
  65. if(rc!=SQLITE_OK) {
  66. fprintf(stderr, "Unable to set up encryption. rc=%d\n",rc);
  67. return 1;
  68. }
  69. rc=sqlite3_exec(db, "CREATE TABLE test (ID VARCHAR NOT NULL UNIQUE PRIMARY KEY,"
  70. "Name VARCHAR NOT NULL DEFAULT '');", 0, 0, &errstr);
  71. if(rc!=SQLITE_OK) {
  72. fprintf(stderr, "Unable to create table. rc=%d msg=%s\n",rc,errstr);
  73. return 1;
  74. }
  75. rc=sqlite3_exec(db, "INSERT INTO test (ID,Name) VALUES ('dog','Dog')", 0, 0, &errstr);
  76. if(rc!=SQLITE_OK) {
  77. fprintf(stderr, "Unable to insert to table. rc=%d msg=%s\n",rc,errstr);
  78. return 1;
  79. }
  80. rc=sqlite3_exec(db, "INSERT INTO test (ID,Name) VALUES ('cat','Cat')", 0, 0, &errstr);
  81. if(rc!=SQLITE_OK) {
  82. fprintf(stderr, "Unable to insert to table. rc=%d msg=%s\n",rc,errstr);
  83. return 1;
  84. }
  85. sqlite3_close(db);
  86. db=NULL;
  87. printf("Records written to table 'test' successfully.\n");
  88. /* now open database again and try to read records back */
  89. rc=sqlite3_open("testenc.db", &db);
  90. if(rc!=SQLITE_OK) {
  91. fprintf(stderr, "Unable to open database. rc=%d\n",rc);
  92. return 1;
  93. }
  94. rc=sqlite3_key_v2(db, NULL, password, strlen(password));
  95. if(rc!=SQLITE_OK) {
  96. fprintf(stderr, "Unable to set up encryption. rc=%d\n",rc);
  97. return 1;
  98. }
  99. rc=sqlite3_prepare_v2(db, "SELECT Name FROM test WHERE ID='dog'", -1, &s, NULL);
  100. if(rc!=SQLITE_OK) {
  101. fprintf(stderr, "Unable to select from database. rc=%d\n",rc);
  102. return 1;
  103. }
  104. rc=sqlite3_step(s);
  105. if(rc!=SQLITE_ROW || strcmp((char*)sqlite3_column_text(s, 0), "Dog")) {
  106. fprintf(stderr, "Unable to read record from database. rc=%d\n",rc);
  107. return 1;
  108. }
  109. sqlite3_finalize(s);
  110. sqlite3_close(db);
  111. printf("Records read back from 'test' successfully.\n");
  112. return 0;
  113. }