aboutsummaryrefslogtreecommitdiff
path: root/VPNAuth.Server/PkceUtils.cs
blob: 2299685037bdb5f8f706f171cba94bd8d2e3fc5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System.Security.Cryptography;

namespace VPNAuth.Server;

public static class PkceUtils
{
    private static string _codeChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
    private static string _tokenChars = _codeChars + ".!$";

    private static string GenerateRandomString(string availableChars, int length)
    {
        string randomString = "";
        for (int i = 0; i < length; i++)
            randomString +=
                availableChars[RandomNumberGenerator.GetInt32(availableChars.Length)];
        return randomString;
    }

    public static string GenerateCode(int length = 10)
        => GenerateRandomString(_codeChars, length);

    public static string GenerateToken(int length = 20)
        => GenerateRandomString(_tokenChars, length);
}