-
Notifications
You must be signed in to change notification settings - Fork 453
/
Sha1Id.cs
206 lines (175 loc) · 6.84 KB
/
Sha1Id.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Runtime.InteropServices;
namespace GVFS.Common.Git
{
[StructLayout(LayoutKind.Explicit, Size = ShaBufferLength, Pack = 1)]
public struct Sha1Id
{
public static readonly Sha1Id None = new Sha1Id();
private const int ShaBufferLength = (2 * sizeof(ulong)) + sizeof(uint);
private const int ShaStringLength = 2 * ShaBufferLength;
[FieldOffset(0)]
private ulong shaBytes1Through8;
[FieldOffset(8)]
private ulong shaBytes9Through16;
[FieldOffset(16)]
private uint shaBytes17Through20;
public Sha1Id(ulong shaBytes1Through8, ulong shaBytes9Through16, uint shaBytes17Through20)
{
this.shaBytes1Through8 = shaBytes1Through8;
this.shaBytes9Through16 = shaBytes9Through16;
this.shaBytes17Through20 = shaBytes17Through20;
}
public Sha1Id(string sha)
{
if (sha == null)
{
throw new ArgumentNullException(nameof(sha));
}
if (sha.Length != ShaStringLength)
{
throw new ArgumentException($"Must be length {ShaStringLength}", nameof(sha));
}
this.shaBytes1Through8 = ShaSubStringToULong(sha.Substring(0, 16));
this.shaBytes9Through16 = ShaSubStringToULong(sha.Substring(16, 16));
this.shaBytes17Through20 = ShaSubStringToUInt(sha.Substring(32, 8));
}
public static bool TryParse(string sha, out Sha1Id sha1, out string error)
{
error = null;
try
{
sha1 = new Sha1Id(sha);
return true;
}
catch (Exception e)
{
error = e.Message;
}
sha1 = new Sha1Id(0, 0, 0);
return false;
}
public static void ShaBufferToParts(
byte[] shaBuffer,
out ulong shaBytes1Through8,
out ulong shaBytes9Through16,
out uint shaBytes17Through20)
{
if (shaBuffer == null)
{
throw new ArgumentNullException(nameof(shaBuffer));
}
if (shaBuffer.Length != ShaBufferLength)
{
throw new ArgumentException($"Must be length {ShaBufferLength}", nameof(shaBuffer));
}
unsafe
{
fixed (byte* firstChunk = &shaBuffer[0], secondChunk = &shaBuffer[sizeof(ulong)], thirdChunk = &shaBuffer[sizeof(ulong) * 2])
{
shaBytes1Through8 = *(ulong*)firstChunk;
shaBytes9Through16 = *(ulong*)secondChunk;
shaBytes17Through20 = *(uint*)thirdChunk;
}
}
}
public void ToBuffer(byte[] shaBuffer)
{
unsafe
{
fixed (byte* firstChunk = &shaBuffer[0], secondChunk = &shaBuffer[sizeof(ulong)], thirdChunk = &shaBuffer[sizeof(ulong) * 2])
{
*(ulong*)firstChunk = this.shaBytes1Through8;
*(ulong*)secondChunk = this.shaBytes9Through16;
*(uint*)thirdChunk = this.shaBytes17Through20;
}
}
}
public override string ToString()
{
char[] shaString = new char[ShaStringLength];
BytesToCharArray(shaString, 0, this.shaBytes1Through8, sizeof(ulong));
BytesToCharArray(shaString, 2 * sizeof(ulong), this.shaBytes9Through16, sizeof(ulong));
BytesToCharArray(shaString, 2 * (2 * sizeof(ulong)), this.shaBytes17Through20, sizeof(uint));
return new string(shaString, 0, shaString.Length);
}
private static void BytesToCharArray(char[] shaString, int startIndex, ulong shaBytes, int numBytes)
{
byte b;
int firstArrayIndex;
for (int i = 0; i < numBytes; ++i)
{
b = (byte)(shaBytes >> (i * 8));
firstArrayIndex = startIndex + (i * 2);
shaString[firstArrayIndex] = GetHexValue(b / 16);
shaString[firstArrayIndex + 1] = GetHexValue(b % 16);
}
}
private static ulong ShaSubStringToULong(string shaSubString)
{
if (shaSubString == null)
{
throw new ArgumentNullException(nameof(shaSubString));
}
if (shaSubString.Length != sizeof(ulong) * 2)
{
throw new ArgumentException($"Must be length {sizeof(ulong) * 2}", nameof(shaSubString));
}
ulong bytes = 0;
string upperCaseSha = shaSubString.ToUpper();
int stringIndex = 0;
for (int i = 0; i < sizeof(ulong); ++i)
{
stringIndex = i * 2;
char firstChar = shaSubString[stringIndex];
char secondChar = shaSubString[stringIndex + 1];
byte nextByte = (byte)(CharToByte(firstChar) << 4 | CharToByte(secondChar));
bytes = bytes | ((ulong)nextByte << (i * 8));
}
return bytes;
}
private static uint ShaSubStringToUInt(string shaSubString)
{
if (shaSubString == null)
{
throw new ArgumentNullException(nameof(shaSubString));
}
if (shaSubString.Length != sizeof(uint) * 2)
{
throw new ArgumentException($"Must be length {sizeof(uint) * 2}", nameof(shaSubString));
}
uint bytes = 0;
string upperCaseSha = shaSubString.ToUpper();
int stringIndex = 0;
for (int i = 0; i < sizeof(uint); ++i)
{
stringIndex = i * 2;
char firstChar = shaSubString[stringIndex];
char secondChar = shaSubString[stringIndex + 1];
byte nextByte = (byte)(CharToByte(firstChar) << 4 | CharToByte(secondChar));
bytes = bytes | ((uint)nextByte << (i * 8));
}
return bytes;
}
private static char GetHexValue(int i)
{
if (i < 10)
{
return (char)(i + '0');
}
return (char)(i - 10 + 'A');
}
private static byte CharToByte(char c)
{
if (c >= '0' && c <= '9')
{
return (byte)(c - '0');
}
if (c >= 'A' && c <= 'F')
{
return (byte)(10 + (c - 'A'));
}
throw new ArgumentException($"Invalid character {c}", nameof(c));
}
}
}