コード例 #1
0
        public static byte[] HMACSHA256(byte[] key, byte[] data)
        {
            var mac = new Bitcoin3.BouncyCastle.Crypto.Macs.HMac(new Sha256Digest());

            mac.Init(new KeyParameter(key));
            mac.Update(data);
            byte[] result = new byte[mac.GetMacSize()];
            mac.DoFinal(result, 0);
            return(result);
        }
コード例 #2
0
ファイル: SCrypt.cs プロジェクト: frankvanbokhoven/Bitcoin-3
        internal static Pbkdf2 GetStream(byte[] key, byte[] salt,
                                         int cost, int blockSize, int parallel, int?maxThreads)
        {
            byte[] B   = GetEffectivePbkdf2Salt(key, salt, cost, blockSize, parallel, maxThreads);
            var    mac = new Bitcoin3.BouncyCastle.Crypto.Macs.HMac(new Bitcoin3.BouncyCastle.Crypto.Digests.Sha256Digest());

            mac.Init(new KeyParameter(key));
            Pbkdf2 kdf = new Pbkdf2(mac, B, 1);

            Security.Clear(B);
            return(kdf);
        }
コード例 #3
0
        public byte[] DeriveSeed(string passphrase = null)
        {
            passphrase = passphrase ?? "";
            var salt  = Concat(NoBOMUTF8.GetBytes("mnemonic"), Normalize(passphrase));
            var bytes = Normalize(_Mnemonic);

#if USEBC || WINDOWS_UWP || NETSTANDARD1X
            var mac = new Bitcoin3.BouncyCastle.Crypto.Macs.HMac(new Bitcoin3.BouncyCastle.Crypto.Digests.Sha512Digest());
            mac.Init(new KeyParameter(bytes));
            return(Pbkdf2.ComputeDerivedKey(mac, salt, 2048, 64));
#else
            return(Pbkdf2.ComputeDerivedKey(new HMACSHA512(bytes), salt, 2048, 64));
#endif
        }
コード例 #4
0
ファイル: SCrypt.cs プロジェクト: frankvanbokhoven/Bitcoin-3
        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 || NETSTANDARD1X)
            byte[] B = Pbkdf2.ComputeDerivedKey(new HMACSHA256(P), S, 1, parallel * MFLen);
#else
            var mac = new Bitcoin3.BouncyCastle.Crypto.Macs.HMac(new Bitcoin3.BouncyCastle.Crypto.Digests.Sha256Digest());
            mac.Init(new 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);
        }