/// <summary>
        /// Preparing params for request
        /// </summary>
        /// <param name="public_key">Public API-key</param>
        /// <param name="private_key">Private API-key</param>
        public void Prepare(string public_key, string private_key)
        {
            Key    = public_key;
            Params = Params ?? new Dictionary <string, string>();
            Params.Add("key", public_key);

            Data = JsonConvert.SerializeObject(Params);

            Signature = SHA512HMAC.Generate(Data, private_key);
        }
示例#2
0
 /// <summary>
 /// Initializes all required instances.
 /// </summary>
 public HashAlgorithmRegister()
 {
     psi_128bit    = new implementations.psi.utility.UsageHelper_Hash();
     md5           = new MD5();
     md5hmac       = new MD5HMAC();
     ripemd160     = new RIPEMD160();
     ripemd160hmac = new RIPEMD160HMAC();
     sha1          = new SHA1();
     sha1hmac      = new SHA1HMAC();
     sha256        = new SHA256();
     sha256hmac    = new SHA256HMAC();
     sha384        = new SHA384();
     sha384hmac    = new SHA384HMAC();
     sha512        = new SHA512();
     sha512hmac    = new SHA512HMAC();
 }
示例#3
0
        /// <summary>
        /// Returns the SHA512 HMAC hash for the file
        /// </summary>
        /// <param name="FilePath">Path to input file</param>
        /// <param name="HashKey">HMAC key</param>
        /// <returns>Computed hash value [byte[]]</returns>
        internal byte[] GetChecksum(string FilePath, byte[] HashKey)
        {
            using (SHA512HMAC hmac = new SHA512HMAC(HashKey))
            {
                int    blockSize  = hmac.BlockSize;
                int    bytesTotal = 0;
                byte[] buffer     = new byte[blockSize];
                byte[] chkSum     = new byte[hmac.DigestSize];

                using (BinaryReader inputReader = new BinaryReader(new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.None)))
                {
                    // start of file bytes
                    inputReader.BaseStream.Position = MessageHeader.GetHeaderSize;
                    int bytesRead = 0;

                    // read into mac
                    while ((bytesRead = inputReader.Read(buffer, 0, blockSize)) == blockSize)
                    {
                        hmac.BlockUpdate(buffer, 0, bytesRead);
                        bytesTotal += bytesRead;

                        if (bytesTotal > MAX_SIGNED)
                        {
                            break;
                        }
                    }

                    // last block
                    if (bytesRead > 0)
                    {
                        hmac.BlockUpdate(buffer, 0, bytesRead);
                    }

                    // get the hash
                    hmac.DoFinal(chkSum, 0);
                }

                return(chkSum);
            }
        }
示例#4
0
        private void CompareBlocks(HmacAlg Algorithm)
        {
            if (Algorithm == HmacAlg.Sha256Hmac)
            {
                byte[] hashKey = new byte[32];
                byte[] buffer = new byte[640];
                byte[] hash1 = new byte[32];
                byte[] hash2 = new byte[32];

                using (System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
                {
                    rng.GetBytes(hashKey);
                    rng.GetBytes(buffer);
                }

                using (System.Security.Cryptography.HMACSHA256 hmac = new System.Security.Cryptography.HMACSHA256(hashKey))
                    hash1 = hmac.ComputeHash(buffer);

                // test 1: HMAC interface
                HMAC hmac1 = new HMAC(new SHA256Digest());
                hmac1.Init(hashKey);
                hmac1.BlockUpdate(buffer, 0, buffer.Length);
                hmac1.DoFinal(hash2, 0);

                if (!Compare.AreEqual(hash2, hash1))
                    throw new Exception("hmac is not equal!");

                // test 2: class with dofinal
                using (SHA256HMAC hmac = new SHA256HMAC())
                {
                    hmac.Init(hashKey);
                    hmac.BlockUpdate(buffer, 0, buffer.Length);
                    hmac.DoFinal(hash2, 0);
                }

                if (!Compare.AreEqual(hash2, hash1))
                    throw new Exception("hmac1 is not equal!");

                // test 3: class with computemac
                using (SHA256HMAC hmac = new SHA256HMAC(hashKey))
                    hash2 = hmac.ComputeMac(buffer);

                if (!Compare.AreEqual(hash2, hash1))
                    throw new Exception("hmac2 is not equal!");
            }
            else
            {
                // SHA512 //
                byte[] hash1 = new byte[64];
                byte[] hash2 = new byte[64];
                byte[] hashKey = new byte[64];
                byte[] buffer = new byte[128];

                using (System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
                {
                    rng.GetBytes(hashKey);
                    rng.GetBytes(buffer);
                }

                using (System.Security.Cryptography.HMACSHA512 hmac = new System.Security.Cryptography.HMACSHA512(hashKey))
                    hash1 = hmac.ComputeHash(buffer);

                // test 1: HMAC interface
                HMAC hmac1 = new HMAC(new SHA512Digest());
                hmac1.Init(hashKey);
                hmac1.BlockUpdate(buffer, 0, buffer.Length);
                hmac1.DoFinal(hash2, 0);

                if (!Compare.AreEqual(hash2, hash1))
                    throw new Exception("hmac1 is not equal!");

                // test 2: class with dofinal
                using (SHA512HMAC hmac = new SHA512HMAC())
                {
                    hmac.Init(hashKey);
                    hmac.BlockUpdate(buffer, 0, buffer.Length);
                    hmac.DoFinal(hash2, 0);
                }

                if (!Compare.AreEqual(hash2, hash1))
                    throw new Exception("hmac1 is not equal!");

                // test 3: class with computemac
                using (SHA512HMAC hmac = new SHA512HMAC(hashKey))
                    hash2 = hmac.ComputeMac(buffer);

                if (!Compare.AreEqual(hash2, hash1))
                    throw new Exception("hmac2 is not equal!");
            }
        }
示例#5
0
        private void CompareBlocks(HmacAlg Algorithm)
        {
            if (Algorithm == HmacAlg.Sha256Hmac)
            {
                byte[] hashKey = new byte[32];
                byte[] buffer  = new byte[640];
                byte[] hash1   = new byte[32];
                byte[] hash2   = new byte[32];

                using (System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
                {
                    rng.GetBytes(hashKey);
                    rng.GetBytes(buffer);
                }

                using (System.Security.Cryptography.HMACSHA256 hmac = new System.Security.Cryptography.HMACSHA256(hashKey))
                    hash1 = hmac.ComputeHash(buffer);


                // test 1: HMAC interface
                HMAC hmac1 = new HMAC(new SHA256Digest());
                hmac1.Init(hashKey);
                hmac1.BlockUpdate(buffer, 0, buffer.Length);
                hmac1.DoFinal(hash2, 0);

                if (!Compare.AreEqual(hash2, hash1))
                {
                    throw new Exception("hmac is not equal!");
                }


                // test 2: class with dofinal
                using (SHA256HMAC hmac = new SHA256HMAC())
                {
                    hmac.Init(hashKey);
                    hmac.BlockUpdate(buffer, 0, buffer.Length);
                    hmac.DoFinal(hash2, 0);
                }

                if (!Compare.AreEqual(hash2, hash1))
                {
                    throw new Exception("hmac1 is not equal!");
                }


                // test 3: class with computemac
                using (SHA256HMAC hmac = new SHA256HMAC(hashKey))
                    hash2 = hmac.ComputeMac(buffer);

                if (!Compare.AreEqual(hash2, hash1))
                {
                    throw new Exception("hmac2 is not equal!");
                }
            }
            else
            {
                // SHA512 //
                byte[] hash1   = new byte[64];
                byte[] hash2   = new byte[64];
                byte[] hashKey = new byte[64];
                byte[] buffer  = new byte[128];

                using (System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
                {
                    rng.GetBytes(hashKey);
                    rng.GetBytes(buffer);
                }

                using (System.Security.Cryptography.HMACSHA512 hmac = new System.Security.Cryptography.HMACSHA512(hashKey))
                    hash1 = hmac.ComputeHash(buffer);


                // test 1: HMAC interface
                HMAC hmac1 = new HMAC(new SHA512Digest());
                hmac1.Init(hashKey);
                hmac1.BlockUpdate(buffer, 0, buffer.Length);
                hmac1.DoFinal(hash2, 0);

                if (!Compare.AreEqual(hash2, hash1))
                {
                    throw new Exception("hmac1 is not equal!");
                }


                // test 2: class with dofinal
                using (SHA512HMAC hmac = new SHA512HMAC())
                {
                    hmac.Init(hashKey);
                    hmac.BlockUpdate(buffer, 0, buffer.Length);
                    hmac.DoFinal(hash2, 0);
                }

                if (!Compare.AreEqual(hash2, hash1))
                {
                    throw new Exception("hmac1 is not equal!");
                }


                // test 3: class with computemac
                using (SHA512HMAC hmac = new SHA512HMAC(hashKey))
                    hash2 = hmac.ComputeMac(buffer);

                if (!Compare.AreEqual(hash2, hash1))
                {
                    throw new Exception("hmac2 is not equal!");
                }
            }
        }
示例#6
0
        /// <summary>
        /// Returns the SHA512 HMAC hash for the file
        /// </summary>
        /// <param name="FilePath">Path to input file</param>
        /// <param name="HashKey">HMAC key</param>
        /// <returns>Computed hash value [byte[]]</returns>
        public byte[] GetChecksum(string FilePath, byte[] HashKey)
        {
            using (SHA512HMAC hmac = new SHA512HMAC(HashKey))
            {
                int blockSize = hmac.BlockSize;
                byte[] buffer = new byte[blockSize];
                byte[] chkSum = new byte[hmac.DigestSize];

                using (BinaryReader inputReader = new BinaryReader(new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.None)))
                {
                    // start of file bytes
                    inputReader.BaseStream.Position = MessageHeader.GetHeaderSize;
                    int bytesRead = 0;

                    // read into mac
                    while ((bytesRead = inputReader.Read(buffer, 0, blockSize)) == blockSize)
                        hmac.BlockUpdate(buffer, 0, bytesRead);

                    // last block
                    if (bytesRead > 0)
                        hmac.BlockUpdate(buffer, 0, bytesRead);

                    // get the hash
                    hmac.DoFinal(chkSum, 0);
                }

                return chkSum;
            }
        }