/// <summary> /// Stores an object into the LocalStorage. /// </summary> /// <param name="key">Unique key, can be any string, used for retrieving it later.</param> /// <param name="instance"></param> public void Store <T>(string key, T instance) { if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException(nameof(key)); } if (instance == null) { throw new ArgumentNullException(nameof(instance)); } var value = JsonConvert.SerializeObject(instance); if (Storage.Keys.Contains(key)) { Storage.Remove(key); } if (_config.EnableEncryption) { value = CryptographyHelpers.Encrypt(_encryptionKey, _config.EncryptionSalt, value); } Storage.Add(key, value); }
public void Encryption_Should_Encrypt_String() { // arrange var key = Guid.NewGuid().ToString(); var salt = Guid.NewGuid().ToString(); var text = "lorem ipsum dom dolor sit amet"; // act var target = CryptographyHelpers.Encrypt(key, salt, text); // assert target.Should().NotBeNullOrEmpty(); target.Should().NotBe(text); }
public void Decrypt_Should_Decode_An_Encrypted_String_With_Special_Characters() { // arrange var key = Guid.NewGuid().ToString("N"); var salt = Guid.NewGuid().ToString("N"); var original_value = "Søm€ unicode s-tring+"; var encrypted_value = CryptographyHelpers.Encrypt(key, salt, original_value); // act var target = CryptographyHelpers.Decrypt(key, salt, encrypted_value); // assert target.Should().NotBeNullOrEmpty(); target.Should().Be(original_value); }
public void Decrypt_Should_Decode_An_Encrypted_String() { // arrange var key = Guid.NewGuid().ToString(); var salt = Guid.NewGuid().ToString(); var original_value = "lorem ipsum dom dolor sit amet"; var encrypted_value = CryptographyHelpers.Encrypt(key, salt, original_value); // act var target = CryptographyHelpers.Decrypt(key, salt, encrypted_value); // assert target.Should().NotBeNullOrEmpty(); target.Should().Be(original_value); }
/// <summary> /// Stores an object into the LocalStorage. /// </summary> /// <param name="key">Unique key, can be any string, used for retrieving it later.</param> /// <param name="instance"></param> public void Store <T>(string key, T instance, bool preserveInstReferences = false) { if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException(nameof(key)); } if (instance == null) { throw new ArgumentNullException(nameof(instance)); } string value = string.Empty; if (preserveInstReferences) { value = JsonConvert.SerializeObject(instance, new JsonSerializerSettings() { //PreserveReferencesHandling = PreserveReferencesHandling.Objects, ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); } else { value = JsonConvert.SerializeObject(instance); } if (value != string.Empty) { if (Storage.Keys.Contains(key)) { Storage.Remove(key); } if (_config.EnableEncryption) { value = CryptographyHelpers.Encrypt(_encryptionKey, _config.EncryptionSalt, value); } Storage.Add(key, value); } }