thieumao

MaHoaSQLiteScript - SQLiteKit Unity Encrypt Demo

Aug 3rd, 2015
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.70 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System;
  5. using System.IO;
  6. using System.Threading;
  7. using System.Diagnostics;
  8.  
  9. public class MaHoaSQLiteScript : MonoBehaviour {
  10.  
  11.     private SQLiteDB db = null;
  12.     private string log;
  13.     //private string queryDelete = "DROP TABLE IF EXISTS test_values;";
  14.     private string queryCreate = "CREATE TABLE IF NOT EXISTS test_values (id INTEGER PRIMARY KEY, str_field TEXT, blob_field BLOB);";
  15.     private string queryInsert = "INSERT INTO test_values (str_field,blob_field) VALUES(?,?);";
  16.     private string querySelect = "SELECT * FROM test_values;";
  17.     private string testString = "1231 \n\r \t weqw";
  18.     private byte[] testBlob = new byte[] {2,3,5,78,98,21,32,255};
  19.  
  20.     Text View;
  21.  
  22.     // Use this for initialization
  23.     void Start () {
  24.         View = GameObject.Find ("View").GetComponent<Text>();
  25.     }
  26.    
  27.     // Update is called once per frame
  28.     void Update () {
  29.    
  30.     }
  31.  
  32.     void OnGUI(){
  33.         //#if ENCRYPTION
  34.         //
  35.         // ENCRYPTION
  36.         //
  37.        
  38.         if ( GUI.Button(new Rect (170,170,150,70), "Run SQLite\nfor ENCRYPTION scenario") )
  39.         {
  40.             print("Thieu Mao");
  41.             try {
  42.                
  43.                 db = new SQLiteDB();
  44.                
  45.                 log = "";
  46.                
  47.                 // a product persistant database path.
  48.                 string filename = Application.persistentDataPath + "/demo_as_sqlcipher.db";
  49.                 print("filename " + filename);
  50.                 View.text = filename;
  51.  
  52.                 File.Delete(filename);
  53.                
  54.                 db.Open(filename);
  55.                
  56.                
  57.                 //
  58.                 // set ENCRYPTION
  59.                 //
  60.                 SQLiteQuery qr = new SQLiteQuery(db,"PRAGMA hexkey=\"0x0102030405060708090a0b0c0d0e0f10\";");
  61.                 qr.Step();
  62.                 qr.Release();
  63.                
  64.                 //
  65.                 // create table
  66.                 //
  67.                 qr = new SQLiteQuery(db, queryCreate);
  68.                 qr.Step();                                             
  69.                 qr.Release();                                        log += "\nTable created.";
  70.                
  71.                 //
  72.                 // insert string and blob
  73.                 //
  74.                 qr = new SQLiteQuery(db, queryInsert);
  75.                 qr.Bind(testString);
  76.                 qr.Bind(testBlob);
  77.                 qr.Step();
  78.                 qr.Release();                                        log += "\nInsert test string and blob.";
  79.                
  80.                
  81.                 // close
  82.                 db.Close();
  83.                
  84.                 // and open again
  85.                 db.Open(filename);
  86.                
  87.                 //
  88.                 // set ENCRYPTION AGAIN, you could  try change to see ENCRYPTION works
  89.                 //
  90.                 qr = new SQLiteQuery(db,"PRAGMA hexkey=\"0x0102030405060708090a0b0c0d0e0f10\";");
  91.                 qr.Step();
  92.                 qr.Release();
  93.                
  94.                 // do test
  95.                 Test2( db, ref log );
  96.                
  97.             } catch (Exception e){
  98.                 log +=  "\nTest Fail with Exception " + e.ToString();
  99.                 log +=  "\n on WebPlayer it must give an exception, it's normal.";
  100.             }
  101.         }
  102.         //#endif
  103.     }
  104.  
  105.     void Test2( SQLiteDB db, ref string log )
  106.     {
  107.         SQLiteQuery qr;
  108.        
  109.         //
  110.         // read strings
  111.         //
  112.         string testStringFromSelect = "";
  113.         qr = new SQLiteQuery(db, querySelect);
  114.         while( qr.Step() )
  115.         {
  116.             testStringFromSelect = qr.GetString("str_field");
  117.             if( testStringFromSelect != testString )
  118.             {
  119.                 throw new Exception( "Test string are not equal!" );
  120.             }
  121.            
  122.             byte[] testBlobFromSelect = qr.GetBlob("blob_field");
  123.            
  124.             if( testBlobFromSelect.Length != testBlob.Length )
  125.             {
  126.                 throw new Exception( "Test blobs are not equal!" );
  127.             }
  128.            
  129.             for (int i = 0; i < testBlobFromSelect.Length; i++)
  130.             {
  131.                 if( testBlobFromSelect[i] != testBlob[i] )
  132.                 {
  133.                     throw new Exception( "Test blobs are not equal!" );
  134.                 }
  135.             }
  136.         }
  137.         if( testStringFromSelect == "" )
  138.         {
  139.             throw new Exception( "Unknowm problem!" );
  140.         }
  141.         qr.Release();                                        log += "\nRead and test strings and blobs.";
  142.        
  143.         //
  144.         // if we reach that point it's mean we pass the test!
  145.         db.Close();                                           log += "\nDatabase closed!\nTest succeeded!";
  146.        
  147.     }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment