forked from Sustainsys/Saml2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRelayStateGenerator.cs
49 lines (44 loc) · 1.4 KB
/
RelayStateGenerator.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Sustainsys.Saml2
{
/// <summary>
/// Generator of secure random keys..
/// </summary>
static class SecureKeyGenerator
{
private static RNGCryptoServiceProvider random =
new RNGCryptoServiceProvider();
/// <summary>
/// Create a unique random string with a cryptographically secure
/// random function.
/// </summary>
/// <returns>Random string 56-chars string</returns>
public static string CreateRelayState()
{
// 16 is considered secure, but Base64 pads 16 bytes so
// use 18 to make it even with Base64 that encodes multiples
// of 3 bytes)
var bytes = new byte[18];
random.GetBytes(bytes);
return Convert.ToBase64String(bytes)
.Replace('/', '-')
.Replace('+', '_');
}
/// <summary>
/// Create a unique random array with a cryptographically secure
/// random function.
/// </summary>
/// <returns>20 random bytes.</returns>
public static byte[] CreateArtifactMessageHandle()
{
var bytes = new byte[20];
random.GetBytes(bytes);
return bytes;
}
}
}