Ejemplo n.º 1
0
        public string DoHash(string hashAlgorithm, string txtToHash)
        {
            this.error.cleanError();
            HashAlgorithm hash = HashAlgorithmUtils.getHashAlgorithm(hashAlgorithm, this.error);

            byte[] resBytes = calculateHash(hash, txtToHash);
            string result   = toHexaString(resBytes);

            if (!this.error.existsError())
            {
                return(result);
            }
            return("");
        }
Ejemplo n.º 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("");
        }
        private IDigest GetHash(string hashAlgorithm)
        {
            HashAlgorithm hash = HashAlgorithmUtils.getHashAlgorithm(hashAlgorithm, this.error);

            if (this.HasError())
            {
                return(null);
            }
            Hashing hashing = new Hashing();
            IDigest digest  = hashing.createHash(hash);

            if (hashing.HasError())
            {
                this.error = hashing.GetError();
                return(null);
            }
            return(digest);
        }
 private HashUtils.HashAlgorithm getHashAlgorithm(ChecksumAlgorithm checksumAlgorithm)
 {
     return(HashAlgorithmUtils.getHashAlgorithm(ChecksumAlgorithmUtils.valueOf(checksumAlgorithm, this.error), this.error));
 }
        /********EXTERNAL OBJECT PUBLIC METHODS  - END ********/


        /// <summary>
        /// Encrypts the string encoded plain text
        /// </summary>
        /// <param name="asymmetricEncryptionAlgorithm">string AsymmetricEncryptionAlgorithm enum, algorithm name</param>
        /// <param name="hashAlgorithm">string HashAlgorithm enum, algorithm name</param>
        /// <param name="asymmetricEncryptionPadding">string AsymmetricEncryptionPadding enum, padding name</param>
        /// <param name="keyPath">string path to key/certificate</param>
        /// <param name="isPrivate">boolean true if key is private, false if it is public</param>
        /// <param name="alias">string keystore/certificate pkcs12 format alias</param>
        /// <param name="password">Srting keysore/certificate pkcs12 format alias</param>
        /// <param name="plainText">string to encrypt</param>
        /// <returns>string Base64 encrypted plainText text</returns>
        private string DoEncryptInternal(string hashAlgorithm, string asymmetricEncryptionPadding, Key key, bool isPrivate, string plainText)
        {
            this.error.cleanError();

            HashAlgorithm hash = HashAlgorithmUtils.getHashAlgorithm(hashAlgorithm, this.error);
            AsymmetricEncryptionPadding padding = AsymmetricEncryptionPaddingUtils.getAsymmetricEncryptionPadding(asymmetricEncryptionPadding, this.error);

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

            string asymmetricEncryptionAlgorithm = "";
            AsymmetricKeyParameter asymKey       = null;

            if (isPrivate)
            {
                PrivateKeyManager keyMan = (PrivateKeyManager)key;
                if (!keyMan.HasPrivateKey || keyMan.HasError())
                {
                    this.error = keyMan.GetError();
                    return("");
                }
                asymmetricEncryptionAlgorithm = keyMan.getPrivateKeyAlgorithm();

                asymKey = keyMan.getPrivateKeyParameterForEncryption();
                if (keyMan.HasError())
                {
                    this.error = keyMan.GetError();
                    return("");
                }
            }
            else
            {
                CertificateX509 cert = (CertificateX509)key;
                if (!cert.Inicialized || cert.HasError())
                {
                    this.error = cert.GetError();
                    return("");
                }
                asymmetricEncryptionAlgorithm = cert.getPublicKeyAlgorithm();
                asymKey = cert.getPublicKeyParameterForEncryption();
                if (cert.HasError())
                {
                    this.error = cert.GetError();
                    return("");
                }
            }

            AsymmetricEncryptionAlgorithm algorithm = AsymmetricEncryptionAlgorithmUtils
                                                      .getAsymmetricEncryptionAlgorithm(asymmetricEncryptionAlgorithm, this.error);

            try
            {
                return(doEncrypt(algorithm, hash, padding, asymKey, plainText));
            }
            catch (InvalidCipherTextException)
            {
                this.error.setError("AE036", "Algoritmo inválido" + algorithm);

                return("");
            }
        }