Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Helpers.cs
Go to the documentation of this file.
1using System;
8
10
11internal static class Helpers
12{
13 [UnsupportedOSPlatformGuard("android")]
14 public static bool IsRC2Supported => !OperatingSystem.IsAndroid();
15
16 public static bool UsesIv(this CipherMode cipherMode)
17 {
18 return cipherMode != CipherMode.ECB;
19 }
20
21 public static byte[] GetCipherIv(this CipherMode cipherMode, byte[] iv)
22 {
23 if (cipherMode.UsesIv())
24 {
25 if (iv == null)
26 {
28 }
29 return iv;
30 }
31 return null;
32 }
33
34 public static byte[] MapZeroLengthArrayToNonNullPointer(this byte[] src)
35 {
36 if (src != null && src.Length == 0)
37 {
38 return new byte[1];
39 }
40 return src;
41 }
42
44 {
45 string provider2 = provider.Provider;
47 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptOpenStorageProvider(out phProvider, provider2, 0);
48 if (errorCode != 0)
49 {
50 throw errorCode.ToCryptographicException();
51 }
52 return phProvider;
53 }
54
56 {
57 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptGetProperty(ncryptHandle, propertyName, null, 0, out var pcbResult, options);
58 switch (errorCode)
59 {
60 case global::Interop.NCrypt.ErrorCode.NTE_NOT_FOUND:
61 return null;
62 default:
63 throw errorCode.ToCryptographicException();
64 case global::Interop.NCrypt.ErrorCode.ERROR_SUCCESS:
65 {
66 byte[] array = new byte[pcbResult];
67 fixed (byte* pbOutput = array)
68 {
69 errorCode = global::Interop.NCrypt.NCryptGetProperty(ncryptHandle, propertyName, pbOutput, array.Length, out pcbResult, options);
70 }
71 switch (errorCode)
72 {
73 case global::Interop.NCrypt.ErrorCode.NTE_NOT_FOUND:
74 return null;
75 default:
76 throw errorCode.ToCryptographicException();
77 case global::Interop.NCrypt.ErrorCode.ERROR_SUCCESS:
78 Array.Resize(ref array, pcbResult);
79 return array;
80 }
81 }
82 }
83 }
84
86 {
87 byte[] property = ncryptHandle.GetProperty(propertyName, options);
88 if (property == null)
89 {
90 return null;
91 }
92 if (property.Length == 0)
93 {
94 return string.Empty;
95 }
96 fixed (byte* ptr = &property[0])
97 {
99 }
100 }
101
103 {
104 byte[] property = ncryptHandle.GetProperty(propertyName, options);
105 if (property == null)
106 {
107 return 0;
108 }
109 return BitConverter.ToInt32(property, 0);
110 }
111
113 {
114 Unsafe.SkipInit(out IntPtr intPtr);
115 int pcbResult;
116 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptGetProperty(ncryptHandle, propertyName, &intPtr, IntPtr.Size, out pcbResult, options);
117 return errorCode switch
118 {
119 global::Interop.NCrypt.ErrorCode.NTE_NOT_FOUND => IntPtr.Zero,
120 global::Interop.NCrypt.ErrorCode.ERROR_SUCCESS => intPtr,
121 _ => throw errorCode.ToCryptographicException(),
122 };
123 }
124
126 {
127 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptSetProperty(keyHandle, "Export Policy", &exportPolicy, 4, CngPropertyOptions.Persist);
128 if (errorCode != 0)
129 {
130 throw errorCode.ToCryptographicException();
131 }
132 }
133
134 public static int BitSizeToByteSize(this int bits)
135 {
136 return (bits + 7) / 8;
137 }
138
139 public static byte[] GenerateRandom(int count)
140 {
141 byte[] array = new byte[count];
143 return array;
144 }
145
146 [return: NotNullIfNotNull("src")]
147 public static byte[] CloneByteArray(this byte[] src)
148 {
149 if (src == null)
150 {
151 return null;
152 }
153 return (byte[])src.Clone();
154 }
155
157 {
158 return ((mode == CipherMode.CFB) ? feedbackSizeInBits : algorithm.BlockSize) / 8;
159 }
160}
static unsafe IntPtr GetPropertyAsIntPtr(this SafeNCryptHandle ncryptHandle, string propertyName, CngPropertyOptions options)
Definition Helpers.cs:112
static byte[] GenerateRandom(int count)
Definition Helpers.cs:139
static unsafe string GetPropertyAsString(this SafeNCryptHandle ncryptHandle, string propertyName, CngPropertyOptions options)
Definition Helpers.cs:85
static byte[] GetCipherIv(this CipherMode cipherMode, byte[] iv)
Definition Helpers.cs:21
static byte[] MapZeroLengthArrayToNonNullPointer(this byte[] src)
Definition Helpers.cs:34
static int GetPaddingSize(this SymmetricAlgorithm algorithm, CipherMode mode, int feedbackSizeInBits)
Definition Helpers.cs:156
static SafeNCryptProviderHandle OpenStorageProvider(this CngProvider provider)
Definition Helpers.cs:43
static int GetPropertyAsDword(this SafeNCryptHandle ncryptHandle, string propertyName, CngPropertyOptions options)
Definition Helpers.cs:102
static int BitSizeToByteSize(this int bits)
Definition Helpers.cs:134
static unsafe void SetExportPolicy(this SafeNCryptKeyHandle keyHandle, CngExportPolicies exportPolicy)
Definition Helpers.cs:125
static unsafe byte[] GetProperty(this SafeNCryptHandle ncryptHandle, string propertyName, CngPropertyOptions options)
Definition Helpers.cs:55
static byte[] CloneByteArray(this byte[] src)
Definition Helpers.cs:147
static bool UsesIv(this CipherMode cipherMode)
Definition Helpers.cs:16
static int ToInt32(byte[] value, int startIndex)
static unsafe? string PtrToStringUni(IntPtr ptr)
Definition Marshal.cs:652
static string Cryptography_MissingIV
Definition SR.cs:112
Definition SR.cs:7
static int Size
Definition IntPtr.cs:21
static readonly IntPtr Zero
Definition IntPtr.cs:18