/// <summary>
        /// Open file and get it's SHA1 hash
        /// </summary>
        /// <param name="file_name">File name (full path)</param>
        /// <returns>String representation of the SHA1 hash</returns>
        public static string GetSHAHashFromFile(string file_name, SHAType shaType)
        {
            System.IO.FileStream file = new System.IO.FileStream(file_name, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            byte[] retVal             = null;
            switch (shaType)
            {
            case SHAType.SHA1:
                System.Security.Cryptography.SHA1 sha1obj = new System.Security.Cryptography.SHA1CryptoServiceProvider();
                retVal = sha1obj.ComputeHash(file);
                break;

            case SHAType.SHA256:
                System.Security.Cryptography.SHA256 sha256 = System.Security.Cryptography.SHA256.Create();
                retVal = sha256.ComputeHash(file);
                break;

            case SHAType.SHA384:
                System.Security.Cryptography.SHA384 sha384 = System.Security.Cryptography.SHA384.Create();
                retVal = sha384.ComputeHash(file);
                break;

            case SHAType.SHA512:
                System.Security.Cryptography.SHA512 sha512 = System.Security.Cryptography.SHA512.Create();
                retVal = sha512.ComputeHash(file);
                break;
            }
            file.Close();
            return(BitConverter.ToString(retVal).Replace("-", "").ToLower());
        }
Beispiel #2
0
 public static string Hash(string input)
 {
     System.Security.Cryptography.SHA384 sha384 = System.Security.Cryptography.SHA384.Create();
     // convert string to byte array
     byte[] input_bytes = ASCIIEncoding.ASCII.GetBytes(input);
     byte[] output      = sha384.ComputeHash(input_bytes);
     return(Base64.Encode(output));
 }
 /// <summary>
 /// CheckSum384 method implementation
 /// </summary>
 public static byte[] CheckSum384(string value)
 {
     byte[] hash = null;
     using (System.Security.Cryptography.SHA384 sha384 = System.Security.Cryptography.SHA384Managed.Create())
     {
         hash = sha384.ComputeHash(Encoding.UTF8.GetBytes(value));
     }
     return(hash);
 }
 /// <summary>
 /// CheckSum384 method implementation
 /// </summary>
 public static byte[] CheckSum384(byte[] value)
 {
     byte[] hash = null;
     using (System.Security.Cryptography.SHA384 sha384 = System.Security.Cryptography.SHA384Cng.Create())
     {
         hash = sha384.ComputeHash(value);
     }
     return(hash);
 }
Beispiel #5
0
        public override string StringHashAlgorithm(string value)
        {
            System.Security.Cryptography.SHA384 sha = System.Security.Cryptography.SHA384.Create();
            byte[]        bytes = sha.ComputeHash(System.Text.ASCIIEncoding.UTF8.GetBytes(value));
            StringBuilder sb    = new StringBuilder();

            foreach (var b in bytes)
            {
                sb.Append(b.ToString("x2"));
            }
            return(sb.ToString());
        }
Beispiel #6
0
        // sha384 below
        public static string GetSHA384Hash(string str)
        {
            System.Security.Cryptography.SHA384 sha384 = System.Security.Cryptography.SHA384.Create();
            byte[] bytes   = Encoding.Default.GetBytes(str);
            byte[] encoded = sha384.ComputeHash(bytes);

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < encoded.Length; i++)
            {
                sb.Append(encoded[i].ToString("x2"));
            }

            return(sb.ToString());
        }
Beispiel #7
0
        /// <summary>
        /// Return SHA-384 (SHA-2) for a string.
        /// </summary>
        /// <param name="text">Source text to calculate hash from</param>
        /// <param name="encoding">Text encoding</param>
        /// <returns>SHA1 hash in hex format</returns>
        public static string SHA384(string text, Encoding encoding)
        {
            if (text == null)
            {
                return(null);
            }
#if !NETCF
            using (System.Security.Cryptography.SHA384 cipher = System.Security.Cryptography.SHA384.Create())
            {
                byte[] array = cipher.ComputeHash(encoding.GetBytes(text));
                return(Energy.Base.Hex.ArrayToHex(array).ToLower());
            }
#endif
#if NETCF
            return(null);
#endif
        }
        /// <summary>
        /// 获取md5值
        /// </summary>
        /// <param name="md5Hash"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        private static string GetSHA384Hash(SHA384 hash, string input)
        {

            // Convert the input string to a byte array and compute the hash.
            byte[] data = hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }
        /// <summary>
        /// Get SHA1 hash from buffer of bytes
        /// </summary>
        /// <param name="input_buffer">Input buffer</param>
        /// <returns>16-byte array of hash</returns>
        public static byte[] GetSHAHash(byte[] input_buffer, SHAType shaType)
        {
            switch (shaType)
            {
            case SHAType.SHA1:
                SHA1 sha1 = new SHA1CryptoServiceProvider();
                return(sha1.ComputeHash(input_buffer));

            case SHAType.SHA256:
                System.Security.Cryptography.SHA256 sha256 = System.Security.Cryptography.SHA256.Create();
                return(sha256.ComputeHash(input_buffer));

            case SHAType.SHA384:
                System.Security.Cryptography.SHA384 sha384 = System.Security.Cryptography.SHA384.Create();
                return(sha384.ComputeHash(input_buffer));

            case SHAType.SHA512:
                System.Security.Cryptography.SHA512 sha512 = System.Security.Cryptography.SHA512.Create();
                return(sha512.ComputeHash(input_buffer));
            }
            return(null);
        }
Beispiel #10
0
	public void FIPS186_c (string testName, SHA384 hash, byte[] input, byte[] result) 
	{
		MemoryStream ms = new MemoryStream (input);
		byte[] output = hash.ComputeHash (ms); 
		Assert.AreEqual (result, output, testName + ".c.1");
		Assert.AreEqual (result, hash.Hash, testName + ".c.2");
		// required or next operation will still return old hash
		hash.Initialize ();
	}
Beispiel #11
0
	public void FIPS186_b (string testName, SHA384 hash, byte[] input, byte[] result) 
	{
		byte[] output = hash.ComputeHash (input, 0, input.Length); 
		Assert.AreEqual (result, output, testName + ".b.1");
		Assert.AreEqual (result, hash.Hash, testName + ".b.2");
		// required or next operation will still return old hash
		hash.Initialize ();
	}
	public void FIPS186_a (string testName, SHA384 hash, byte[] input, byte[] result) 
	{
		byte[] output = hash.ComputeHash (input); 
		AssertEquals (testName + ".a.1", result, output);
		AssertEquals (testName + ".a.2", result, hash.Hash);
		// required or next operation will still return old hash
		hash.Initialize ();
	}
Beispiel #13
0
 public override byte[] ByteHashAlgorithm(string value)
 {
     System.Security.Cryptography.SHA384 sha = System.Security.Cryptography.SHA384.Create();
     byte[] bytes = sha.ComputeHash(System.Text.ASCIIEncoding.UTF8.GetBytes(value));
     return(bytes);
 }