Exemplo n.º 1
0
        public void HashCore(byte[] data, int start, int length)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (start < 0)
            {
                throw new ArgumentException("start");
            }
            if (length < 0)
            {
                throw new ArgumentException("length");
            }

            if (length == 0)
            {
                return;
            }

            // avoid copying data unless required (API limitation)
            if (start == 0)
            {
                MHashWrapper.mhash(handle, data, (IntPtr)length);
            }
            else
            {
                byte[] partial = new byte [length];
                Buffer.BlockCopy(data, start, partial, 0, length);
                MHashWrapper.mhash(handle, partial, (IntPtr)length);
            }
        }
Exemplo n.º 2
0
 public void Initialize()
 {
     if (handle == IntPtr.Zero)
     {
         GC.ReRegisterForFinalize(this);
     }
     handle = MHashWrapper.mhash_init(type);
 }
Exemplo n.º 3
0
 public void Dispose()
 {
     if (handle != IntPtr.Zero)
     {
         // this frees the hashing structure, but allocates the digest
         IntPtr digest = MHashWrapper.mhash_end(handle);
         // so we still have a second free to make to complete dispose
         Marshal.FreeHGlobal(digest);
         handle = IntPtr.Zero;
         GC.SuppressFinalize(this);
     }
 }
Exemplo n.º 4
0
        public MHashHelper(MHashId type)
        {
            this.type = type;
            handle    = MHashWrapper.mhash_init(type);
            if (handle == IntPtr.Zero)
            {
                string msg = String.Format("Unknown mhash id '{0}'.", type);
                throw new CryptographicException(msg);
            }

            blocksize = (int)MHashWrapper.mhash_get_block_size(type);
        }
Exemplo n.º 5
0
        public byte[] HashFinal()
        {
            byte[] result = new byte [blocksize];
            IntPtr digest = MHashWrapper.mhash_end(handle);

            try {
                Marshal.Copy(digest, result, 0, blocksize);
            }
            finally {
                Marshal.FreeHGlobal(digest);
                handle = IntPtr.Zero;
            }
            return(result);
        }