public byte[] HashFinal() { byte[] hash = new byte [hashSize]; context.InternalResult = CryptoAPI.CryptGetHashParam(handle, CryptoAPI.HP_HASHVAL, hash, ref hashSize, 0); GC.KeepAlive(this); return(hash); }
// Create an instance using the specified CSP public CapiContext(CspParameters csp) { providerHandle = IntPtr.Zero; if (csp == null) { // default parameters cspParams = new CspParameters(); } else { // keep of copy of the parameters cspParams = new CspParameters(csp.ProviderType, csp.ProviderName, csp.KeyContainerName); cspParams.KeyNumber = csp.KeyNumber; cspParams.Flags = csp.Flags; } // do not show user interface (CRYPT_SILENT) - if UI is required then the function fails. uint flags = CryptoAPI.CRYPT_SILENT; if ((cspParams.Flags & CspProviderFlags.UseMachineKeyStore) == CspProviderFlags.UseMachineKeyStore) { flags |= CryptoAPI.CRYPT_MACHINE_KEYSET; } lastResult = CryptoAPI.CryptAcquireContextA(ref providerHandle, cspParams.KeyContainerName, cspParams.ProviderName, cspParams.ProviderType, flags); if (!lastResult) { // key container may not exist flags |= CryptoAPI.CRYPT_NEWKEYSET; lastResult = CryptoAPI.CryptAcquireContextA(ref providerHandle, cspParams.KeyContainerName, cspParams.ProviderName, cspParams.ProviderType, flags); } }
// release unmanaged resources public void Dispose() { if (providerHandle != IntPtr.Zero) { lastResult = CryptoAPI.CryptReleaseContext(providerHandle, 0); GC.KeepAlive(this); providerHandle = IntPtr.Zero; GC.SuppressFinalize(this); } }
// FIXME: calling this function 1,000,000 times (with a single character) // is a good way to lose time (and hung NUnit) // TODO: find the bug that hang NUnit // TODO: optimize the function to call CryptHashData less often (bufferize) public void HashCore(byte[] data, int start, int length) { byte[] toBeHashed = data; if (start != 0) { toBeHashed = new byte [length]; Array.Copy(data, start, toBeHashed, 0, length); } context.InternalResult = CryptoAPI.CryptHashData(handle, toBeHashed, (uint)length, 0); GC.KeepAlive(this); }
public void Dispose() { if (handle != IntPtr.Zero) { CryptoAPI.CryptDestroyHash(handle); context.Dispose(); GC.KeepAlive(this); handle = IntPtr.Zero; GC.SuppressFinalize(this); } }
public void Initialize(int algo) { if (context != null) { context.InternalResult = CryptoAPI.CryptCreateHash(context.Handle, (uint)algo, IntPtr.Zero, 0, ref handle); hashSize = 0; if (context.Result) { context.InternalResult = CryptoAPI.CryptGetHashParam(handle, CryptoAPI.HP_HASHVAL, null, ref hashSize, 0); } GC.KeepAlive(this); } }
public void GenRandom(byte[] data) { uint l = (uint)data.Length; InternalResult = CryptoAPI.CryptGenRandom(Handle, l, data); }