|
| 1 | +# Secure Local Storage |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +Secure Local Storage is a simple package for .NET that allows you to safely store information locally, allowing you to select whether the information will only be decryptable on the computer where it was created, or if it can be shared between several computers. |
| 6 | + |
| 7 | +Mainly it offers you the option of storing information or sensitive settings and obtaining it from the same or another application. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +Use the nuget package manager to install [SecureLocalStorage.](https://www.nuget.org/packages/SecureLocalStorage/) |
| 12 | + |
| 13 | +_Package Manager:_ |
| 14 | +```csharp |
| 15 | +Install-Package SecureLocalStorage -Version 1.0.0 |
| 16 | +``` |
| 17 | +_.NET CLI:_ |
| 18 | +```csharp |
| 19 | +dotnet add package SecureLocalStorage --version 1.0.0 |
| 20 | +``` |
| 21 | + |
| 22 | +## Simple Usage |
| 23 | + |
| 24 | + |
| 25 | +### Instantiate the class with the default settings: |
| 26 | + |
| 27 | +```csharp |
| 28 | +var config = new DefaultLocalStorageConfig(); |
| 29 | +var storage = new SecureLocalStorage.SecureLocalStorage(config); |
| 30 | +``` |
| 31 | +### Add data to storage: |
| 32 | +```csharp |
| 33 | +storage.Set("Example", "Hello world."); |
| 34 | +//If key exists content will replaced. |
| 35 | +``` |
| 36 | +### Add custom data to storage: |
| 37 | +```csharp |
| 38 | +var userExample = new UserExample {Name = "Juan", Verified = true}; |
| 39 | +storage.Set("User", userExample); |
| 40 | +``` |
| 41 | +### Get data from storage: |
| 42 | +```csharp |
| 43 | +string data = storage.Get("Example"); //Hello world |
| 44 | +``` |
| 45 | +### Get custom data from storage: |
| 46 | +```csharp |
| 47 | +var data = storage.Get<UserExample>("User"); //{Name = "Juan", Verified = true} |
| 48 | +``` |
| 49 | +### Remove data from storage: |
| 50 | +```csharp |
| 51 | +storage.Remove("User"); |
| 52 | +``` |
| 53 | +### Check if data exists on storage |
| 54 | +```csharp |
| 55 | +var exists = storage.Exists("User"); |
| 56 | +``` |
| 57 | + |
| 58 | +## Advanced Usage |
| 59 | + |
| 60 | +By default, the information is encrypted with the values of the machine that runs the application and it is only possible to manipulate the data in the same environment, that is, information stored between devices cannot be stolen. |
| 61 | + |
| 62 | +Optionally you can configure where that information is stored and how it is encrypted. |
| 63 | + |
| 64 | +### Custom configuration |
| 65 | + |
| 66 | +The information is stored by default in `Environment.SpecialFolder.ApplicationData`. |
| 67 | + |
| 68 | +and its hierarchy is as follows: |
| 69 | + |
| 70 | +`%SpecialFolder% / Your_App_Name / default.` |
| 71 | + |
| 72 | +#### Change default app name: |
| 73 | + |
| 74 | +```csharp |
| 75 | +config = new CustomLocalStorageConfig(null, "DotnetsaferTesting").WithDefaultKeyBuilder(); |
| 76 | +``` |
| 77 | +Make sure the name is unique to your application, or it could create conflicts with other applications. |
| 78 | + |
| 79 | +#### Change default path: |
| 80 | + |
| 81 | +```csharp |
| 82 | +config = new CustomLocalStorageConfig(@"current/default", "DotnetsaferTesting").WithDefaultKeyBuilder(); |
| 83 | +``` |
| 84 | + |
| 85 | +#### Change default encryption key: |
| 86 | + |
| 87 | +If you don't want the encryption key to be unique values of the device, you can set one yourself. |
| 88 | +```csharp |
| 89 | +config = new CustomLocalStorageConfig(@"current/default", "DotnetsaferTesting", "secret1234"); |
| 90 | +``` |
| 91 | +**If you want to add a dynamic key you can override the `BuildLocalSecureKey` method:** |
| 92 | + |
| 93 | +```csharp |
| 94 | +config.BuildLocalSecureKey = () => Guid.NewGuid().ToString(); |
| 95 | +``` |
| 96 | + |
| 97 | +## Contributing |
| 98 | +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. |
| 99 | + |
| 100 | +Please make sure to update tests as appropriate. |
0 commit comments