-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathCryptoAlgorithmFactory.cs
68 lines (61 loc) · 2.36 KB
/
CryptoAlgorithmFactory.cs
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.IO;
using System.Security.Cryptography;
namespace Synercoding.FormsAuthentication.Encryption
{
// based upon: https://github.com/Microsoft/referencesource/blob/master/System.Web/Security/Cryptography/MachineKeyCryptoAlgorithmFactory.cs
internal sealed class CryptoAlgorithmFactory : ICryptoAlgorithmFactory
{
private readonly FormsAuthenticationOptions _options;
private Func<SymmetricAlgorithm> _encryptionAlgorithmFactory;
private Func<KeyedHashAlgorithm> _validationAlgorithmFactory;
public CryptoAlgorithmFactory(FormsAuthenticationOptions options)
{
_options = options;
}
public SymmetricAlgorithm GetEncryptionAlgorithm()
{
if (_encryptionAlgorithmFactory == null)
{
_encryptionAlgorithmFactory = GetEncryptionAlgorithmFactory();
}
return _encryptionAlgorithmFactory();
}
private Func<SymmetricAlgorithm> GetEncryptionAlgorithmFactory()
{
switch (_options.EncryptionMethod)
{
case EncryptionMethod.AES:
return CryptoAlgorithms.CreateAes;
case EncryptionMethod.TripleDES:
return CryptoAlgorithms.CreateTripleDES;
default:
throw new InvalidDataException();
}
}
public KeyedHashAlgorithm GetValidationAlgorithm()
{
if (_validationAlgorithmFactory == null)
{
_validationAlgorithmFactory = GetValidationAlgorithmFactory();
}
return _validationAlgorithmFactory();
}
private Func<KeyedHashAlgorithm> GetValidationAlgorithmFactory()
{
switch (_options.ValidationMethod)
{
case ValidationMethod.SHA1:
return CryptoAlgorithms.CreateHMACSHA1;
case ValidationMethod.HMACSHA256:
return CryptoAlgorithms.CreateHMACSHA256;
case ValidationMethod.HMACSHA384:
return CryptoAlgorithms.CreateHMACSHA384;
case ValidationMethod.HMACSHA512:
return CryptoAlgorithms.CreateHMACSHA512;
default:
throw new InvalidDataException();
}
}
}
}