Ejemplo n.º 1
0
        public byte[] CalculateRawSignature(AsymmetricKeyParameter privateKey, byte[] md5andsha1)
        {
            // Note: Only use the SHA1 part of the hash
            ISigner sig = new DsaDigestSigner(new DsaSigner(), new NullDigest());

            sig.Init(true, privateKey);
            sig.BlockUpdate(md5andsha1, 16, 20);
            return(sig.GenerateSignature());
        }
Ejemplo n.º 2
0
        public virtual byte[] CalculateRawSignature(SecureRandom random,
                                                    AsymmetricKeyParameter privateKey, byte[] md5andsha1)
        {
            // Note: Only use the SHA1 part of the hash
            ISigner sig = new DsaDigestSigner(CreateDsaImpl(), new NullDigest());

            sig.Init(true, new ParametersWithRandom(privateKey, random));
            sig.BlockUpdate(md5andsha1, 16, 20);
            return(sig.GenerateSignature());
        }
        public byte[] Sign(byte[] data, string privateKey)
        {
            var key = PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));

            var dsaDigestSigner = new DsaDigestSigner(new DsaSigner(), new Sha1Digest());

            dsaDigestSigner.Init(true, key);

            dsaDigestSigner.BlockUpdate(data, 0, data.Length);
            return(dsaDigestSigner.GenerateSignature());
        }
        public bool Verify(byte[] data, byte[] signature, string publicKey)
        {
            var key = PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));

            var dsaDigestSigner = new DsaDigestSigner(new DsaSigner(), new Sha1Digest());

            dsaDigestSigner.Init(false, key);

            dsaDigestSigner.BlockUpdate(data, 0, data.Length);

            return(dsaDigestSigner.VerifySignature(signature));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Signs the passed in data with a private key
        /// </summary>
        /// <param name="privateKey">the private key used to create the signature</param>
        /// <param name="data">The data to sign</param>
        /// <returns>the signature as a byte array</returns>
        public byte[] Sign(byte[] privateKey, byte[] data)
        {
            var signer  = new DsaDigestSigner(new DsaSigner(), new Sha1Digest());
            var privKey = (DsaPrivateKeyParameters)CreateAsymmetricKeyParameterFromPrivateKeyInfo(privateKey);

            signer.Init(true, privKey);
            signer.BlockUpdate(data, 0, data.Length);
            byte[] signature;
            try
            {
                signature = signer.GenerateSignature();
            }
            catch (Exception exception)
            {
                string message = "Signature Failure!\n" +
                                 $"{exception.Message}.\n" +
                                 $"The private key file is corrupted, verify private key file or try another key.\n" +
                                 $"If all fails create a new key pair.";
                throw new CryptoException(message, exception);
            }
            return(signature);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Verifies a signature to be authentic
        /// </summary>
        /// <param name="originalSignature">The signature which is be verified</param>
        /// <param name="publicKey">the public key used for the verification</param>
        /// <param name="data">the data which is signed</param>
        /// <returns>true if signature is authentic, false if not</returns>
        public bool Verify(byte[] originalSignature, byte[] publicKey, byte[] data)
        {
            var signer = new DsaDigestSigner(new DsaSigner(), new Sha1Digest());

            DsaPublicKeyParameters pubKey = null;

            try
            {
                pubKey = (DsaPublicKeyParameters)CreateAsymmetricKeyParameterFromPublicKeyInfo(publicKey);
            }
            catch (Exception exception)
            {
                string message = "Public Key Creation Failure!\n" +
                                 $"{exception.Message}.\n" +
                                 $"The public key file is corrupted, verify public key file or try another key.\n" +
                                 $"If all fails create a new key pair.";
                throw new CryptoException(message, exception);
            }
            signer.Init(false, pubKey);
            signer.BlockUpdate(data, 0, data.Length);
            return(signer.VerifySignature(originalSignature));
        }