-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ModelExtensions.cs
52 lines (45 loc) · 1.75 KB
/
ModelExtensions.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
using System.Text.RegularExpressions;
namespace Octokit
{
// TODO: this is only related to SSH keys, we should rename this
/// <summary>
/// Extensions for working with SSH keys
/// </summary>
public static class ModelExtensions
{
#if NETFX_CORE
static readonly Regex sshKeyRegex = new Regex(@"ssh-[rd]s[as] (?<data>\S+) ?(?<name>.*)$");
#else
static readonly Regex sshKeyRegex = new Regex(@"ssh-[rd]s[as] (?<data>\S+) ?(?<name>.*)$", RegexOptions.Compiled);
#endif
/// <summary>
/// Extract SSH key information from the API response
/// </summary>
/// <param name="sshKey">Key details received from API</param>
public static SshKeyInfo GetKeyDataAndName(this SshKey sshKey)
{
Ensure.ArgumentNotNull(sshKey, "sshKey");
var key = sshKey.Key;
if (key == null) return null;
var match = sshKeyRegex.Match(key);
return (match.Success ? new SshKeyInfo(match.Groups["data"].Value, match.Groups["name"].Value) : null);
}
/// <summary>
/// Compare two SSH keys to see if they are equal
/// </summary>
/// <param name="key">Reference SSH key</param>
/// <param name="otherKey">Key to compare</param>
public static bool HasSameDataAs(this SshKey key, SshKey otherKey)
{
Ensure.ArgumentNotNull(key, "key");
if (otherKey == null) return false;
var keyData = key.GetKeyData();
return keyData != null && keyData == otherKey.GetKeyData();
}
static string GetKeyData(this SshKey key)
{
var keyInfo = key.GetKeyDataAndName();
return keyInfo == null ? null : keyInfo.Data;
}
}
}