forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CertificateSsh.cs
97 lines (82 loc) · 2.64 KB
/
CertificateSsh.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Runtime.InteropServices;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
/// <summary>
/// This class represents the hostkey which is avaiable when connecting to a SSH host.
/// </summary>
public class CertificateSsh : Certificate
{
/// <summary>
/// For mocking purposes
/// </summary>
protected CertificateSsh()
{ }
/// <summary>
/// The MD5 hash of the host. Meaningful if <see cref="HasMD5"/> is true
/// </summary>
public readonly byte[] HashMD5;
/// <summary>
/// The SHA1 hash of the host. Meaningful if <see cref="HasSHA1"/> is true
/// </summary>
public readonly byte[] HashSHA1;
/// <summary>
/// True if we have the MD5 hostkey hash from the server
/// </summary>
public readonly bool HasMD5;
/// <summary>
/// True if we have the SHA1 hostkey hash from the server
/// </summary>
public readonly bool HasSHA1;
internal unsafe CertificateSsh(git_certificate_ssh* cert)
{
HasMD5 = cert->type.HasFlag(GitCertificateSshType.MD5);
HasSHA1 = cert->type.HasFlag(GitCertificateSshType.SHA1);
HashMD5 = new byte[16];
for (var i = 0; i < HashMD5.Length; i++)
{
HashMD5[i] = cert->HashMD5[i];
}
HashSHA1 = new byte[20];
for (var i = 0; i < HashSHA1.Length; i++)
{
HashSHA1[i] = cert->HashSHA1[i];
}
}
internal unsafe IntPtr ToPointer()
{
GitCertificateSshType sshCertType = 0;
if (HasMD5)
{
sshCertType |= GitCertificateSshType.MD5;
}
if (HasSHA1)
{
sshCertType |= GitCertificateSshType.SHA1;
}
var gitCert = new git_certificate_ssh()
{
cert_type = GitCertificateType.Hostkey,
type = sshCertType,
};
fixed (byte *p = &HashMD5[0])
{
for (var i = 0; i < HashMD5.Length; i++)
{
gitCert.HashMD5[i] = p[i];
}
}
fixed (byte *p = &HashSHA1[0])
{
for (var i = 0; i < HashSHA1.Length; i++)
{
gitCert.HashSHA1[i] = p[i];
}
}
var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(gitCert));
Marshal.StructureToPtr(gitCert, ptr, false);
return ptr;
}
}
}