forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPKCEUtil.cs
More file actions
70 lines (61 loc) · 2.13 KB
/
PKCEUtil.cs
File metadata and controls
70 lines (61 loc) · 2.13 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
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
69
70
using System;
using System.Security.Cryptography;
using System.Text;
namespace SpotifyAPI.Web
{
public static class PKCEUtil
{
private const int VERIFY_MIN_LENGTH = 43;
private const int VERIFY_MAX_LENGTH = 128;
private const int VERIFY_DEFAULT_LENGTH = 100;
/// <summary>
/// Generate a verifier and challenge pair using RNGCryptoServiceProvider
/// </summary>
/// <param name="length">The length of the generated verifier</param>
/// <returns></returns>
public static (string verifier, string challenge) GenerateCodes(int length = VERIFY_DEFAULT_LENGTH)
{
if (length < VERIFY_MIN_LENGTH || length > VERIFY_MAX_LENGTH)
{
throw new ArgumentException(
$"length must be between {VERIFY_MIN_LENGTH} and {VERIFY_MAX_LENGTH}",
nameof(length)
);
}
var verifier = GenerateRandomURLSafeString(length);
return GenerateCodes(verifier);
}
/// <summary>
/// Return the paseed verifier and its challenge
/// </summary>
/// <param name="verifier">A secure random generated verifier</param>
/// <returns></returns>
public static (string verifier, string challenge) GenerateCodes(string verifier)
{
Ensure.ArgumentNotNull(verifier, nameof(verifier));
if (verifier.Length < VERIFY_MIN_LENGTH || verifier.Length > VERIFY_MAX_LENGTH)
{
throw new ArgumentException(
$"length must be between {VERIFY_MIN_LENGTH} and {VERIFY_MAX_LENGTH}",
nameof(verifier)
);
}
var challenge = Base64Util.UrlEncode(ComputeSHA256(verifier));
return (verifier, challenge);
}
private static string GenerateRandomURLSafeString(int length)
{
var bit_count = length * 6;
var byte_count = (bit_count + 7) / 8; // rounded up
var bytes = new byte[byte_count];
using var generator = RandomNumberGenerator.Create();
generator.GetBytes(bytes);
return Base64Util.UrlEncode(bytes);
}
private static byte[] ComputeSHA256(string value)
{
using var hash = SHA256.Create();
return hash.ComputeHash(Encoding.UTF8.GetBytes(value));
}
}
}