Ejemplo n.º 1
0
        /// <summary>
        /// Get the checksum of a file and return as a string
        /// </summary>
        /// <param name="file">File name of file to get hash from</param>
        /// <param name="hashOption">Default = SHA1, also MD5 and CRC32</param>
        /// <param name="headerlength">Number of bytes to skip prior to computing hash. Should try both platform header length and 0 when comparing to known checksum to account for Rom files with dumped intros. Default = 0.</param>
        /// <returns>Returns hash as hex string</returns>
        public static string GetHash(string file, HashOption hashOption = HashOption.SHA1, int headerlength = 0)
        {
            string hash;

            using (FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                hash = GetHash(stream, hashOption, headerlength);
            }
            return(hash);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get the CRC of a file stream and return as a string
        /// </summary>
        /// <param name="stream">FileStream</param>
        /// <param name="hashOption">Default = SHA1, also MD5 and CRC32</param>
        /// <param name="headerlength">Number of bytes to skip prior to computing hash. Should try both platform header length and 0 when comparing to known checksum to account for Rom files with dumped intros. Default = 0.</param>
        /// <returns>Returns hash as hex string</returns>
        public static string GetHash(Stream stream, HashOption hashOption = HashOption.SHA1, int headerlength = 0)
        {
            string hash         = "";
            int    streamLength = (int)stream.Length;

            if (streamLength < headerlength)
            {
                return("");
            }
            // Read header and discard
            stream.Seek(headerlength, SeekOrigin.Begin);

            // Read everything after the header into a byte array
            byte[] buffer = new byte[streamLength - headerlength];
            stream.Read(buffer, 0, (streamLength - headerlength));

            switch (hashOption)
            {
            case HashOption.CRC32:
                var crc32 = new Ionic.Crc.CRC32();
                foreach (byte b in buffer)
                {
                    crc32.UpdateCRC(b);
                }
                hash = crc32.Crc32Result.ToString("x8").ToUpper();
                break;

            case HashOption.MD5:
                var    md5       = MD5.Create();
                byte[] hashBytes = md5.ComputeHash(buffer);
                hash = BitConverter.ToString(hashBytes).Replace("-", String.Empty).ToUpper();
                break;

            case HashOption.SHA1:
                SHA1Managed managedSHA1 = new SHA1Managed();
                byte[]      shaBuffer   = managedSHA1.ComputeHash(buffer);
                foreach (byte b in shaBuffer)
                {
                    hash += b.ToString("x2").ToUpper();
                }
                break;
            }

            return(hash);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Computes the hash digest.
        /// </summary>
        /// <returns>The hash digest.</returns>
        /// <param name="hashString">Hash string.</param>
        /// <param name="preSharedKey">Pre shared key.</param>
        /// <param name="hashMethod">Hash method.</param>
        public static string ComputeHashDigest(string hashString, string preSharedKey, HashOption hashMethod)
        {
            byte[] numArray;
            var    hash = StringToByteArray(hashString);
            var    pre  = StringToByteArray(preSharedKey);

            if (hashMethod == HashOption.Sha1)
            {
                numArray = (new SHA1CryptoServiceProvider()).ComputeHash(hash);
            }
            else
            {
                throw new InvalidOperationException("Invalid hash method");
            }

            return(ByteArrayToHexString(numArray));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Subs the trans.
        /// </summary>
        /// <param name="request">Request.</param>
        /// <param name="merchantPwd">Merchant password.</param>
        /// <param name="preSharedKey">Pre shared key.</param>
        /// <param name="postUrl">Post URL.</param>
        /// <param name="hashMethod">Hash method.</param>
        public void Submit(Transaction request, string merchantPwd, string preSharedKey, string postUrl, HashOption hashMethod = HashOption.Sha1)
        {
            //before submit - Validade Fields of Request Transaction
            //ValidateTypeFieldsTransaction(request);


            string[] strArrays = { merchantPwd, preSharedKey, postUrl };

            var remPost = new Remote(_context, postUrl, DefaultMethod.Post);

            var nvCollection = new NameValueCollection();

            nvCollection.Add("PreSharedKey", preSharedKey);
            nvCollection.Add("MerchantID", MerchantId);
            nvCollection.Add("Password", merchantPwd);

            var requestNVCol = request.ToNameValueCollection();

            for (int i = 0; i < requestNVCol.AllKeys.Length; i++)
            {
                var key = requestNVCol.AllKeys[i];
                nvCollection.Add(key, requestNVCol.GetValues(key)[0]);
                remPost.AddInput(key, requestNVCol.GetValues(key)[0]);
            }

            var qStr = nvCollection.ToQueryString(false, false);

            if (qStr == null)
            {
                throw new ArgumentNullException("qStr Error!");
            }

            var digest = Hash.ComputeHashDigest(qStr, preSharedKey, hashMethod);

            if (digest == null)
            {
                throw new ArgumentNullException("digest Error!");
            }

            remPost.AddInput("ThreeDSecureCompatMode", "false");
            remPost.AddInput("ServerResultCompatMode", "false");
            remPost.AddInput("HashDigest", digest);
            remPost.AddInput("MerchantID", MerchantId);

            remPost.Post("CardsavePaymentForm");
        }