00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00026 #ifndef OW32_CryptContext_h
00027 #define OW32_CryptContext_h
00028
00029 #include <OW32/XHCRYPTPROV.h>
00030
00031 namespace OW32
00032 {
00033
00035 class OW32_LIB_EXPORT CCryptContext
00036 {
00037 XHCRYPTPROV m_hCryptProv;
00038
00039 CCryptContext(const CCryptContext& );
00040 CCryptContext operator=(const CCryptContext& );
00041
00042 public:
00043 CCryptContext() {}
00044
00046 CCryptContext(HCRYPTPROV hCryptProv) {
00047 m_hCryptProv = hCryptProv;
00048 }
00049
00050 operator HCRYPTPROV() { return m_hCryptProv; }
00051
00052 BOOL AcquireContext(LPCTSTR ContainerName = NULL, LPCTSTR ProviderName = NULL,
00053 DWORD ProvType = PROV_RSA_FULL, DWORD dwFlags = 0)
00054 {
00055 m_hCryptProv.Close();
00056 return CryptAcquireContext(&m_hCryptProv, ContainerName, ProviderName,
00057 ProvType, dwFlags);
00058 }
00059
00060 BOOL AcquireContextWithCreate(LPCTSTR ContainerName = NULL, LPCTSTR ProviderName = NULL,
00061 DWORD ProvType = PROV_RSA_FULL, DWORD dwFlags = 0)
00062 {
00063 if (AcquireContext(ContainerName, ProviderName, ProvType, dwFlags))
00064 return TRUE;
00065
00066 return AcquireContext(ContainerName, ProviderName, ProvType, dwFlags|CRYPT_NEWKEYSET);
00067 }
00068
00069 BOOL GetProvParam(DWORD dwParam, BYTE* pbData, DWORD* cbData, DWORD Flags = 0)
00070 {
00071 return CryptGetProvParam(m_hCryptProv, dwParam, pbData, cbData, Flags);
00072 }
00073
00074 BOOL GetUserKey(DWORD KeySpec, HCRYPTKEY* phKey)
00075 {
00076 return CryptGetUserKey(m_hCryptProv, KeySpec, phKey);
00077 }
00078
00079 BOOL GenKey(ALG_ID AlgId, HCRYPTKEY* phKey, DWORD Flags = 0)
00080 {
00081 return CryptGenKey(m_hCryptProv, AlgId, Flags, phKey);
00082 }
00083
00084 BOOL GenRandom(DWORD dwLen, BYTE *pbBuffer)
00085 {
00086 return CryptGenRandom(m_hCryptProv, dwLen, pbBuffer);
00087 }
00088
00089 BOOL ImportKey(BYTE* pbData, DWORD dwDataLen, HCRYPTKEY* phKey, HCRYPTKEY hPubKey = NULL, DWORD dwFlags = 0)
00090 {
00091 return CryptImportKey(m_hCryptProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
00092 }
00093 };
00094
00095 }
00096
00097 #endif // OW32_CryptContext_h