11//
2- // Copyright (c) 2009-2017 Krueger Systems, Inc.
2+ // Copyright (c) 2009-2018 Krueger Systems, Inc.
33//
44// Permission is hereby granted, free of charge, to any person obtaining a copy
55// of this software and associated documentation files (the "Software"), to deal
@@ -228,8 +228,11 @@ static SQLiteConnection ()
228228 /// If you use DateTimeOffset properties, it will be always stored as ticks regardingless
229229 /// the storeDateTimeAsTicks parameter.
230230 /// </param>
231- public SQLiteConnection ( string databasePath , bool storeDateTimeAsTicks = true )
232- : this ( databasePath , SQLiteOpenFlags . ReadWrite | SQLiteOpenFlags . Create , storeDateTimeAsTicks )
231+ /// <param name="key">
232+ /// Specifies the encryption key to use on the database. Should be a string or a byte[].
233+ /// </param>
234+ public SQLiteConnection ( string databasePath , bool storeDateTimeAsTicks = true , object key = null )
235+ : this ( databasePath , SQLiteOpenFlags . ReadWrite | SQLiteOpenFlags . Create , storeDateTimeAsTicks , key : key )
233236 {
234237 }
235238
@@ -250,7 +253,10 @@ public SQLiteConnection (string databasePath, bool storeDateTimeAsTicks = true)
250253 /// If you use DateTimeOffset properties, it will be always stored as ticks regardingless
251254 /// the storeDateTimeAsTicks parameter.
252255 /// </param>
253- public SQLiteConnection ( string databasePath , SQLiteOpenFlags openFlags , bool storeDateTimeAsTicks = true )
256+ /// <param name="key">
257+ /// Specifies the encryption key to use on the database. Should be a string or a byte[].
258+ /// </param>
259+ public SQLiteConnection ( string databasePath , SQLiteOpenFlags openFlags , bool storeDateTimeAsTicks = true , object key = null )
254260 {
255261 if ( databasePath == null )
256262 throw new ArgumentException ( "Must be specified" , nameof ( databasePath ) ) ;
@@ -284,10 +290,20 @@ public SQLiteConnection (string databasePath, SQLiteOpenFlags openFlags, bool st
284290 StoreDateTimeAsTicks = storeDateTimeAsTicks ;
285291
286292 BusyTimeout = TimeSpan . FromSeconds ( 0.1 ) ;
293+ Tracer = line => Debug . WriteLine ( line ) ;
294+
295+ if ( key is string stringKey ) {
296+ SetKey ( stringKey ) ;
297+ }
298+ else if ( key is byte [ ] bytesKey ) {
299+ SetKey ( bytesKey ) ;
300+ }
301+ else if ( key != null ) {
302+ throw new ArgumentException ( "Encryption keys must be strings or byte arrays" , nameof ( key ) ) ;
303+ }
287304 if ( openFlags . HasFlag ( SQLiteOpenFlags . ReadWrite ) ) {
288305 ExecuteScalar < string > ( "PRAGMA journal_mode=WAL" ) ;
289306 }
290- Tracer = line => Debug . WriteLine ( line ) ;
291307 }
292308
293309 /// <summary>
@@ -304,13 +320,13 @@ static string Quote (string unsafeString)
304320 }
305321
306322 /// <summary>
307- /// Sets the key used to encrypt/decrypt the database.
323+ /// Sets the key used to encrypt/decrypt the database with "pragma key = ..." .
308324 /// This must be the first thing you call before doing anything else with this connection
309325 /// if your database is encrypted.
310326 /// This only has an effect if you are using the SQLCipher nuget package.
311327 /// </summary>
312328 /// <param name="key">Ecryption key plain text that is converted to the real encryption key using PBKDF2 key derivation</param>
313- public void SetKey ( string key )
329+ void SetKey ( string key )
314330 {
315331 if ( key == null ) throw new ArgumentNullException ( nameof ( key ) ) ;
316332 var q = Quote ( key ) ;
@@ -324,7 +340,7 @@ public void SetKey (string key)
324340 /// This only has an effect if you are using the SQLCipher nuget package.
325341 /// </summary>
326342 /// <param name="key">256-bit (32 byte) ecryption key data</param>
327- public void SetKey ( byte [ ] key )
343+ void SetKey ( byte [ ] key )
328344 {
329345 if ( key == null ) throw new ArgumentNullException ( nameof ( key ) ) ;
330346 if ( key . Length != 32 ) throw new ArgumentException ( "Key must be 32 bytes (256-bit)" , nameof ( key ) ) ;
@@ -2021,6 +2037,7 @@ public class SQLiteConnectionString
20212037 public string ConnectionString { get ; private set ; }
20222038 public string DatabasePath { get ; private set ; }
20232039 public bool StoreDateTimeAsTicks { get ; private set ; }
2040+ public object Key { get ; private set ; }
20242041
20252042#if NETFX_CORE
20262043 static readonly string MetroStyleDataPath = Windows . Storage . ApplicationData . Current . LocalFolder . Path ;
@@ -2038,10 +2055,11 @@ public static bool IsInMemoryPath(string databasePath)
20382055
20392056#endif
20402057
2041- public SQLiteConnectionString ( string databasePath , bool storeDateTimeAsTicks )
2058+ public SQLiteConnectionString ( string databasePath , bool storeDateTimeAsTicks , object key )
20422059 {
20432060 ConnectionString = databasePath ;
20442061 StoreDateTimeAsTicks = storeDateTimeAsTicks ;
2062+ Key = key ;
20452063
20462064#if NETFX_CORE
20472065 DatabasePath = IsInMemoryPath ( databasePath )
0 commit comments