Giter Club home page Giter Club logo

ghostmanexp / sqlite.net.cipher Goto Github PK

View Code? Open in Web Editor NEW

This project forked from has-taiar/sqlite.net.cipher

0.0 1.0 0.0 1.59 MB

An easy to use extension for SQLite.Net PCL that allows you to seamlessly encrypt/decrypt data when inserted/accessed from the database by adding one simple attribute. Works great on all major platforms (iOS, Android, Windows Universal)

Home Page: http://www.hasaltaiar.com.au/sqlite-net-cipher-secure-your-data-on-all-mobile-platforms-seamlessly-and-effortlessly/

License: MIT License

Batchfile 0.71% C# 99.29%

sqlite.net.cipher's Introduction

SQLite.Net.Cipher

An easy to use extension for SQLite.Net PCL that allows you to seamlessly encrypt/decrypt data when getting data in/out of the database. This library allows you to securely encrypt/decrypt group of your objects' properties simply by decorating these propertiese with one attribute (Secure). The library Works great one all major platforms (iOS, Android, Windows Universal, and .NET 4.0 onwards). There is a dependency on SQLite.Net PCL and PCLCrypto.
More details can be found here

License

This projected is licensed under the terms of the MIT license. See LICENSE.TXT

Code Sample

To use the library you need to subclass SecureDatabase and add the attribute [Secure] on the properties that you want to secure.

public class MyDatabase : SecureDatabase
{
	public MyDatabase(ISQLitePlatform platform, string dbfile): base(platform, dbfile)
	{
	}
		
	protected override void CreateTables()
	{
		CreateTable<SampleUser>();
	}
}

public class SampleUser : IModel
{
	public string Id { get; set; }
	public string Name { get; set; }
		
	[Secure] // This property will be encrypted when inserted to the database, and decrypted when read out of the db again.
	public string Password { get; set; }
		
	public string Bio { get; set; }
}

iOS

var dbFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "mysequredb.db3");
var platform = new SQLite.Net.Platform.XamarinIOS.SQLitePlatformIOS();
ISecureDatabase database = new MyDatabase(platform, dbFilePath);
var keySeed = "my very very secure key seed. This can be any string, with any size. You should use PCLCrypt strong random generator for this";

var user = new SampleUser()
{
	Name = "Has AlTaiar",
	Password = "very secure password :)",
	Bio = "Very cool guy :) ",
	Id = Guid.NewGuid().ToString()
};

// insert object to the database (it will encrypt all [Secure] properties)
var inserted = database.SecureInsert<SampleUser>(user, keySeed);

var newUser = database.SecureGet<SampleUser>(user.Id, keySeed);
Assert.AreEqual("very secure password :)", newUser.Password); // password will be decrypted seamlessly when acceed from db. 

Android

var dbFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "mysequredb.db3");
var platform = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
ISecureDatabase database = new MyDatabase(platform, dbFilePath);
var keySeed = "my very very secure key seed. This can be any string, with any size. You should use PCLCrypt strong random generator for this";

var user = new SampleUser()
{
	Name = "Has AlTaiar",
	Password = "very secure password :)",
	Bio = "Very cool guy :) ",
	Id = Guid.NewGuid().ToString()
};

// insert object to the database (it will encrypt all [Secure] properties)
var inserted = database.SecureInsert<SampleUser>(user, keySeed);

var newUser = database.SecureGet<SampleUser>(user.Id, keySeed);
Assert.AreEqual("very secure password :)", newUser.Password); // password will be decrypted seamlessly when acceed from db.

Windows Universal (and Other platform)

You could use the same code samples above for all other platforms. The only things to change, you database file path and this line:

var dbFilePath = "set-your-db-file-path";
var platform = new SQLite.Net.Platform.WhateverPlatformYouAreUsing.SQLitePlatformX();
ISecureDatabase database = new MyDatabase(platform, dbFilePath);

// the rest of the code samples above works the same way.

How-To use it?

To install, simply grab the package from Nuget

> Install-Package SQLite.Net.Cipher

Tips

  • Use Strong Random generators, like the one provided by PCLCrypto. Avoid using simple-to-guess passwords.
  • Avoid storing the encryption key on the device. If you have to do so, use KeyChain.Net to store your key in the device specialised database of keys.
  • SQLite.Net.Cipher allows you to have your implementation of CryptoService, which is used for encryption/decryption.
  • Your Data models (objects) need to implement IModel.

Limitations:

  1. The library assumes that your property values are using UTF-8 for encoding.
  2. The [Secure] property can only be used on properties of type string.
  3. Limited support to generic methods like Query(), and ExecuteScalar(), use CryptoService along with these methods to construct your own secure sql query.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.