Esempio n. 1
0
        /// <summary>
        /// Compute hash from message source
        /// </summary>
        /// <param name="source">Stream or buffer</param>
        /// <returns>Hash computed</returns>
        public byte[] Compute(MessageToHashReader source)
        {
            // INIT
            // Init hash buffer
            UInt64[,] hashState = new UInt64[8, 2];
            for (int row = 0; row < 8; row++)
            {
                hashState[row, 0] = this._initState[row, 0];
                hashState[row, 1] = this._initState[row, 1];
            }

            // UPDATE
            // Transform complete blocks
            byte[] buffer       = new byte[BUFFER_LEN];
            ulong  blockCount   = 0;
            long   allBytesRead = 0;
            int    bytesRead;

            while ((bytesRead = source.Read(buffer, 0, BUFFER_LEN)) == BUFFER_LEN)
            {
                allBytesRead += bytesRead;
                Compress(hashState, buffer);
                blockCount++;
            }
            allBytesRead += bytesRead;
            ulong bitCount = (ulong)(allBytesRead << 3);

            // FINAL
            ulong bitPadding  = 384 + (ulong)((((-(long)bitCount) % 512) + 512) % 512);
            ulong totalBlocks = (bitCount + bitPadding + 128) / 512;
            int   blocksLeft  = (int)(totalBlocks - blockCount);

            // 0x80 tail and zero padding
            buffer[bytesRead] = 0x80;
            for (int p = bytesRead + 1; p < BUFFER_LEN; p++)
            {
                buffer[p] = 0x00;
            }

            // Process extra blocks
            if (blocksLeft > 1) // if two extra blocks
            {
                Compress(hashState, buffer);
                buffer = new byte[BUFFER_LEN];
            }
            // last extra block
            {
                byte[] bytes = BitConverter.GetBytes(bitCount);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(bytes);
                }
                Buffer.BlockCopy(bytes, 0, buffer, BUFFER_LEN - 8, 8);
                Compress(hashState, buffer);
            }

            var result = PlainHash(hashState);

            return(result);
        }
Esempio n. 2
0
        public byte[] Compute(MessageToHashReader source)
        {
            // INIT
            // Init hash variables (in derivate class)
            var bufferLength = this._bufferLength;

            InitState(out ISha2State hashState);

            // UPDATE
            // Transform complete blocks
            byte[] buffer     = new byte[bufferLength];
            UInt64 blockCount = 0;
            int    bytesRead;

            while ((bytesRead = source.Read(buffer, 0, bufferLength)) == bufferLength)
            {
                Compress(hashState, buffer);
                blockCount++;
            }
            UInt64 bitLength = ((blockCount * (UInt64)bufferLength) + (UInt64)bytesRead) << 3;

            // FINAL
            // Calc extra blocks
            int blocksLeft = 1;

            if ((bytesRead + 1 + (bufferLength >> 3)) > bufferLength)
            {
                blocksLeft++;
            }

            // 0x80 tail and zero padding
            buffer[bytesRead] = 0x80;
            for (int p = bytesRead + 1; p < bufferLength; p++)
            {
                buffer[p] = 0x00;
            }

            // Process extra blocks
            if (blocksLeft > 1) // if two extra blocks
            {
                Compress(hashState, buffer);
                buffer = new byte[bufferLength];
            }
            // last extra block
            {
                byte[] length = BitConverter.GetBytes(bitLength);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(length);
                }
                Buffer.BlockCopy(length, 0, buffer, bufferLength - 8, 8);
                Compress(hashState, buffer);
            }

            // Finalize hash and truncate (Output Transformation)
            return(FinalHash(hashState));
        }
Esempio n. 3
0
        /// <summary>
        /// Compute hash from message source
        /// </summary>
        /// <param name="source">Stream or buffer</param>
        /// <returns>Hash computed</returns>
        public byte[] Compute(MessageToHashReader source)
        {
            // INIT
            // Init hash buffer
            var state        = Init();
            var bufferLength = this._bufferLength;
            var remain       = source.Length; // Skein needs to know data length

            // UPDATE
            // Transform complete blocks
            byte[] buffer       = new byte[bufferLength];
            long   allBytesRead = 0;
            int    bytesRead;

            while (((bytesRead = source.Read(buffer, 0, bufferLength)) == bufferLength) &&
                   ((remain -= bytesRead) > 0)) // This condition order enable full final block
            {
                allBytesRead += bytesRead;
                Compress(ref state, buffer, (UInt64)allBytesRead);
            }
            allBytesRead += bytesRead;

            // FINAL
            // Process last block with data
            state.Flags |= SkeinFlags.Final;
            for (int p = bytesRead; p < bufferLength; p++)  // fill with 0 rest of buffer
            {
                buffer[p] = 0x00;
            }
            Compress(ref state, buffer, (ulong)allBytesRead);

            // Last block for extended hash (> buffer length)
            state.Flags = SetType(BlockType.OUT, SkeinFlags.Final);
            buffer      = new byte[bufferLength];
            Compress(ref state, buffer, 8);

            // Format final hash
            return(FinalHash(state));
        }
Esempio n. 4
0
        /// <summary>
        /// Compute hash from message source
        /// </summary>
        /// <param name="source">Stream or buffer</param>
        /// <returns>Hash computed</returns>
        public byte[] Compute(MessageToHashReader source)
        {
            // INIT
            // Init hash variables (in derivate class)
            var bufferLength = this._bufferLength;

            InitState(out IBlakeState hashState);

            // UPDATE
            // Transform complete blocks
            byte[] buffer     = new byte[bufferLength];
            UInt64 bitLength  = 0;
            UInt64 blockCount = 0;
            int    bytesRead;

            while ((bytesRead = source.Read(buffer, 0, bufferLength)) == bufferLength)
            {
                bitLength += (UInt64)(bytesRead << 3);
                Compress(hashState, buffer, bitLength);
                blockCount++;
            }
            bitLength += (UInt64)(bytesRead << 3);

            // FINAL
            // Calc extra blocks
            int bitWrap = bufferLength << 3;
            //int finalBytes = bufferLength >> 3;
            UInt64 bitPadding  = (UInt64)((((-(long)bitLength - (bufferLength + 1)) % bitWrap) + bitWrap) % bitWrap);
            UInt64 totalBlocks = (bitLength + bitPadding + ((UInt64)bufferLength + 1)) / (UInt64)bitWrap;
            int    blocksLeft  = (int)(totalBlocks - blockCount);

            // 0x80 tail and zero padding
            buffer[bytesRead] = 0x80;
            for (int p = bytesRead + 1; p < bufferLength; p++)
            {
                buffer[p] = 0x00;
            }

            // Process extra blocks

            if (blocksLeft > 1) // if two extra blocks
            {
                Compress(hashState, buffer, bitLength);
                buffer = new byte[bufferLength];
            }
            // last extra block
            {
                byte[] length = BitConverter.GetBytes((UInt64)bitLength);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(length);
                }
                Buffer.BlockCopy(length, 0, buffer, bufferLength - 8, 8);
                buffer[bufferLength - (bufferLength >> 3) - 1] ^= this._finalXor;
                if (blocksLeft == 2 || (blocksLeft == 1 && bytesRead == 0))
                {
                    bitLength = 0;
                }
                Compress(hashState, buffer, bitLength);
            }

            // Finalize hash and truncate (Output Transformation)
            return(FinalHash(hashState));
        }
Esempio n. 5
0
        public byte[] Compute(MessageToHashReader source)
        {
            // INIT
            // Init hash variables (in derivate class)
            var bufferLength = this._bufferLength;

            InitState(out IQmhHukState hashState);

            // UPDATE
            // Transform complete blocks
            byte[] buffer     = new byte[bufferLength];
            UInt64 blockCount = 0;
            int    bytesRead;

            while ((bytesRead = source.Read(buffer, 0, bufferLength)) == bufferLength)
            {
                Compress(hashState, buffer);
                blockCount++;
            }
            UInt64 bitLength = ((blockCount * (UInt64)bufferLength) + (UInt64)bytesRead) << 3;

            // FINAL
            // Calc extra blocks
            int blocksLeft = 1;

            if ((bytesRead + 11) > bufferLength)  // 11 = 1 (bit tail) + 2 (bit HASH len) + 8 (bit DATA len)
            {
                blocksLeft++;
            }

            // 0x80 tail and PI padding
            buffer[bytesRead] = 0x80;
            for (int p = bytesRead + 1, pi = 0; p < bufferLength; p++, pi++)
            {
                buffer[p] = PI_PADDING[pi];
            }

            // Process extra blocks
            if (blocksLeft > 1) // if two extra blocks
            {
                Compress(hashState, buffer);
                buffer = new byte[bufferLength];
                Buffer.BlockCopy(PHI_PADDING, 0, buffer, 0, bufferLength);
            }
            // last extra block
            {
                // set HASH bitLen
                buffer[bufferLength - 10] = _bitLenMark[0];
                buffer[bufferLength - 9]  = _bitLenMark[1];
                // set DATA bitLen
                byte[] length = BitConverter.GetBytes(bitLength);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(length);
                }
                Buffer.BlockCopy(length, 0, buffer, bufferLength - 8, 8);
                Compress(hashState, buffer);
            }

            // Finalize hash and truncate (Output Transformation)
            return(FinalHash(hashState));
        }