예제 #1
0
        /// <summary>	Construct from base58 bitshares public key </summary>
        ///
        /// <remarks>	Paul, 08/12/2014. </remarks>
        ///
        /// <param name="base58BtsPubKey">	The base 58 bts pub key. </param>
        public BitsharesPubKey(string base58BtsPubKey)
        {
            m_ripe = RIPEMD160.Create();

            if (base58BtsPubKey.Length == 54)
            {
                base58BtsPubKey = base58BtsPubKey.Substring(4, base58BtsPubKey.Length - 4);
            }
            else if (base58BtsPubKey.Length == 53)
            {
                base58BtsPubKey = base58BtsPubKey.Substring(3, base58BtsPubKey.Length - 3);
            }
            else
            {
                throw new NotImplementedException();
            }

            byte[] data = Base58.ToByteArray(base58BtsPubKey);
            Debug.Assert(data.Length == 37);

            byte[] pubkeyCheck = RIPEMD160.Create().ComputeHash(data, 0, 33);

            // check the hash first four bytes are equal to the last four bytes of the pub key
            for (int i = 0; i < 4; i++)
            {
                if (pubkeyCheck[i] != data[i + 33])
                {
                    throw new BadChecksumException();
                }
            }

            m_compressed = new byte[33];
            Array.Copy(data, 0, m_compressed, 0, m_compressed.Length);
        }
예제 #2
0
        public void RIPEMD160Empty()
        {
            var ripemd160 = RIPEMD160.Create();
            var result    = ripemd160.ComputeHash(string.Empty.ToByteArray());

            Assert.AreEqual("9c1185a5c5e9fc54612808977ee8f548b2258d31", result.ToHexString());
        }
예제 #3
0
        public void RIPEMD160LowerLetters()
        {
            var ripemd160 = RIPEMD160.Create();
            var result    = ripemd160.ComputeHash("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".ToByteArray());

            Assert.AreEqual("12a053384a9c0c88e405a06c27dcf49ada62eb2b", result.ToHexString());
        }
예제 #4
0
        static PublicKey FromStringOrThrow(string publicKey, string addressPrefix = null)
        {
            if (addressPrefix.IsNull())
            {
                addressPrefix = ChainConfig.AddressPrefix;
            }
            var prefix = publicKey.Substring(0, addressPrefix.Length);

            Assert.Equal(
                addressPrefix, prefix,
                string.Format("Expecting key to begin with {0}, instead got {1}", addressPrefix, prefix)
                );

            publicKey = publicKey.Substring(addressPrefix.Length);
            var buffer = Base58.Decode(publicKey);

            var checksum = buffer.Slice(buffer.Length - 4);

            buffer = buffer.Slice(0, buffer.Length - 4);

            var newChecksum = RIPEMD160.Create().HashAndDispose(buffer);

            newChecksum = newChecksum.Slice(0, 4);

            var isEqual = checksum.DeepEqual(newChecksum);               // invalid checksum

            if (!isEqual)
            {
                throw new InvalidOperationException("Checksum did not match");
            }
            return(FromBuffer(buffer));
        }
예제 #5
0
        public static HashAlgorithm GetHashAlgorithmByType(DigestType dgType)
        {
            HashAlgorithm ha = null;

            switch (dgType)
            {
            case DigestType.MD5:
                ha = MD5.Create();
                break;

            case DigestType.SHA1:
                ha = SHA1.Create();
                break;

            case DigestType.SHA256:
                ha = SHA256.Create();
                break;

            case DigestType.SHA384:
                ha = SHA384.Create();
                break;

            case DigestType.SHA512:
                ha = SHA512.Create();
                break;

            case DigestType.RIPEMD160:
                ha = RIPEMD160.Create();
                break;
            }
            return(ha);
        }
예제 #6
0
        protected override void BeginProcessing()
        {
            base.BeginProcessing();
            switch (HashType.ToLower())
            {
            case "md5":
                Algorithm = MD5.Create();
                break;

            case "sha1":
                Algorithm = SHA1.Create();
                break;

            case "sha256":
                Algorithm = SHA256.Create();
                break;

            case "sha384":
                Algorithm = SHA384.Create();
                break;

            default:
            case "sha512":
                Algorithm = SHA512.Create();
                break;

            case "ripemd160":
                Algorithm = RIPEMD160.Create();
                break;
            }
        }
예제 #7
0
        /// <summary>
        /// Creates a <see cref="HashAlgorithm"/> for the specified <paramref name="hashType"/>
        /// </summary>
        /// <param name="hashType">Type of the hash.</param>
        /// <returns></returns>
        public static HashAlgorithm CreateHashAlgorithm(HashType hashType)
        {
            switch (GetHashType(hashType))
            {
            case HashType.MD5:
                return(MD5.Create());

            case HashType.SHA1:
                return(SHA1.Create());

            case HashType.SHA256:
                return(SHA256.Create());

            case HashType.SHA512:
                return(SHA512.Create());

            case HashType.SHA384:
                return(SHA384.Create());

            case HashType.RipeMD160:
                return(RIPEMD160.Create());

            default:
                throw new ArgumentException("Invalid hashtype", "hashType");
            }
        }
예제 #8
0
        public void Calculate(Stream _stream)
        {
            if (chkKey.Checked)
            {
                if (!txtKey.IsValid)
                {
                    HashFields_Empty();
                    HashFields_ResetColour();
                    txtKey.BackColor = Color.Salmon;
                    return;
                }

                byte[] abyKey = txtKey.ByteArray;

                ComputeHash(txtMD5, new HMACMD5(abyKey), _stream);
                ComputeHash(txtRIPEMD160, new HMACRIPEMD160(abyKey), _stream);
                ComputeHash(txtSHA1, new HMACSHA1(abyKey), _stream);
                ComputeHash(txtSHA256, new HMACSHA256(abyKey), _stream);
                ComputeHash(txtSHA384, new HMACSHA384(abyKey), _stream);
                ComputeHash(txtSHA512, new HMACSHA512(abyKey), _stream);
            }
            else
            {
                ComputeHash(txtMD5, MD5.Create(), _stream);
                ComputeHash(txtRIPEMD160, RIPEMD160.Create(), _stream);
                ComputeHash(txtSHA1, SHA1.Create(), _stream);
                ComputeHash(txtSHA256, SHA256.Create(), _stream);
                ComputeHash(txtSHA384, SHA384.Create(), _stream);
                ComputeHash(txtSHA512, SHA512.Create(), _stream);
                ComputeHash(txtCRC32, new CRC32(), _stream);
            }

            TestMatcher();
        }
예제 #9
0
        public void AddColumnInformationTestReverse()
        {
            OutputColumn          target        = new OutputColumn();
            IDTSBufferManager100  bufferManager = new BufferManagerTestImpl();
            IDTSOutput100         output        = new OutputTestImpl();
            IDTSInput100          input         = new InputTestImpl();
            IDTSOutputColumn100   outputColumn;
            IDTSCustomProperty100 customProperty;
            int outputColumnIndex = 0;

            outputColumn         = output.OutputColumnCollection.New();
            customProperty       = outputColumn.CustomPropertyCollection.New();
            customProperty.Name  = Utility.InputColumnLineagePropName;
            customProperty.Value = "#1,#2,#3,#4,#5,#6";
            customProperty       = outputColumn.CustomPropertyCollection.New();
            customProperty.Name  = Utility.HashTypePropName;
            customProperty.Value = MultipleHash.HashTypeEnumerator.RipeMD160;

            target.AddColumnInformation(bufferManager, output, input, outputColumnIndex);
            Assert.AreEqual(6, target.Count, "The number of items in the list");
            Assert.AreEqual(1, target[0], "The first input");
            Assert.AreEqual(2, target[1], "The second input");
            Assert.AreEqual(3, target[2], "The third input");
            Assert.AreEqual(4, target[3], "The forth input");
            Assert.AreEqual(5, target[4], "The fifth input");
            Assert.AreEqual(6, target[5], "The sixth input");
            Assert.AreEqual(MultipleHash.HashTypeEnumerator.RipeMD160, target.HashType, "Hash");
            Assert.AreEqual(RIPEMD160.Create().ToString(), target.HashObject.ToString(), "Hash Object");
        }
예제 #10
0
        private HashAlgorithm Algorithm()
        {
            switch (hash)
            {
            case (Hashes.MD5):
                return(MD5.Create());

            case (Hashes.RIPEMD160):
                return(RIPEMD160.Create());

            case (Hashes.SHA1):
                return(SHA1.Create());

            case (Hashes.SHA256):
                return(SHA256.Create());

            case (Hashes.SHA384):
                return(SHA384.Create());

            case (Hashes.SHA512):
                return(SHA512.Create());

            default:
                return(MD5.Create());
            }
        }
예제 #11
0
        public void TestP2SH_P2PKH()
        {
            //Create p2pkh
            CryptoRSA rsa = new CryptoRSA(key, true);

            Interpreter.Initialize();

            //Create address
            byte[] pubkeyhash;

            using (var rid = RIPEMD160.Create())
                using (var sha = SHA256.Create())
                    pubkeyhash = rid.ComputeHash(sha.ComputeHash(key.publicKey));

            LockingScript   ls = new LockingScript(BASE58.Encode(pubkeyhash));
            UnlockingScript us = new UnlockingScript(rsa.Sign(transaction.Hash()), key.publicKey);

            //Create p2sh and insert p2pkh in it
            LockingScript   ls1 = new LockingScript(ls);
            UnlockingScript us1 = new UnlockingScript(ls, us);

            us1.InsertScript(ls1);

            Assert.AreEqual(EXECUTION_RESULT.SUCCESS, us1.Run(transaction), "Execution of P2SH script failed");
        }
예제 #12
0
        private void setHash()
        {
            switch (chooseHash)
            {
            case (Hashes.MD5):
                hash = MD5.Create();
                break;

            case (Hashes.RIPEMD160):
                hash = RIPEMD160.Create();
                break;

            case (Hashes.SHA1):
                hash = SHA1.Create();
                break;

            case (Hashes.SHA256):
                hash = SHA256.Create();
                break;

            case (Hashes.SHA384):
                hash = SHA384.Create();
                break;

            case (Hashes.SHA512):
                hash = SHA512.Create();
                break;
            }
        }
예제 #13
0
        private static String GetAddressFromPublicKey(byte[] scriptBytes, int publicKeyIndex, int keyLength, byte[] type)
        {
            var sha  = SHA256.Create().ComputeHash(scriptBytes, publicKeyIndex, keyLength);
            var ripe = RIPEMD160.Create().ComputeHash(sha);

            return(GetAddressFromHash160(ripe, 0, BASE58PREFIX_PUBKEY_ADDRESS));
        }
예제 #14
0
        public static HashAlgorithm Create(string algorithmName)
        {
            switch (algorithmName.ToUpperInvariant())
            {
            case HashAlgorithmNames.MD5:
                return(MD5.Create());

            case HashAlgorithmNames.SHA1:
                return(SHA1.Create());

            case HashAlgorithmNames.SHA256:
                return(SHA256.Create());

            case HashAlgorithmNames.SHA384:
                return(SHA384.Create());

            case HashAlgorithmNames.SHA512:
                return(SHA512.Create());

            case HashAlgorithmNames.RIPEMD160:
                return(RIPEMD160.Create());
            }

            return(null);
        }
예제 #15
0
        public Wallet(byte[] publicKey)
        {
            // Step 1: Allocate payload list
            var payload = new List <byte>();

            // Step 2: Append version
            //payload.Add(version);
            payload.AddRange(WalletVersion);

            // Step 3: Create ripmd160(sha256(publicKey)) hash
            var pub256Key = SHA256.Create().ComputeHash(publicKey);
            var ripmd160  = RIPEMD160.Create().ComputeHash(pub256Key);

            // Step 4: append hash to payload
            payload.AddRange(ripmd160);

            // Step 5: compute checksum of current payload
            var checksum = Checksum(payload.ToArray());

            // Step 6: append computed checksum to payload
            payload.AddRange(checksum);

            var address = payload.ToArray();

            // Step 7: convert payload bytes to base58 format
            Base58Address = Base58.Bitcoin.Encode(address);
        }
예제 #16
0
        /// <summary>
        /// Get the <see cref="HashAlgorithm"/> associated to the <see cref="HashAlgorithmEnum"/>
        /// </summary>
        /// <param name="algorithm">Algorithm request</param>
        /// <returns></returns>
        static public HashAlgorithm GetAlgorithm(HashAlgorithmEnum algorithm)
        {
            switch (algorithm)
            {
            case HashAlgorithmEnum.MD5:
                return(MD5.Create());

            case HashAlgorithmEnum.SHA1:
                return(SHA1.Create());

            case HashAlgorithmEnum.SHA256:
                return(SHA256.Create());

            case HashAlgorithmEnum.SHA384:
                return(SHA384.Create());

            case HashAlgorithmEnum.SHA512:
                return(SHA512.Create());

            case HashAlgorithmEnum.RIPEMD160:
                return(RIPEMD160.Create());

            case HashAlgorithmEnum.KeyedHashAlgorithm:
                return(KeyedHashAlgorithm.Create());

            default:
                return(SHA256.Create());
            }
        }
예제 #17
0
        private string GetHash(SupportedHashAlgorithims hashType, string filePath)
        {
            switch (hashType)
            {
            case SupportedHashAlgorithims.MESSAGEDIGEST5:
                return(MakeHashString(MD5.Create().ComputeHash(new FileStream(filePath, FileMode.Open))));

            case SupportedHashAlgorithims.SECUREHASHALGORITHIM1:
                return(MakeHashString(SHA1.Create().ComputeHash(new FileStream(filePath, FileMode.Open))));

            case SupportedHashAlgorithims.SECUREHASHALGORITHIM256:
                return(MakeHashString(SHA256.Create().ComputeHash(new FileStream(filePath, FileMode.Open))));

            case SupportedHashAlgorithims.SECUREHASHALGORITHIM384:
                return(MakeHashString(SHA384.Create().ComputeHash(new FileStream(filePath, FileMode.Open))));

            case SupportedHashAlgorithims.SECUREHASHALGORITHIM512:
                return(MakeHashString(SHA512.Create().ComputeHash(new FileStream(filePath, FileMode.Open))));

                // TODO: Why do we need to do this?
#if !NETCOREAPP
            case SupportedHashAlgorithims.RACEINTEGRITYPRIMITIVESEVALUATIONMESSAGEDIGEST:
                return(MakeHashString(RIPEMD160.Create().ComputeHash(new FileStream(filePath, FileMode.Open))));
#endif
            default:
                return("");
            }
        }
예제 #18
0
        private void btnGeraHashRIPEMD160_Click(object sender, EventArgs e)
        {
            txtHash.Text = "";
            var hash = new TrataHash(RIPEMD160.Create());

            txtHash.Text = hash.GerarHash(txtTexto.Text);
        }
예제 #19
0
        public void GenerateHash_RIPEMD160()
        {
            Hash h = new Hash(wellKnownAssembly);

            byte[] result = h.GenerateHash(RIPEMD160.Create());
            Assert.AreEqual("29-5A-21-11-BC-CA-1B-26-A2-46-F2-48-B4-61-96-1C-A5-2C-9A-9A", BitConverter.ToString(result));
        }
 public static byte[] Ripemd160(byte[] data)
 {
     using (RIPEMD160 hash = RIPEMD160.Create())
     {
         return(hash.ComputeHash(data));
     }
 }
예제 #21
0
        public static string GetHash(this string input, HashType hashType)
        {
            byte[] inputBytes = input.ToByteArray();

            switch (hashType)
            {
            case HashType.HMAC: return(Convert.ToBase64String(HMAC.Create().ComputeHash(inputBytes)));

            case HashType.HMACMD5: return(Convert.ToBase64String(HMAC.Create().ComputeHash(inputBytes)));

            case HashType.HMACSHA1: return(Convert.ToBase64String(HMAC.Create().ComputeHash(inputBytes)));

            case HashType.HMACSHA256: return(Convert.ToBase64String(HMAC.Create().ComputeHash(inputBytes)));

            case HashType.HMACSHA384: return(Convert.ToBase64String(HMAC.Create().ComputeHash(inputBytes)));

            case HashType.HMACSHA512: return(Convert.ToBase64String(HMAC.Create().ComputeHash(inputBytes)));

            case HashType.MACTripleDES: return(Convert.ToBase64String(KeyedHashAlgorithm.Create().ComputeHash(inputBytes)));

            case HashType.MD5: return(Convert.ToBase64String(MD5.Create().ComputeHash(inputBytes)));

            case HashType.RIPEMD160: return(Convert.ToBase64String(RIPEMD160.Create().ComputeHash(inputBytes)));

            case HashType.SHA1: return(Convert.ToBase64String(SHA1.Create().ComputeHash(inputBytes)));

            case HashType.SHA256: return(Convert.ToBase64String(SHA256.Create().ComputeHash(inputBytes)));

            case HashType.SHA384: return(Convert.ToBase64String(SHA384.Create().ComputeHash(inputBytes)));

            case HashType.SHA512: return(Convert.ToBase64String(SHA512.Create().ComputeHash(inputBytes)));

            default: return(Convert.ToBase64String(inputBytes));
            }
        }
예제 #22
0
파일: Crypto.cs 프로젝트: sunxk/CmisSync
        /// <summary>
        /// Creates the hash algorithm by the given name.
        /// </summary>
        /// <returns>The hash algorithm.</returns>
        /// <param name="name">Name of the has algorithm.</param>
        public static HashAlgorithm CreateHashAlgorithm(string name)
        {
            name = name.ToLower();
            if (name.Equals("sha1") || name.Equals("sha-1"))
            {
                return(SHA1.Create());
            }

            if (name.Equals("sha256") || name.Equals("sha-256"))
            {
                return(SHA256.Create());
            }

            if (name.Equals("sha384") || name.Equals("sha-384"))
            {
                return(SHA384.Create());
            }

            if (name.Equals("sha512") || name.Equals("sha-512"))
            {
                return(SHA512.Create());
            }

            if (name.Equals("md5"))
            {
                return(MD5.Create());
            }

            if (name.Equals("ripemd160") || name.Equals("ripemd"))
            {
                return(RIPEMD160.Create());
            }

            return(HashAlgorithm.Create());
        }
예제 #23
0
        private void bgwRIPEMD160_DoWork(object sender, DoWorkEventArgs e)
        {
            string filePath = e.Argument.ToString();

            byte[] buffer;

            int bytesRead;

            long sizeOfFile, totalBytesToRead = 0;

            using (Stream fileStream = File.OpenRead(filePath))
            {
                sizeOfFile = fileStream.Length;

                using (HashAlgorithm ripemd160 = RIPEMD160.Create())
                {
                    do
                    {
                        buffer = new byte[4096];

                        bytesRead = fileStream.Read(buffer, 0, buffer.Length);

                        totalBytesToRead += bytesRead;

                        ripemd160.TransformBlock(buffer, 0, bytesRead, null, 0);

                        bgwRIPEMD160.ReportProgress((int)((double)totalBytesToRead / sizeOfFile * 100));
                    } while (bytesRead != 0);

                    ripemd160.TransformFinalBlock(buffer, 0, 0);

                    e.Result = MakeRIPEMD160HashString(ripemd160.Hash);
                }
            }
        }
예제 #24
0
            /// <summary>
            /// Transforms a file's bytes into an array of cryptographically strong data.
            /// </summary>
            /// <param name="path">The location of the file to hash.</param>
            /// <param name="hash">The algorithm to be used for hashing.</param>
            private byte[] GetFileHash(string path, HashType hash)
            {
                using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, (1024 * 1024)))
                {
                    switch (hash)
                    {
                    case HashType.MD5:
                        return(MD5.Create().ComputeHash(file));

                    case HashType.RIPEMD160:
                        return(RIPEMD160.Create().ComputeHash(file));

                    case HashType.SHA1:
                        return(SHA1.Create().ComputeHash(file));

                    case HashType.SHA256:
                        return(SHA256.Create().ComputeHash(file));

                    case HashType.SHA384:
                        return(SHA384.Create().ComputeHash(file));

                    case HashType.SHA512:
                        return(SHA512.Create().ComputeHash(file));

                    default:
                        return(null);
                    }
                }
            }
예제 #25
0
        static void Main(string[] args)
        {
            //--INPUT STRING
            Console.Write("Input Text To encrypt : ");
            string input = Console.ReadLine();

            //--MD5 ALGORITHM
            MD5 mD5 = MD5.Create();

            PrintOutput(GetHashMethode(mD5, input), "MD5");
            //--RIPEMD160 ALGORIHM
            RIPEMD160 rIPEMD160 = RIPEMD160.Create();

            PrintOutput(GetHashMethode(rIPEMD160, input), "RIPEMD160");
            //--SHA1 ALGORITHM
            SHA1 sHA1 = SHA1.Create();

            PrintOutput(GetHashMethode(sHA1, input), "SHA1");
            //--SHA256 ALGHORITHM
            SHA256 sHA256 = SHA256.Create();

            PrintOutput(GetHashMethode(sHA256, input), "SHA256");
            //--SHA384 ALGORITHM
            SHA384 sHA384 = SHA384.Create();

            PrintOutput(GetHashMethode(sHA384, input), "SHA384");
            //--SHA512 ALGORITHM
            SHA512 sHA512 = SHA512.Create();

            PrintOutput(GetHashMethode(sHA512, input), "SHA512");
        }
예제 #26
0
            /// <summary>
            /// Transforms a string into a cryptographically strong hash.
            /// </summary>
            /// <param name="input">The string to be transformed.</param>
            /// <param name="hash">The algorithm to used for hashing.</param>
            private byte[] GetHash(string input, HashType hash)
            {
                byte[] inputBytes = Encoding.ASCII.GetBytes(input);

                switch (hash)
                {
                case HashType.MD5:
                    return(MD5.Create().ComputeHash(inputBytes));

                case HashType.RIPEMD160:
                    return(RIPEMD160.Create().ComputeHash(inputBytes));

                case HashType.SHA1:
                    return(SHA1.Create().ComputeHash(inputBytes));

                case HashType.SHA256:
                    return(SHA256.Create().ComputeHash(inputBytes));

                case HashType.SHA384:
                    return(SHA384.Create().ComputeHash(inputBytes));

                case HashType.SHA512:
                    return(SHA512.Create().ComputeHash(inputBytes));

                default:
                    return(inputBytes);
                }
            }
예제 #27
0
        public void RIPEMD160abc()
        {
            var ripemd160 = RIPEMD160.Create();
            var result    = ripemd160.ComputeHash("abc".ToByteArray());

            Assert.AreEqual("8eb208f7e05d987a9b044a8e98c6b087f15a0bfc", result.ToHexString());
        }
예제 #28
0
        public static byte[] getHash(string password)
        {
            var r   = RIPEMD160.Create();
            var bfr = password.GetBytes().Concat(Salt).ToArray();

            return(r.ComputeHash(bfr));
        }
예제 #29
0
        public void RIPEMD160FullAlphabet()
        {
            var ripemd160 = RIPEMD160.Create();
            var result    = ripemd160.ComputeHash("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".ToByteArray());

            Assert.AreEqual("b0e20b6e3116640286ed3a87a5713079b21f5189", result.ToHexString());
        }
예제 #30
0
        public string ToPtsAddy()
        {
            var buffer    = ToBuffer(true);
            var firstHash = SHA256.Create().HashAndDispose(buffer);

            buffer.Clear();
            var secondHash = RIPEMD160.Create().HashAndDispose(firstHash);

            firstHash.Clear();
            var hash = new byte[] { 0x38 }.Concat(secondHash); // version 56(decimal)

            secondHash.Clear();
            var firstChecksum  = SHA256.Create().HashAndDispose(hash);
            var secondChecksum = SHA256.Create().HashAndDispose(firstChecksum);

            firstChecksum.Clear();
            var checksum = secondChecksum.Slice(0, 4);

            secondChecksum.Clear();
            buffer = hash.Concat(checksum);
            checksum.Clear();
            hash.Clear();
            var result = Base58CheckEncoding.EncodePlain(buffer);

            buffer.Clear();
            return(result);
        }