-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptoObject.cs
More file actions
62 lines (50 loc) · 1.79 KB
/
Copy pathCryptoObject.cs
File metadata and controls
62 lines (50 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace Articy.EToken
{
public class CryptoObject : IDisposable
{
#region P/Invoke imports
// ReSharper disable InconsistentNaming
// ReSharper disable IdentifierTypo
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CryptAcquireContext(out IntPtr hProv, string pszContainer, string pszProvider, uint dwProvType, uint dwFlags);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CryptSetProvParam(IntPtr hProv, uint dwParam, [In] byte[] pbData, uint dwFlags);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CryptReleaseContext(IntPtr hProv, uint dwFlags);
// ReSharper enable InconsistentNaming
// ReSharper enable IdentifierTypo
#endregion
private const string cProviderName = "eToken Base Cryptographic Provider";
private IntPtr mHandle;
public CryptoObject()
{
mHandle = IntPtr.Zero;
}
public void Dispose()
{
if (mHandle != IntPtr.Zero)
{
CryptReleaseContext(mHandle, 0);
}
}
public bool OpenToken(string aContainerId)
{
return CryptAcquireContext(out mHandle, aContainerId, cProviderName, /*PROV_RSA_FULL*/ 1, /*CRYPT_SILENT*/ 0x00000040);
}
public bool SetPassword(string aPassword)
{
if (mHandle != IntPtr.Zero)
{
throw new ArgumentException("Container not aquired", "mHandle");
}
var data = Encoding.UTF8.GetBytes(aPassword);
return CryptSetProvParam(mHandle, /*PP_SIGNATURE_PIN*/ 33, data, 0);
}
}
}