1
0
mirror of https://github.com/bitwarden/directory-connector synced 2025-12-13 14:53:16 +00:00
Files
directory-connector/src/Core/Models/EncryptedData.cs
2017-10-24 17:13:56 -04:00

61 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Bit.Core.Models
{
public class EncryptedData
{
public EncryptedData() { }
public EncryptedData(byte[] plainValue)
{
IV = RandomBytes();
#if NET461
Value = ProtectedData.Protect(plainValue, IV, DataProtectionScope.LocalMachine);
#endif
}
public EncryptedData(string plainValue)
{
var bytes = Encoding.UTF8.GetBytes(plainValue);
IV = RandomBytes();
#if NET461
Value = ProtectedData.Protect(bytes, IV, DataProtectionScope.LocalMachine);
#endif
}
public byte[] Value { get; set; }
public byte[] IV { get; set; }
public byte[] Decrypt()
{
#if NET461
return ProtectedData.Unprotect(Value, IV, DataProtectionScope.LocalMachine);
#else
return new byte[0];
#endif
}
public string DecryptToString()
{
#if NET461
var bytes = ProtectedData.Unprotect(Value, IV, DataProtectionScope.LocalMachine);
#else
var bytes = new byte[0];
#endif
return Encoding.UTF8.GetString(bytes);
}
private byte[] RandomBytes()
{
var entropy = new byte[16];
new RNGCryptoServiceProvider().GetBytes(entropy);
return entropy;
}
}
}