Granados.SSH2.SSH2UserAuthKey.PassphraseToKey C# (CSharp) Method

PassphraseToKey() public static method

public static PassphraseToKey ( string passphrase, int length ) : byte[]
passphrase string
length int
return byte[]
        public static byte[] PassphraseToKey(string passphrase, int length)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] pp = Encoding.UTF8.GetBytes(passphrase);
            int hashlen = md5.HashSize / 8;
            byte[] buf = new byte[((length + hashlen) / hashlen) * hashlen];
            int offset = 0;

            while (offset < length) {
                MemoryStream s = new MemoryStream();
                s.Write(pp, 0, pp.Length);
                if (offset > 0)
                    s.Write(buf, 0, offset);
                Array.Copy(md5.ComputeHash(s.ToArray()), 0, buf, offset, hashlen);
                offset += hashlen;
                md5.Initialize();
            }

            byte[] key = new byte[length];
            Array.Copy(buf, 0, key, 0, length);
            return key;
        }