Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ DeriveKey()

static void Internal.NativeCrypto.CapiHelper.DeriveKey ( SafeProvHandle hProv,
int algid,
int algidHash,
byte[] password,
int cbPassword,
int dwFlags,
byte[] IV_Out,
int cbIV_In,
[NotNull] ref byte[] pbKey )
inlinestatic

Definition at line 1194 of file CapiHelper.cs.

1195 {
1196 VerifyValidHandle(hProv);
1197 SafeHashHandle phHash = null;
1198 SafeKeyHandle phKey = null;
1199 try
1200 {
1201 if (!CryptCreateHash(hProv, algidHash, SafeKeyHandle.InvalidHandle, global::Interop.Advapi32.CryptCreateHashFlags.None, out phHash))
1202 {
1203 int hRForLastWin32Error = Marshal.GetHRForLastWin32Error();
1204 throw hRForLastWin32Error.ToCryptographicException();
1205 }
1206 if (!global::Interop.Advapi32.CryptHashData(phHash, password, cbPassword, 0))
1207 {
1208 int hRForLastWin32Error2 = Marshal.GetHRForLastWin32Error();
1209 throw hRForLastWin32Error2.ToCryptographicException();
1210 }
1211 if (!CryptDeriveKey(hProv, algid, phHash, dwFlags | 1, out phKey))
1212 {
1213 int hRForLastWin32Error3 = Marshal.GetHRForLastWin32Error();
1214 throw hRForLastWin32Error3.ToCryptographicException();
1215 }
1216 byte[] key_out = null;
1217 int cb_out = 0;
1218 UnloadKey(hProv, phKey, ref key_out, ref cb_out);
1219 int pdwDataLen = 0;
1220 if (!global::Interop.Advapi32.CryptGetKeyParam(phKey, global::Interop.Advapi32.CryptGetKeyParamFlags.KP_IV, null, ref pdwDataLen, 0))
1221 {
1222 int hRForLastWin32Error4 = Marshal.GetHRForLastWin32Error();
1223 throw hRForLastWin32Error4.ToCryptographicException();
1224 }
1225 byte[] array = new byte[pdwDataLen];
1226 if (!global::Interop.Advapi32.CryptGetKeyParam(phKey, global::Interop.Advapi32.CryptGetKeyParamFlags.KP_IV, array, ref pdwDataLen, 0))
1227 {
1228 int hRForLastWin32Error5 = Marshal.GetHRForLastWin32Error();
1229 throw hRForLastWin32Error5.ToCryptographicException();
1230 }
1231 if (pdwDataLen != cbIV_In)
1232 {
1234 }
1235 Buffer.BlockCopy(array, 0, IV_Out, 0, pdwDataLen);
1236 pbKey = new byte[cb_out];
1237 Buffer.BlockCopy(key_out, 0, pbKey, 0, cb_out);
1238 }
1239 finally
1240 {
1241 phKey?.Dispose();
1242 phHash?.Dispose();
1243 }
1244 }
static bool CryptCreateHash(SafeProvHandle hProv, int algId, SafeKeyHandle hKey, global::Interop.Advapi32.CryptCreateHashFlags dwFlags, out SafeHashHandle phHash)
static void UnloadKey(SafeProvHandle hProv, SafeKeyHandle hKey, [NotNull] ref byte[] key_out, ref int cb_out)
static void VerifyValidHandle(SafeHandleZeroOrMinusOneIsInvalid handle)
static bool CryptDeriveKey(SafeProvHandle hProv, int algId, SafeHashHandle phHash, int dwFlags, out SafeKeyHandle phKey)
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
static string Cryptography_PasswordDerivedBytes_InvalidIV
Definition SR.cs:58
Definition SR.cs:7

References System.array, System.Buffer.BlockCopy(), Internal.NativeCrypto.CapiHelper.CryptCreateHash(), Internal.NativeCrypto.CapiHelper.CryptDeriveKey(), System.SR.Cryptography_PasswordDerivedBytes_InvalidIV, System.Runtime.InteropServices.SafeHandle.Dispose(), System.Security.Cryptography.SafeHashHandle.Dispose(), System.Runtime.InteropServices.Marshal.GetHRForLastWin32Error(), Internal.NativeCrypto.CapiHelper.UnloadKey(), and Internal.NativeCrypto.CapiHelper.VerifyValidHandle().

Referenced by System.Security.Cryptography.PasswordDeriveBytes.CryptDeriveKey().