Esempio n. 1
0
File: Util.cs Progetto: shoff/ngit
 internal static string GetFingerPrint(HASH hash, byte[] data)
 {
     try
     {
         hash.Init();
         hash.Update(data, 0, data.Length);
         byte[]        foo = hash.Digest();
         StringBuilder sb  = new StringBuilder();
         int           bar;
         for (int i = 0; i < foo.Length; i++)
         {
             bar = foo[i] & unchecked ((int)(0xff));
             sb.Append(chars[((int)(((uint)bar) >> 4)) & unchecked ((int)(0xf))]);
             sb.Append(chars[(bar) & unchecked ((int)(0xf))]);
             if (i + 1 < foo.Length)
             {
                 sb.Append(":");
             }
         }
         return(sb.ToString());
     }
     catch (Exception)
     {
         return("???");
     }
 }
 internal virtual byte[] GenKey(byte[] passphrase, byte[] iv)
 {
     lock (this)
     {
         if (cipher == null)
         {
             cipher = GenCipher();
         }
         if (hash == null)
         {
             hash = GenHash();
         }
         byte[] key   = new byte[cipher.GetBlockSize()];
         int    hsize = hash.GetBlockSize();
         byte[] hn    = new byte[key.Length / hsize * hsize + (key.Length % hsize == 0 ? 0 :
                                                               hsize)];
         try
         {
             byte[] tmp = null;
             if (vendor == VENDOR_OPENSSH)
             {
                 for (int index = 0; index + hsize <= hn.Length;)
                 {
                     if (tmp != null)
                     {
                         hash.Update(tmp, 0, tmp.Length);
                     }
                     hash.Update(passphrase, 0, passphrase.Length);
                     hash.Update(iv, 0, iv.Length > 8 ? 8 : iv.Length);
                     tmp = hash.Digest();
                     System.Array.Copy(tmp, 0, hn, index, tmp.Length);
                     index += tmp.Length;
                 }
                 System.Array.Copy(hn, 0, key, 0, key.Length);
             }
             else
             {
                 if (vendor == VENDOR_FSECURE)
                 {
                     for (int index = 0; index + hsize <= hn.Length;)
                     {
                         if (tmp != null)
                         {
                             hash.Update(tmp, 0, tmp.Length);
                         }
                         hash.Update(passphrase, 0, passphrase.Length);
                         tmp = hash.Digest();
                         System.Array.Copy(tmp, 0, hn, index, tmp.Length);
                         index += tmp.Length;
                     }
                     System.Array.Copy(hn, 0, key, 0, key.Length);
                 }
             }
         }
         catch (Exception e)
         {
             System.Console.Error.WriteLine(e);
         }
         return(key);
     }
 }
Esempio n. 3
0
		internal static string GetFingerPrint(HASH hash, byte[] data)
		{
			try
			{
				hash.Init();
				hash.Update(data, 0, data.Length);
				byte[] foo = hash.Digest();
				StringBuilder sb = new StringBuilder();
				int bar;
				for (int i = 0; i < foo.Length; i++)
				{
					bar = foo[i] & unchecked((int)(0xff));
					sb.Append(chars[((int)(((uint)bar) >> 4)) & unchecked((int)(0xf))]);
					sb.Append(chars[(bar) & unchecked((int)(0xf))]);
					if (i + 1 < foo.Length)
					{
						sb.Append(":");
					}
				}
				return sb.ToString();
			}
			catch (Exception)
			{
				return "???";
			}
		}
Esempio n. 4
0
 /// <exception cref="NSch.JSchException"></exception>
 public virtual bool SetPassphrase(byte[] _passphrase)
 {
     try
     {
         if (encrypted)
         {
             if (_passphrase == null)
             {
                 return(false);
             }
             byte[] passphrase = _passphrase;
             int    hsize      = hash.GetBlockSize();
             byte[] hn         = new byte[key.Length / hsize * hsize + (key.Length % hsize == 0 ? 0 :
                                                                        hsize)];
             byte[] tmp = null;
             if (keytype == OPENSSH)
             {
                 for (int index = 0; index + hsize <= hn.Length;)
                 {
                     if (tmp != null)
                     {
                         hash.Update(tmp, 0, tmp.Length);
                     }
                     hash.Update(passphrase, 0, passphrase.Length);
                     hash.Update(iv, 0, iv.Length > 8 ? 8 : iv.Length);
                     tmp = hash.Digest();
                     System.Array.Copy(tmp, 0, hn, index, tmp.Length);
                     index += tmp.Length;
                 }
                 System.Array.Copy(hn, 0, key, 0, key.Length);
             }
             else
             {
                 if (keytype == FSECURE)
                 {
                     for (int index = 0; index + hsize <= hn.Length;)
                     {
                         if (tmp != null)
                         {
                             hash.Update(tmp, 0, tmp.Length);
                         }
                         hash.Update(passphrase, 0, passphrase.Length);
                         tmp = hash.Digest();
                         System.Array.Copy(tmp, 0, hn, index, tmp.Length);
                         index += tmp.Length;
                     }
                     System.Array.Copy(hn, 0, key, 0, key.Length);
                 }
             }
             Util.Bzero(passphrase);
         }
         if (Decrypt())
         {
             encrypted = false;
             return(true);
         }
         P_array = Q_array = G_array = pub_array = prv_array = null;
         return(false);
     }
     catch (Exception e)
     {
         if (e is JSchException)
         {
             throw (JSchException)e;
         }
         if (e is Exception)
         {
             throw new JSchException(e.ToString(), (Exception)e);
         }
         throw new JSchException(e.ToString());
     }
 }