SecurityExtensions
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace Relativity.Transfer
{
[DebuggerStepThrough]
internal static class SecurityExtensions
{
private const int DefaultBlockSize = 16;
public static IEnumerable<byte> ProtectData(this string value)
{
byte[] array = CreatePaddedBytes(value, 16).ToArray();
ProtectedMemory.Protect(array, GlobalSettings.Instance.MemoryProtectionScope);
return array;
}
public static string UnprotectData(this IEnumerable<byte> value)
{
if (value == null)
return string.Empty;
byte[] array = value.ToArray();
byte[] array2 = new byte[array.Length];
Array.Copy(array, array2, array.Length);
ProtectedMemory.Unprotect(array2, GlobalSettings.Instance.MemoryProtectionScope);
return RemovePadding(Encoding.ASCII.GetString(array2));
}
private static IEnumerable<byte> CreatePaddedBytes(string value, int blockSize)
{
if (string.IsNullOrEmpty(value))
return new byte[blockSize];
byte[] array = new byte[(value.Length / blockSize + 1) * blockSize];
Encoding.ASCII.GetBytes(value).CopyTo(array, 0);
return array.ToList();
}
private static string RemovePadding(string value)
{
if (string.IsNullOrEmpty(value))
return value;
int startIndex = value.IndexOf(' ');
return value.Remove(startIndex);
}
}
}