fix return the wrong value
This commit is contained in:
@@ -11,20 +11,19 @@ namespace Pilz.Cryptography
|
|||||||
public class SecureString
|
public class SecureString
|
||||||
{
|
{
|
||||||
public static ICrypter DefaultCrypter { get; set; }
|
public static ICrypter DefaultCrypter { get; set; }
|
||||||
|
|
||||||
[JsonIgnore]
|
|
||||||
public ICrypter Crypter { get; set; }
|
public ICrypter Crypter { get; set; }
|
||||||
|
|
||||||
[JsonProperty]
|
|
||||||
public string EncryptedValue { get; set; }
|
public string EncryptedValue { get; set; }
|
||||||
|
|
||||||
[JsonIgnore]
|
|
||||||
public string Value
|
public string Value
|
||||||
{
|
{
|
||||||
get => GetCrypter().Decrypt(EncryptedValue);
|
get => GetCrypter()?.Decrypt(EncryptedValue);
|
||||||
set => EncryptedValue = GetCrypter().Encrypt(value);
|
set => EncryptedValue = GetCrypter().Encrypt(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[JsonConstructor]
|
||||||
|
private SecureString(JsonConstructorAttribute dummyAttribute)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public SecureString() :
|
public SecureString() :
|
||||||
this(string.Empty, true)
|
this(string.Empty, true)
|
||||||
{
|
{
|
||||||
@@ -73,7 +72,7 @@ namespace Pilz.Cryptography
|
|||||||
public static implicit operator string(SecureString value) => value?.Value;
|
public static implicit operator string(SecureString value) => value?.Value;
|
||||||
public static implicit operator SecureString(string value) => new SecureString(value, false);
|
public static implicit operator SecureString(string value) => new SecureString(value, false);
|
||||||
|
|
||||||
public static bool operator ==(SecureString left, SecureString right) => left.EncryptedValue == right.EncryptedValue;
|
public static bool operator ==(SecureString left, SecureString right) => left?.EncryptedValue == right?.EncryptedValue;
|
||||||
public static bool operator !=(SecureString left, SecureString right) => left.EncryptedValue != right.EncryptedValue;
|
public static bool operator !=(SecureString left, SecureString right) => left?.EncryptedValue != right?.EncryptedValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,12 +22,13 @@ namespace Pilz.Json.Converters
|
|||||||
var idString = serializer.Deserialize<string>(reader);
|
var idString = serializer.Deserialize<string>(reader);
|
||||||
SecureString id;
|
SecureString id;
|
||||||
|
|
||||||
if (existingValue is object)
|
if (existingValue is SecureString)
|
||||||
|
{
|
||||||
id = (SecureString)existingValue;
|
id = (SecureString)existingValue;
|
||||||
|
id.EncryptedValue = idString;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
id = new SecureString();
|
id = new SecureString(idString, true);
|
||||||
|
|
||||||
id.EncryptedValue = idString;
|
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,10 +35,13 @@ namespace Pilz.Cryptography
|
|||||||
byte[] bytes = TextEncoding.GetBytes(key);
|
byte[] bytes = TextEncoding.GetBytes(key);
|
||||||
byte[] array = sha1CryptoServiceProvider.ComputeHash(bytes);
|
byte[] array = sha1CryptoServiceProvider.ComputeHash(bytes);
|
||||||
|
|
||||||
var output = new byte[checked(length - 1 + 1)];
|
var output = new byte[length];
|
||||||
array.CopyTo(output, 0);
|
var lowerLength = Math.Min(array.Length, output.Length);
|
||||||
|
|
||||||
|
for (int i = 0; i < lowerLength; i++)
|
||||||
|
output[i] = array[i];
|
||||||
|
|
||||||
return array;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string EncryptData(string plaintext)
|
private string EncryptData(string plaintext)
|
||||||
@@ -63,7 +66,7 @@ namespace Pilz.Cryptography
|
|||||||
|
|
||||||
public string Encrypt(string plainValue)
|
public string Encrypt(string plainValue)
|
||||||
{
|
{
|
||||||
return EncryptData(plainValue);
|
return EncryptData(plainValue ?? string.Empty);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Decrypt(string encryptedValue)
|
public string Decrypt(string encryptedValue)
|
||||||
|
|||||||
Reference in New Issue
Block a user