Esempio n. 1
0
 public static byte[] RIPEMD160(byte[] data, int offset, int count)
 {
     using (var ripm = new RIPEMD160Managed())
     {
         return ripm.ComputeHash(data, offset, count);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Calculates the RIPEMD160-hash of the given string
        /// </summary>
        /// <returns>Hexadecimal representation of the RIPEMD160-hash.</returns>
        /// <param name="str">Input string.</param>
        public static string RIPEMD160(string str)
        {
            var bytes = Encoding.ASCII.GetBytes (str);
            byte[] hash;

            // We're using the managed RIPEMD160 implementation here.
            using (var hasher = new RIPEMD160Managed ()) {
                hash = hasher.ComputeHash (bytes);
            }

            return hash.ToHex ();
        }
Esempio n. 3
0
 public Address(Byte[] data, Byte version = PUBKEY)
 {
     SHA256 sha256 = new SHA256Managed();
     RIPEMD160 ripemd160 = new RIPEMD160Managed();
     switch (version)
     {
         case PUBKEY:
             pubKeyHash = ripemd160.ComputeHash(sha256.ComputeHash(data));
             version = PUBKEYHASH;
             break;
         case SCRIPT:
             scriptHash = ripemd160.ComputeHash(sha256.ComputeHash(data));
             version = SCRIPTHASH;
             break;
         case PUBKEYHASH:
             pubKeyHash = data;
             break;
         case SCRIPTHASH:
             scriptHash = data;
             break;
     }
     this.type = version;
 }
Esempio n. 4
0
		public static byte[] RIPEMD160(byte[] data, int offset, int count)
		{
#if !USEBC
			using (var ripm = new RIPEMD160Managed())
			{
				return ripm.ComputeHash(data, offset, count);
			}
#else
			RipeMD160Digest ripemd = new RipeMD160Digest();
			ripemd.BlockUpdate(data, offset, count);
			byte[] rv = new byte[20];
			ripemd.DoFinal(rv, 0);
			return rv;
#endif
		}
Esempio n. 5
0
        public static string GetHash(string input)
        {
            RIPEMD160Managed hasher = new RIPEMD160Managed(); //supposedly no collission for RIPEMD160 yet. As of 25.07.2010.
            byte[] data = hasher.ComputeHash(Encoding.Default.GetBytes(input));

            StringBuilder sBuilder = new StringBuilder();

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

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }
Esempio n. 6
0
        public static string GetFileHash(string filePath, HashType type)
        {
            if (!File.Exists(filePath))
                return string.Empty;

            System.Security.Cryptography.HashAlgorithm hasher;
            switch(type)
            {
                case HashType.SHA1:
                default:
                    hasher = new SHA1CryptoServiceProvider();
                    break;
                case HashType.SHA256:
                    hasher = new SHA256Managed();
                    break;
                case HashType.SHA384:
                    hasher = new SHA384Managed();
                    break;
                case HashType.SHA512:
                    hasher = new SHA512Managed();
                    break;
                case HashType.MD5:
                    hasher = new MD5CryptoServiceProvider();
                    break;
                case HashType.RIPEMD160:
                    hasher = new RIPEMD160Managed();
                    break;
            }
            StringBuilder buff = new StringBuilder();
            try
            {
                using (FileStream f = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192))
                {
                    hasher.ComputeHash(f);
                    Byte[] hash = hasher.Hash;
                    foreach (Byte hashByte in hash)
                    {
                        buff.Append(string.Format("{0:x2}", hashByte));
                    }
                }
            }
            catch
            {
                return "Error reading file." + new System.Random(DateTime.Now.Second * DateTime.Now.Millisecond).Next().ToString();
            }
            return buff.ToString();
        }
Esempio n. 7
0
        public static string H2hash(BigInteger qid, BigInteger p)
        {
            // H2 je RiPEMD-120: točka iz polja -> niz bita mod p
            string tocka = qid.ToString();

            RIPEMD160Managed crypt = new RIPEMD160Managed();
            StringBuilder hash = new StringBuilder();
            byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(tocka), 0, Encoding.UTF8.GetByteCount(tocka));
            foreach (byte theByte in crypto)
            {
                hash.Append(theByte.ToString("x2"));
            }

            BigInteger hsh = new BigInteger(hash.ToString(), 16);
            // hash mod n
            BigInteger c = hsh.DivideAndRemainder(p)[1];

            return c.ToString();
        }
Esempio n. 8
0
        /// <summary>
        /// Generates a hash based on the source text.
        /// </summary>
        /// <param name="SourceText">The text to hash.</param>
        /// <returns>The hashed text as a Base64 string.</returns>
        public static string GenerateHash(string sourceText, string salt, HashType hashType)
        {
            sourceText = sourceText + salt;

            System.Security.Cryptography.HashAlgorithm hasher;
            switch (hashType) {
                case HashType.SHA1:
                default:
                    hasher = new SHA1CryptoServiceProvider();
                    break;
                case HashType.SHA256:
                    hasher = new SHA256Managed();
                    break;
                case HashType.SHA384:
                    hasher = new SHA384Managed();
                    break;
                case HashType.SHA512:
                    hasher = new SHA512Managed();
                    break;
                case HashType.MD5:
                    hasher = new MD5CryptoServiceProvider();
                    break;
                case HashType.RIPEMD160:
                    hasher = new RIPEMD160Managed();
                    break;
            }

            StringBuilder buffer = new StringBuilder();
            UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
            //Retrieve a byte array based on the source text
            byte[] byteSourceText = unicodeEncoding.GetBytes(sourceText);
            hasher.ComputeHash(byteSourceText);
            foreach (Byte hashByte in hasher.Hash) {
                buffer.Append(string.Format("{0:x2}", hashByte));
            }
            return buffer.ToString();
        }
Esempio n. 9
0
 /// <summary>
 /// Computes the RIPEMD-160 checksum for the bytes.
 /// </summary>
 public static Byte[] Compute(Byte[] bytes)
 {
     RIPEMD160Managed rmd160 = new RIPEMD160Managed();
     return rmd160.ComputeHash(bytes);
 }
Esempio n. 10
0
 public static string GetRIPEMD160(string text)
 {
     var ue = new UTF8Encoding();
     var message = ue.GetBytes(text);
     var hex = string.Empty;
     var hashString = new RIPEMD160Managed();
     var hashValue = hashString.ComputeHash(message);
     return hashValue.Aggregate(hex, (current, x) => current + String.Format("{0:x2}", x));
 }
Esempio n. 11
0
        /// <summary>
        /// Thanks http://gobittest.appspot.com/Address
        /// </summary>
        /// <param name="ecdsaPublickey">Standard format ecdsa public key taken from the blockchain</param>
        /// <returns></returns>
        private static string ComputeBitcoinAddress(byte[] ecdsaPublickey)
        {
            // step1: byte[32] -- sha256 ecdsaPublicKey
            // step2: byte[20] -- ripemd-160 hash step1
            // step3: byte[21] -- add network bytes to step2
            // step4: byte[32] -- sha256 step3
            // step5: byte[32] -- sha256 step4
            // step6: byte[4]  -- get first four bytes of step 5
            // step7: byte[25] -- add step6 to the end of step3

            using (var sha256Managed = new SHA256Managed())
            {
                using (var ripeMd160Managed = new RIPEMD160Managed())
                {
                    var step3 = new byte[21];
                    var step6 = new byte[4];
                    var step7 = new byte[25];

                    var step1 = sha256Managed.ComputeHash(ecdsaPublickey);
                    var step2 = ripeMd160Managed.ComputeHash(step1);
                    Array.Copy(step2, 0, step3, 1, 20);
                    var step4 = sha256Managed.ComputeHash(step3);
                    var step5 = sha256Managed.ComputeHash(step4);
                    Array.Copy(step5, 0, step6, 0, 4);
                    Array.Copy(step3, 0, step7, 0, 21);
                    Array.Copy(step6, 0, step7, 21, 4);

                    return Base58Encoding.Encode(step7);
                }
            }
        }
Esempio n. 12
0
 /// <summary>
 /// Generate the initialization vector (IV) to feed to the encryption engine.
 /// </summary>
 /// <param name="passphrase">A string containing the passphrase</param>
 /// <param name="size">The size in bits of the IV to generate</param>
 /// <returns>An array of bytes containing the generated IV</returns>
 private static byte[] GenerateIV(string passphrase, int size)
 {
     try
     {
         // Complain if the passphrase is empty:
         if (passphrase == null || passphrase == String.Empty || passphrase == "")
             throw new SecureFileException("The passphrase is empty");
         if (size <= 0 || size % 8 != 0)
             throw new SecureFileException("The block size of the generated initialization " +
                 "vector (IV) is invalid");
         // To generate the IV, we're going to do some funky things to the passphrase (and the
         // salt) to try and get something close to random but still derived.  To do that, we'll
         // use the RIPEMD-160 hash engine.
         RIPEMD160Managed rip = new RIPEMD160Managed();
         // Hash the password with RIPEMD-160 and convert the bytes to Base64:
         string passripper = Convert.ToBase64String(rip.ComputeHash(utf8.GetBytes(passphrase)));
         // Do the same thing with the salt:
         string saltripper = Convert.ToBase64String(rip.ComputeHash(utf8.GetBytes(salt)));
         // Reverse the passphrase using a StringBuilder, then hash that string as well using
         // the RIPEMD-160 / Base64 combo:
         StringBuilder sb = new StringBuilder(passphrase.Length);
         char[] ppBits = passphrase.ToCharArray();
         for (int i = passphrase.Length - 1; i >= 0; i--)
         {
             sb.Append(ppBits[i]);
         }
         string passriprev = Convert.ToBase64String(rip.ComputeHash(utf8.GetBytes(sb.ToString())));
         // Now we'll use SHA-512 and has the combined strings we generated above.  We'll end up
         // with a 512-bit IV, which is probably overkill, but it should be good and "randomized"
         // but still derived from the passphrase.
         SHA512Managed sha = new SHA512Managed();
         byte[] hashed =
             sha.ComputeHash(sha.ComputeHash(utf8.GetBytes(passriprev + saltripper +
             passripper)));
         // Now just get the first so many bytes (size divided by 8) and return them:
         byte[] iv = new byte[size / 8];
         Array.Copy(hashed, iv, size / 8);
         return iv;
     }
     catch (SecureFileException sfe) { throw sfe; }
     catch (Exception ex)
     {
         throw new SecureFileException("An unspecified error occurred: " + ex.ToString());
     }
 }
Esempio n. 13
0
 /// <summary>
 /// Calculates RIPEMD160(SHA256(input)). This is used in Address calculations.
 /// </summary>
 public static byte[] Sha256Hash160(byte[] input)
 {
     var shaAlgorithm = new SHA256Managed();
     var ripemdAlgorithm = new RIPEMD160Managed();
     var sha256 = shaAlgorithm.ComputeHash(input);
     return ripemdAlgorithm.ComputeHash(sha256, 0, sha256.Length);
 }
Esempio n. 14
0
 private static string ripemdencrypt(string phrase)
 {
     UTF8Encoding encoder = new UTF8Encoding();
     RIPEMD160Managed ripemdhasher = new RIPEMD160Managed();
     byte[] hashedDataBytes = ripemdhasher.ComputeHash(encoder.GetBytes(phrase));
     return byteArrayToString(hashedDataBytes);
 }
 /// <summary>
 /// Метод возвращает строку, которая представляет хеш-сумму файла по указанному пути для алгоритма RIPEMD-160.
 /// </summary>
 /// <returns></returns>
 public string GetRIPEMD160Hash()
 {
     StringBuilder sb = new StringBuilder();
     try
     {
         FileStream file = new FileStream(filePath, FileMode.Open);
         RIPEMD160 ripeMD = new RIPEMD160Managed();
         byte[] retVal = ripeMD.ComputeHash(file);
         file.Close();
         for (int i = 0; i < retVal.Length; i++)
         {
             sb.Append(retVal[i].ToString("x2"));
         }
     }
     catch (Exception e)
     {
         System.Windows.MessageBox.Show(e.Message);
     }
     return sb.ToString();
 }