Пример #1
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];
                }
            }

            Security.Clear(_digestT1);
        }
Пример #2
0
        static byte[] MFcrypt(byte[] P, byte[] S,
                              int cost, int blockSize, int parallel, int?maxThreads)
        {
            int MFLen = blockSize * 128;

            if (maxThreads == null)
            {
                maxThreads = int.MaxValue;
            }

            if (!BitMath.IsPositivePowerOf2(cost))
            {
                throw Exceptions.ArgumentOutOfRange("cost", "Cost must be a positive power of 2.");
            }
            Check.Range("blockSize", blockSize, 1, int.MaxValue / 128);
            Check.Range("parallel", parallel, 1, int.MaxValue / MFLen);
            Check.Range("maxThreads", (int)maxThreads, 1, int.MaxValue);

#if !(USEBC || NETCORE)
            byte[] B = Pbkdf2.ComputeDerivedKey(new HMACSHA256(P), S, 1, parallel * MFLen);
#else
            var mac = new nStratis.BouncyCastle.crypto.macs.HMac(new nStratis.BouncyCastle.crypto.digests.Sha256Digest());
            mac.Init(new nStratis.BouncyCastle.crypto.parameters.KeyParameter(P));
            byte[] B = Pbkdf2.ComputeDerivedKey(mac, S, 1, parallel * MFLen);
#endif
            uint[] B0 = new uint[B.Length / 4];
            for (int i = 0; i < B0.Length; i++)
            {
                B0[i] = BitPacking.UInt32FromLEBytes(B, i * 4);
            }             // code is easier with uint[]
            ThreadSMixCalls(B0, MFLen, cost, blockSize, parallel, (int)maxThreads);
            for (int i = 0; i < B0.Length; i++)
            {
                BitPacking.LEBytesFromUInt32(B0[i], B, i * 4);
            }
            Security.Clear(B0);

            return(B);
        }