Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
KeyBlobHelpers.cs
Go to the documentation of this file.
3
5
6internal static class KeyBlobHelpers
7{
8 internal static byte[] ToUnsignedIntegerBytes(this ReadOnlyMemory<byte> memory, int length)
9 {
10 if (memory.Length == length)
11 {
12 return memory.ToArray();
13 }
14 ReadOnlySpan<byte> span = memory.Span;
15 if (memory.Length == length + 1 && span[0] == 0)
16 {
17 return span.Slice(1).ToArray();
18 }
19 if (span.Length > length)
20 {
22 }
23 byte[] array = new byte[length];
24 span.CopyTo(array.AsSpan(length - span.Length));
25 return array;
26 }
27
28 internal static byte[] ToUnsignedIntegerBytes(this ReadOnlyMemory<byte> memory)
29 {
30 ReadOnlySpan<byte> span = memory.Span;
31 if (span.Length > 1 && span[0] == 0)
32 {
33 return span.Slice(1).ToArray();
34 }
35 return span.ToArray();
36 }
37
38 internal static byte[] ExportKeyParameter(this BigInteger value, int length)
39 {
40 byte[] array = new byte[length];
41 if (value.TryWriteBytes(array, out var bytesWritten, isUnsigned: true, isBigEndian: true))
42 {
43 if (bytesWritten < length)
44 {
45 Buffer.BlockCopy(array, 0, array, length - bytesWritten, bytesWritten);
46 array.AsSpan(0, length - bytesWritten).Clear();
47 }
48 return array;
49 }
51 }
52
53 internal static void WriteKeyParameterInteger(this AsnWriter writer, ReadOnlySpan<byte> integer)
54 {
55 if (integer[0] == 0)
56 {
57 int i;
58 for (i = 1; i < integer.Length; i++)
59 {
60 if (integer[i] >= 128)
61 {
62 i--;
63 break;
64 }
65 if (integer[i] != 0)
66 {
67 break;
68 }
69 }
70 if (i == integer.Length)
71 {
72 i--;
73 }
74 integer = integer.Slice(i);
75 }
76 writer.WriteIntegerUnsigned(integer);
77 }
78}
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
static string Cryptography_Der_Invalid_Encoding
Definition SR.cs:50
static string Cryptography_NotValidPublicOrPrivateKey
Definition SR.cs:122
Definition SR.cs:7
static byte[] ToUnsignedIntegerBytes(this ReadOnlyMemory< byte > memory)
static byte[] ExportKeyParameter(this BigInteger value, int length)
static void WriteKeyParameterInteger(this AsnWriter writer, ReadOnlySpan< byte > integer)
static byte[] ToUnsignedIntegerBytes(this ReadOnlyMemory< byte > memory, int length)
unsafe ReadOnlySpan< T > Span
void CopyTo(Span< T > destination)
ReadOnlySpan< T > Slice(int start)