Beispiel #1
0
        /// <summary>
        /// Initialization constructor for byte array
        /// </summary>
        /// <param name="buffer">Data</param>
        internal SecBufferDesc(byte[] buffer)
        {
            // Set version to SECBUFFER_VERSION
            Version = 0;

            // We have only one buffer
            BufferCount = 1;

            // Allocate security buffer
            var secBuffer = new SecBuffer(buffer);

            // Allocate native memory for managed block
            BuffersPtr = Marshal.AllocHGlobal(Marshal.SizeOf(secBuffer));

            try
            {
                // Copy managed data into the native memory
                Marshal.StructureToPtr(secBuffer, BuffersPtr, false);
            }
            catch (Exception)
            {
                // Delete native memory
                Marshal.FreeHGlobal(BuffersPtr);

                // Reset native buffer pointer
                BuffersPtr = IntPtr.Zero;

                // Re-throw exception
                throw;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Initialization constructor
        /// </summary>
        /// <param name="size">Size of the buffer to allocate</param>
        internal SecBufferDesc(int size)
        {
            // Set version to SECBUFFER_VERSION
            Version = 0;

            // Set the number of buffers
            BufferCount = 1;

            // Allocate a security buffer of the requested size
            var secBuffer = new SecBuffer(size);

            // Allocate a native chunk of memory for security buffer
            BuffersPtr = Marshal.AllocHGlobal(Marshal.SizeOf(secBuffer));

            try
            {
                // Copy managed data into the native memory
                Marshal.StructureToPtr(secBuffer, BuffersPtr, false);
            }
            catch (Exception)
            {
                // Delete native memory
                Marshal.FreeHGlobal(BuffersPtr);

                // Reset native buffer pointer
                BuffersPtr = IntPtr.Zero;

                // Re-throw exception
                throw;
            }
        }