public byte[] Expand(HashAlgorithm hash, byte[] secret, byte[] seed, int length)
        {
            int hashLength = hash.HashSize / 8;
            int iterations = (int)(length / hashLength);

            if ((length % hashLength) > 0)
            {
                iterations++;
            }

            M.HMAC    hmac    = new M.HMAC(hash, secret);
            TlsStream resMacs = new TlsStream();

            byte[][] hmacs = new byte[iterations + 1][];
            hmacs[0] = seed;
            for (int i = 1; i <= iterations; i++)
            {
                TlsStream hcseed = new TlsStream();
                hmac.TransformFinalBlock(hmacs[i - 1], 0, hmacs[i - 1].Length);
                hmacs[i] = hmac.Hash;
                hcseed.Write(hmacs[i]);
                hcseed.Write(seed);
                hmac.TransformFinalBlock(hcseed.ToArray(), 0, (int)hcseed.Length);
                resMacs.Write(hmac.Hash);
                hcseed.Reset();
            }

            byte[] res = new byte[length];

            Buffer.BlockCopy(resMacs.ToArray(), 0, res, 0, res.Length);

            resMacs.Reset();

            return(res);
        }
Exemple #2
0
        public byte[] Expand(string hashName, byte[] secret, byte[] seed, int length)
        {
            int num  = (!(hashName == "MD5")) ? 20 : 16;
            int num2 = length / num;

            if (length % num > 0)
            {
                num2++;
            }
            Mono.Security.Cryptography.HMAC hMAC = new Mono.Security.Cryptography.HMAC(hashName, secret);
            TlsStream tlsStream = new TlsStream();

            byte[][] array = new byte[num2 + 1][];
            array[0] = seed;
            for (int i = 1; i <= num2; i++)
            {
                TlsStream tlsStream2 = new TlsStream();
                hMAC.TransformFinalBlock(array[i - 1], 0, array[i - 1].Length);
                array[i] = hMAC.Hash;
                tlsStream2.Write(array[i]);
                tlsStream2.Write(seed);
                hMAC.TransformFinalBlock(tlsStream2.ToArray(), 0, (int)tlsStream2.Length);
                tlsStream.Write(hMAC.Hash);
                tlsStream2.Reset();
            }
            byte[] array2 = new byte[length];
            Buffer.BlockCopy(tlsStream.ToArray(), 0, array2, 0, array2.Length);
            tlsStream.Reset();
            return(array2);
        }
        // from Mono.Security.Protocol.Tls.CipherSuite.Expand() with
        // a bit of modification ...
        byte [] Expand(string hashName, byte[] secret, byte[] seed, int length)
        {
            int hashLength = hashName == "MD5" ? 16 : 20;
            int iterations = (int)(length / hashLength);

            if ((length % hashLength) > 0)
            {
                iterations++;
            }

            M.HMAC       hmac    = new M.HMAC(hashName, secret);
            MemoryStream resMacs = new MemoryStream();

            byte[][] hmacs = new byte[iterations + 1][];
            hmacs[0] = seed;
            for (int i = 1; i <= iterations; i++)
            {
                MemoryStream hcseed = new MemoryStream();
                hmac.TransformFinalBlock(hmacs[i - 1], 0, hmacs[i - 1].Length);
                hmacs[i] = hmac.Hash;
                hcseed.Write(hmacs[i], 0, hmacs [i].Length);
                hcseed.Write(seed, 0, seed.Length);
                hmac.TransformFinalBlock(hcseed.ToArray(), 0, (int)hcseed.Length);
                resMacs.Write(hmac.Hash, 0, hmac.Hash.Length);
            }

            byte[] res = new byte[length];

            Buffer.BlockCopy(resMacs.ToArray(), 0, res, 0, res.Length);

            return(res);
        }
Exemple #4
0
        public byte[] Expand(string hashName, byte[] secret, byte[] seed, int length)
        {
            int num1 = !(hashName == "MD5") ? 20 : 16;
            int num2 = length / num1;

            if (length % num1 > 0)
            {
                ++num2;
            }
            Mono.Security.Cryptography.HMAC hmac = new Mono.Security.Cryptography.HMAC(hashName, secret);
            TlsStream tlsStream1 = new TlsStream();

            byte[][] numArray1 = new byte[num2 + 1][];
            numArray1[0] = seed;
            for (int index = 1; index <= num2; ++index)
            {
                TlsStream tlsStream2 = new TlsStream();
                hmac.TransformFinalBlock(numArray1[index - 1], 0, numArray1[index - 1].Length);
                numArray1[index] = hmac.Hash;
                tlsStream2.Write(numArray1[index]);
                tlsStream2.Write(seed);
                hmac.TransformFinalBlock(tlsStream2.ToArray(), 0, (int)tlsStream2.Length);
                tlsStream1.Write(hmac.Hash);
                tlsStream2.Reset();
            }
            byte[] numArray2 = new byte[length];
            Buffer.BlockCopy((Array)tlsStream1.ToArray(), 0, (Array)numArray2, 0, numArray2.Length);
            tlsStream1.Reset();
            return(numArray2);
        }
Exemple #5
0
		public byte[] Expand(string hashName, byte[] secret, byte[] seed, int length)
		{
			int hashLength	= hashName == "MD5" ? 16 : 20;
			int	iterations	= (int)(length / hashLength);
			if ((length % hashLength) > 0)
			{
				iterations++;
			}
			
			M.HMAC		hmac	= new M.HMAC(hashName, secret);
			TlsStream	resMacs	= new TlsStream();
			
			byte[][] hmacs = new byte[iterations + 1][];
			hmacs[0] = seed;
			for (int i = 1; i <= iterations; i++)
			{				
				TlsStream hcseed = new TlsStream();
				hmac.TransformFinalBlock(hmacs[i-1], 0, hmacs[i-1].Length);
				hmacs[i] = hmac.Hash;
				hcseed.Write(hmacs[i]);
				hcseed.Write(seed);
				hmac.TransformFinalBlock(hcseed.ToArray(), 0, (int)hcseed.Length);
				resMacs.Write(hmac.Hash);
				hcseed.Reset();
			}

			byte[] res = new byte[length];
			
			Buffer.BlockCopy(resMacs.ToArray(), 0, res, 0, res.Length);

			resMacs.Reset();

			return res;
		}