Пример #1
1
 public static byte[] SHA256_RIPEMD160(byte[] input)
 {
     byte[] sha256 = new SHA256CryptoServiceProvider().ComputeHash(input);
     var digest = new RipeMD160Digest();
     digest.BlockUpdate(sha256, 0, sha256.Length);
     var outArray = new byte[20];
     digest.DoFinal(outArray, 0);
     return outArray;
 }
Пример #2
0
        public static byte[] Digest(byte[] data, String algo)
        {
            if (algo == null)
            {
                throw new ArgumentNullException("El algoritmo de huella digital no puede ser nulo");
            }
            if (data == null)
            {
                throw new ArgumentNullException("Los datos no pueden ser nulos");
            }

            switch (algo)
            {
                /**
                 * ALGORITMOS DE HASING
                 */
                case AOSignConstants.SIGN_ALGORITHM_SHA1:
                    {
                        Sha1Digest dig = new Sha1Digest();
                        dig.BlockUpdate(data, 0, data.Length);
                        byte[] result = new byte[dig.GetDigestSize()];
                        dig.DoFinal(result, 0);
                        return result;
                    }

                case AOSignConstants.SIGN_ALGORITHM_SHA256:
                    {
                        Sha256Digest dig = new Sha256Digest();
                        dig.BlockUpdate(data, 0, data.Length);
                        byte[] result = new byte[dig.GetDigestSize()];
                        dig.DoFinal(result, 0);
                        return result;
                    }

                case AOSignConstants.SIGN_ALGORITHM_SHA384:
                    {
                        Sha384Digest dig = new Sha384Digest();
                        dig.BlockUpdate(data, 0, data.Length);
                        byte[] result = new byte[dig.GetDigestSize()];
                        dig.DoFinal(result, 0);
                        return result;
                    }

                case AOSignConstants.SIGN_ALGORITHM_SHA512:
                    {
                        Sha512Digest dig = new Sha512Digest();
                        dig.BlockUpdate(data, 0, data.Length);
                        byte[] result = new byte[dig.GetDigestSize()];
                        dig.DoFinal(result, 0);
                        return result;
                    }

                case AOSignConstants.SIGN_ALGORITHM_RIPEMD160:
                    {
                        RipeMD160Digest dig = new RipeMD160Digest();
                        dig.BlockUpdate(data, 0, data.Length);
                        byte[] result = new byte[dig.GetDigestSize()];
                        dig.DoFinal(result, 0);
                        return result;
                    }
                case AOSignConstants.SIGN_ALGORITHM_MD5:
                    {
                        MD5Digest dig = new MD5Digest();
                        dig.BlockUpdate(data, 0, data.Length);
                        byte[] result = new byte[dig.GetDigestSize()];
                        dig.DoFinal(result, 0);
                        return result;
                    }

                case AOSignConstants.SIGN_ALGORITHM_MD2:
                    {
                        MD2Digest dig = new MD2Digest();
                        dig.BlockUpdate(data, 0, data.Length);
                        byte[] result = new byte[dig.GetDigestSize()];
                        dig.DoFinal(result, 0);
                        return result;
                    }

                default:
                    // You can use the default case.
                    throw new ArgumentNullException("El algoritmo no es reconocido");
            }

            throw new ArgumentNullException("Algoritmo de hash no soportado: " + algo);
        }
Пример #3
0
 /// <summary>
 /// Calculates RIPEMD160(SHA256(input)). This is used in Address calculations.
 /// </summary>
 public static byte[] Sha256Hash160(byte[] input)
 {
     var sha256 = new SHA256Managed().ComputeHash(input);
     var digest = new RipeMD160Digest();
     digest.BlockUpdate(sha256, 0, sha256.Length);
     var @out = new byte[20];
     digest.DoFinal(@out, 0);
     return @out;
 }
Пример #4
0
        public ITestResult Perform()
        {
            IDigest digest = new RipeMD160Digest();
            byte[] resBuf = new byte[digest.GetDigestSize()];

            for (int i = 0; i < messages.Length; i++)
            {
                byte[] m = Encoding.ASCII.GetBytes(messages[i]);
                digest.BlockUpdate(m, 0, m.Length);
                digest.DoFinal(resBuf, 0);

                if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[i])))
                {
                    return new SimpleTestResult(false, Name + ": Vector " + i + " failed");
                }
            }

            //
            // test 2
            //
            byte[] mm = Encoding.ASCII.GetBytes(messages[messages.Length-1]);

            digest.BlockUpdate(mm, 0, mm.Length/2);

            // clone the IDigest
            IDigest d = new RipeMD160Digest((RipeMD160Digest)digest);

            digest.BlockUpdate(mm, mm.Length/2, mm.Length - mm.Length/2);
            digest.DoFinal(resBuf, 0);

            if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[digests.Length-1])))
            {
                return new SimpleTestResult(false,
                    "RipeMD160 failing clone test"
                    + SimpleTest.NewLine
                    + "    expected: " + digests[digests.Length-1]
                    + SimpleTest.NewLine
                    + "    got     : " + Hex.ToHexString(resBuf));
            }

            d.BlockUpdate(mm, mm.Length/2, mm.Length - mm.Length/2);
            d.DoFinal(resBuf, 0);

            if (!Arrays.AreEqual(resBuf, Hex.Decode(digests[digests.Length-1])))
            {
                return new SimpleTestResult(false,
                    "RipeMD160 failing clone test - part 2"
                    + SimpleTest.NewLine
                    + "    expected: " +  digests[digests.Length-1]
                    + SimpleTest.NewLine
                    + "    got     : " + Hex.ToHexString(resBuf));
            }

            for (int i = 0; i < 1000000; i++)
            {
                digest.Update((byte)'a');
            }
            digest.DoFinal(resBuf, 0);

            if (!Arrays.AreEqual(resBuf, Hex.Decode(MillionADigest)))
            {
                return new SimpleTestResult(false, Name + ": Million a's failed");
            }

            return new SimpleTestResult(true, Name + ": Okay");
        }
Пример #5
0
 public static byte[] RIPEMD160(byte[] data, int count)
 {
     RipeMD160Digest ripemd = new RipeMD160Digest();
     ripemd.BlockUpdate(data, 0, count);
     byte[] rv = new byte[20];
     ripemd.DoFinal(rv, 0);
     return rv;
 }
Пример #6
0
        /// <summary>
        /// Calculates RIPEMD160(SHA256(input)). This is used in Address calculations.
        /// </summary>
        public static byte[] Sha256Hash160(this byte[] me)
        {
            byte[] sha256=new SHA256Managed().ComputeHash(me);
            RipeMD160Digest digest=new RipeMD160Digest();
            digest.BlockUpdate(sha256,0,sha256.Length);

            byte[] result=new byte[20];
            digest.DoFinal(result,0);
            return result;
        }