Sonarr/src/Microsoft.AspNet.SignalR.Core/Infrastructure/DefaultProtectedData.cs
2013-11-21 21:26:57 -08:00

35 lines
1.3 KiB
C#

// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
using System;
using System.Security.Cryptography;
using System.Text;
namespace Microsoft.AspNet.SignalR.Infrastructure
{
public class DefaultProtectedData : IProtectedData
{
private static readonly UTF8Encoding _encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
public string Protect(string data, string purpose)
{
byte[] purposeBytes = _encoding.GetBytes(purpose);
byte[] unprotectedBytes = _encoding.GetBytes(data);
byte[] protectedBytes = ProtectedData.Protect(unprotectedBytes, purposeBytes, DataProtectionScope.CurrentUser);
return Convert.ToBase64String(protectedBytes);
}
public string Unprotect(string protectedValue, string purpose)
{
byte[] purposeBytes = _encoding.GetBytes(purpose);
byte[] protectedBytes = Convert.FromBase64String(protectedValue);
byte[] unprotectedBytes = ProtectedData.Unprotect(protectedBytes, purposeBytes, DataProtectionScope.CurrentUser);
return _encoding.GetString(unprotectedBytes);
}
}
}