2 // Mono.Security.Cryptography.CryptoAPI
5 // Sebastien Pouliot (spouliot@motus.com)
7 // Copyright (C) 2003 Motus Technologies Inc. (http://www.motus.com)
11 using System
.Runtime
.InteropServices
;
13 namespace Mono
.Security
.Cryptography
{
15 internal class CryptoAPI
{
17 [DllImport ("advapi32.dll", CallingConvention
=CallingConvention
.StdCall
, SetLastError
=true)]
18 public static extern bool CryptAcquireContextA (ref IntPtr phProv
, string pszContainer
, string pszProvider
, int dwProvType
, uint dwFlags
);
19 [DllImport ("advapi32.dll", CallingConvention
=CallingConvention
.StdCall
, SetLastError
=true)]
20 public static extern bool CryptCreateHash (IntPtr hProv
, uint Algid
, IntPtr hKey
, uint dwFlags
, ref IntPtr phHash
);
21 [DllImport ("advapi32.dll", CallingConvention
=CallingConvention
.StdCall
, SetLastError
=true)]
22 public static extern bool CryptDecrypt (IntPtr hKey
, IntPtr hHash
, bool Final
, uint dwFlags
, byte[] pbData
, ref uint pdwDataLen
);
23 [DllImport ("advapi32.dll", CallingConvention
=CallingConvention
.StdCall
, SetLastError
=true)]
24 public static extern bool CryptDestroyHash (IntPtr hHash
);
25 [DllImport ("advapi32.dll", CallingConvention
=CallingConvention
.StdCall
, SetLastError
=true)]
26 public static extern bool CryptDestroyKey (IntPtr hKey
);
27 [DllImport ("advapi32.dll", CallingConvention
=CallingConvention
.StdCall
, SetLastError
=true)]
28 public static extern bool CryptEncrypt (IntPtr hKey
, IntPtr hHash
, bool Final
, uint dwFlags
, byte[] pbData
, ref uint pdwDataLen
, uint dwBufLen
);
29 [DllImport ("advapi32.dll", CallingConvention
=CallingConvention
.StdCall
, SetLastError
=true)]
30 public static extern bool CryptGenKey (IntPtr hProv
, uint Algid
, uint dwFlags
, ref IntPtr phKey
);
31 [DllImport ("advapi32.dll", CallingConvention
=CallingConvention
.StdCall
, SetLastError
=true)]
32 public static extern bool CryptGenRandom (IntPtr hProv
, uint dwLen
, byte[] pbBuffer
);
33 [DllImport ("advapi32.dll", CallingConvention
=CallingConvention
.StdCall
, SetLastError
=true)]
34 public static extern bool CryptGetHashParam (IntPtr hHash
, uint dwParam
, byte[] pbData
, ref uint pdwDataLen
, uint dwFlags
);
35 [DllImport ("advapi32.dll", CallingConvention
=CallingConvention
.StdCall
, SetLastError
=true)]
36 public static extern bool CryptHashData (IntPtr hHash
, byte[] pbData
, uint dwDataLen
, uint dwFlags
);
37 [DllImport ("advapi32.dll", CallingConvention
=CallingConvention
.StdCall
, SetLastError
=true)]
38 public static extern bool CryptImportKey (IntPtr hProv
, byte[] pbData
, uint dwDataLen
, IntPtr hPubKey
, uint dwFlags
, ref IntPtr phKey
);
39 [DllImport ("advapi32.dll", CallingConvention
=CallingConvention
.StdCall
, SetLastError
=true)]
40 public static extern bool CryptReleaseContext (IntPtr hProv
, uint dwFlags
);
42 public static readonly uint CRYPT_VERIFYCONTEXT
= 0xF0000000;
43 public static readonly uint CRYPT_NEWKEYSET
= 0x00000008;
44 public static readonly uint CRYPT_DELETEKEYSET
= 0x00000010;
45 public static readonly uint CRYPT_MACHINE_KEYSET
= 0x00000020;
46 public static readonly uint CRYPT_SILENT
= 0x00000040;
48 public static readonly int PROV_RSA_FULL
= 1;
50 public static readonly uint HP_HASHVAL
= 0x0002;
52 public static readonly int CALG_MD2
= 0x8001;
53 public static readonly int CALG_MD4
= 0x8002;
54 public static readonly int CALG_MD5
= 0x8003;
55 public static readonly int CALG_SHA1
= 0x8004;
57 // just so we don't have to add System.Runtime.InteropServices
59 static public int GetLastError ()
61 return Marshal
.GetLastWin32Error ();