Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CngCommon.cs
Go to the documentation of this file.
1using System;
2using System.IO;
5
7
8internal static class CngCommon
9{
10 public static byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
11 {
12 using HashProviderCng hashProviderCng = new HashProviderCng(hashAlgorithm.Name, null);
13 hashProviderCng.AppendHashData(data, offset, count);
14 return hashProviderCng.FinalizeHashAndReset();
15 }
16
17 public static bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
18 {
19 using HashProviderCng hashProviderCng = new HashProviderCng(hashAlgorithm.Name, null);
20 if (destination.Length < hashProviderCng.HashSizeInBytes)
21 {
22 bytesWritten = 0;
23 return false;
24 }
25 hashProviderCng.AppendHashData(source);
26 return hashProviderCng.TryFinalizeHashAndReset(destination, out bytesWritten);
27 }
28
29 public static byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm)
30 {
31 using HashProviderCng hashProviderCng = new HashProviderCng(hashAlgorithm.Name, null);
32 byte[] array = new byte[4096];
33 int count;
34 while ((count = data.Read(array, 0, array.Length)) > 0)
35 {
36 hashProviderCng.AppendHashData(array, 0, count);
37 }
38 return hashProviderCng.FinalizeHashAndReset();
39 }
40
41 public unsafe static byte[] SignHash(this SafeNCryptKeyHandle keyHandle, ReadOnlySpan<byte> hash, global::Interop.NCrypt.AsymmetricPaddingMode paddingMode, void* pPaddingInfo, int estimatedSize)
42 {
43 byte[] array = new byte[estimatedSize];
44 int pcbResult;
45 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptSignHash(keyHandle, pPaddingInfo, hash, hash.Length, array, array.Length, out pcbResult, paddingMode);
46 if (errorCode == global::Interop.NCrypt.ErrorCode.STATUS_UNSUCCESSFUL)
47 {
48 errorCode = global::Interop.NCrypt.NCryptSignHash(keyHandle, pPaddingInfo, hash, hash.Length, array, array.Length, out pcbResult, paddingMode);
49 }
50 if (errorCode == global::Interop.NCrypt.ErrorCode.NTE_BUFFER_TOO_SMALL)
51 {
52 array = new byte[pcbResult];
53 errorCode = global::Interop.NCrypt.NCryptSignHash(keyHandle, pPaddingInfo, hash, hash.Length, array, array.Length, out pcbResult, paddingMode);
54 }
55 if (errorCode == global::Interop.NCrypt.ErrorCode.STATUS_UNSUCCESSFUL)
56 {
57 errorCode = global::Interop.NCrypt.NCryptSignHash(keyHandle, pPaddingInfo, hash, hash.Length, array, array.Length, out pcbResult, paddingMode);
58 }
59 if (errorCode != 0)
60 {
61 throw errorCode.ToCryptographicException();
62 }
63 Array.Resize(ref array, pcbResult);
64 return array;
65 }
66
67 public unsafe static bool TrySignHash(this SafeNCryptKeyHandle keyHandle, ReadOnlySpan<byte> hash, Span<byte> signature, global::Interop.NCrypt.AsymmetricPaddingMode paddingMode, void* pPaddingInfo, out int bytesWritten)
68 {
69 for (int i = 0; i <= 1; i++)
70 {
71 int pcbResult;
72 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptSignHash(keyHandle, pPaddingInfo, hash, hash.Length, signature, signature.Length, out pcbResult, paddingMode);
73 switch (errorCode)
74 {
75 case global::Interop.NCrypt.ErrorCode.ERROR_SUCCESS:
76 bytesWritten = pcbResult;
77 return true;
78 case global::Interop.NCrypt.ErrorCode.NTE_BUFFER_TOO_SMALL:
79 bytesWritten = 0;
80 return false;
81 default:
82 throw errorCode.ToCryptographicException();
83 case global::Interop.NCrypt.ErrorCode.STATUS_UNSUCCESSFUL:
84 break;
85 }
86 }
87 throw global::Interop.NCrypt.ErrorCode.STATUS_UNSUCCESSFUL.ToCryptographicException();
88 }
89
90 public unsafe static bool VerifyHash(this SafeNCryptKeyHandle keyHandle, ReadOnlySpan<byte> hash, ReadOnlySpan<byte> signature, global::Interop.NCrypt.AsymmetricPaddingMode paddingMode, void* pPaddingInfo)
91 {
92 global::Interop.NCrypt.ErrorCode errorCode = global::Interop.NCrypt.NCryptVerifySignature(keyHandle, pPaddingInfo, hash, hash.Length, signature, signature.Length, paddingMode);
93 if (errorCode == global::Interop.NCrypt.ErrorCode.STATUS_UNSUCCESSFUL)
94 {
95 errorCode = global::Interop.NCrypt.NCryptVerifySignature(keyHandle, pPaddingInfo, hash, hash.Length, signature, signature.Length, paddingMode);
96 }
97 return errorCode == global::Interop.NCrypt.ErrorCode.ERROR_SUCCESS;
98 }
99}
static byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
Definition CngCommon.cs:10
static byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm)
Definition CngCommon.cs:29
static bool TryHashData(ReadOnlySpan< byte > source, Span< byte > destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
Definition CngCommon.cs:17
static unsafe bool TrySignHash(this SafeNCryptKeyHandle keyHandle, ReadOnlySpan< byte > hash, Span< byte > signature, global::Interop.NCrypt.AsymmetricPaddingMode paddingMode, void *pPaddingInfo, out int bytesWritten)
Definition CngCommon.cs:67
static unsafe bool VerifyHash(this SafeNCryptKeyHandle keyHandle, ReadOnlySpan< byte > hash, ReadOnlySpan< byte > signature, global::Interop.NCrypt.AsymmetricPaddingMode paddingMode, void *pPaddingInfo)
Definition CngCommon.cs:90
static unsafe byte[] SignHash(this SafeNCryptKeyHandle keyHandle, ReadOnlySpan< byte > hash, global::Interop.NCrypt.AsymmetricPaddingMode paddingMode, void *pPaddingInfo, int estimatedSize)
Definition CngCommon.cs:41
int Read(byte[] buffer, int offset, int count)
int Length
Definition Span.cs:70