private string CalculateHash(byte[] input, ChecksumAlgorithm checksumAlgorithm)
        {
            HashUtils.HashAlgorithm alg = getHashAlgorithm(checksumAlgorithm);
            if (this.HasError())
            {
                return("");
            }
            Hashing hash = new Hashing();

            byte[] digest = hash.calculateHash(alg, input);
            if (hash.HasError())
            {
                this.error = hash.GetError();
                return("");
            }
            return(toHexaString(digest));
        }
Beispiel #2
0
        public string calculate(string plainText, string password, string algorithm)
        {
            byte[] pass = SecurityUtils.GetHexa(password, "HS002", this.error);
            if (this.HasError())
            {
                return("");
            }

            EncodingUtil eu = new EncodingUtil();

            byte[] inputBytes = eu.getBytes(plainText);
            if (this.HasError())
            {
                return("");
            }
            Hashing hash = new Hashing();

            HashUtils.HashAlgorithm alg = HashAlgorithmUtils.getHashAlgorithm(algorithm, this.error);
            if (this.HasError())
            {
                return("");
            }
            IDigest digest = hash.createHash(alg);
            HMac    engine = new HMac(digest);

            try
            {
                engine.Init(new KeyParameter(pass));
            }catch (Exception e)
            {
                this.error.setError("HS003", e.Message);
                return("");
            }
            byte[] resBytes = new byte[engine.GetMacSize()];
            engine.BlockUpdate(inputBytes, 0, inputBytes.Length);
            engine.DoFinal(resBytes, 0);

            string result = toHexastring(resBytes);

            if (!this.error.existsError())
            {
                return(result);
            }
            return("");
        }