Exemplo n.º 1
0
        /// <summary>
        /// Uses the cipher to generate a BCrypt hash.
        /// </summary>
        /// <returns>A BCrypt hash.</returns>
        public byte[] BCrypt()
        {
            uint[] magicWords = null;
            byte[] magicBytes = null;

            try
            {
                magicWords = (uint[])Magic.Clone();
                for (int j = 0; j < magicWords.Length; j += 2)
                {
                    for (int i = 0; i < 64; i++)
                    {
                        Encipher(ref magicWords[j], ref magicWords[j + 1]);
                    }
                }

                magicBytes = new byte[magicWords.Length * 4];
                for (int i = 0; i < magicWords.Length; i++)
                {
                    BitPacking.BEBytesFromUInt32(magicWords[i], magicBytes, i * 4);
                }
                return(ByteArray.TruncateAndCopy(magicBytes, magicBytes.Length - 1));
            }
            finally
            {
                Security.Clear(magicWords);
                Security.Clear(magicBytes);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reverses the encipherment of eight bytes of data from one buffer and places the result in another buffer.
        /// </summary>
        /// <param name="inputBuffer">The buffer to read enciphered data from.</param>
        /// <param name="inputOffset">The offset of the first enciphered byte.</param>
        /// <param name="outputBuffer">The buffer to write plaintext data to.</param>
        /// <param name="outputOffset">The offset at which to place the first plaintext byte.</param>
        public void Decipher
            (byte[] inputBuffer, int inputOffset,
            byte[] outputBuffer, int outputOffset)
        {
            CheckCipherBuffers(inputBuffer, inputOffset, outputBuffer, outputOffset);

            uint xl = BitPacking.UInt32FromBEBytes(inputBuffer, inputOffset + 0);
            uint xr = BitPacking.UInt32FromBEBytes(inputBuffer, inputOffset + 4);

            Decipher(ref xl, ref xr);
            BitPacking.BEBytesFromUInt32(xl, outputBuffer, outputOffset + 0);
            BitPacking.BEBytesFromUInt32(xr, outputBuffer, outputOffset + 4);
        }
Exemplo n.º 3
0
        void ComputeBlock(uint pos)
        {
            BitPacking.BEBytesFromUInt32(pos, _saltBuffer, _saltBuffer.Length - 4);
            ComputeHmac(_saltBuffer, _digestT1);
            Array.Copy(_digestT1, _digest, _digestT1.Length);

            for (int i = 1; i < _iterations; i++)
            {
                ComputeHmac(_digestT1, _digestT1);
                for (int j = 0; j < _digest.Length; j++)
                {
                    _digest[j] ^= _digestT1[j];
                }
            }

            NBitcoin.Crypto.Internal.Security.Clear(_digestT1);
        }
Exemplo n.º 4
0
        private void ComputeBlock(uint pos)
        {
            BitPacking.BEBytesFromUInt32(pos, this._saltBuffer, this._saltBuffer.Length - 4);
            ComputeHmac(this._saltBuffer, this._digestT1);
            Array.Copy(this._digestT1, this._digest, this._digestT1.Length);

            for (int i = 1; i < this._iterations; i++)
            {
                ComputeHmac(this._digestT1, this._digestT1);
                for (int j = 0; j < this._digest.Length; j++)
                {
                    this._digest[j] ^= this._digestT1[j];
                }
            }

            Security.Clear(this._digestT1);
        }