forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachineKey45CryptoSystem.cs
More file actions
40 lines (34 loc) · 1.39 KB
/
Copy pathMachineKey45CryptoSystem.cs
File metadata and controls
40 lines (34 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Reflection;
using System.Web.Security;
namespace System.Web.Helpers.AntiXsrf
{
// Interfaces with the System.Web.MachineKey static class using the 4.5 Protect / Unprotect methods.
internal sealed class MachineKey45CryptoSystem : ICryptoSystem
{
private static readonly string[] _purposes = new string[] { "System.Web.Helpers.AntiXsrf.AntiForgeryToken.v1" };
private static readonly MachineKey45CryptoSystem _singletonInstance = GetSingletonInstance();
public static MachineKey45CryptoSystem Instance
{
get
{
return _singletonInstance;
}
}
private static MachineKey45CryptoSystem GetSingletonInstance()
{
return new MachineKey45CryptoSystem();
}
public string Protect(byte[] data)
{
byte[] rawProtectedBytes = MachineKey.Protect(data, _purposes);
return HttpServerUtility.UrlTokenEncode(rawProtectedBytes);
}
public byte[] Unprotect(string protectedData)
{
byte[] rawProtectedBytes = HttpServerUtility.UrlTokenDecode(protectedData);
return MachineKey.Unprotect(rawProtectedBytes, _purposes);
}
}
}