public Tuple <byte[], string> SignHash(byte[] hashedBytes, string algorithm)
        {
            if (hashedBytes == null)
            {
                throw new ArgumentNullException(nameof(hashedBytes));
            }
            if (algorithm == null)
            {
                throw new ArgumentNullException(nameof(algorithm));
            }
            algorithm = BasicHasherAlgorithms.VerifyAndMapToAlogrithm(algorithm);

#if DEBUG
            var hashHex = hashedBytes.ToHexString();
            Trace.WriteLine("Signing\t" + hashHex + "\t" + algorithm);
#endif

            byte[] signedHash = null;
            try
            {
                signedHash = _privateKey.SignHash(hashedBytes, CryptoConfig.MapNameToOID(algorithm));
            }
            catch (CryptographicException ex)
            {
                if (ex.Message == "Bad Hash.")
                {
                    var cryptoEx = new CryptographicException("Bad Hash; Use BasicHasher.GetMd5HashBytes() to generate a proper hash before calling this method.");
                }
                else
                {
                    throw;
                }
            }

            string res2;
            if (_encoding == EncodingOption.Base64String)
            {
                res2 = Convert.ToBase64String(signedHash);
            }
            else if (_encoding == EncodingOption.HexString)
            {
                res2 = signedHash.ToHexString();
            }
            else
            {
                throw new NotImplementedException(_encoding.ToString());
            }
#if DEBUG
            Trace.WriteLine("Signed\t" + hashHex + "\t" + algorithm + "\tresult\t" + res2);
#endif
            return(new Tuple <byte[], string>(signedHash, res2));
        }
Example #2
0
        public bool VerifyHash(byte[] hashedBytes, string signedHash, string algorithm)
        {
            algorithm = BasicHasherAlgorithms.VerifyAndMapToAlogrithm(algorithm);
            BasicHasher.ValidateDigestLength(algorithm, hashedBytes);

            byte[] signedBytes = BasicHasher.ConvertFromHexOrBase64(signedHash);
            var    isValid     = _publicKey.VerifyHash(hashedBytes, algorithm, signedBytes);

#if DEBUG
            var hashHex = hashedBytes.ToHexString();
            Trace.WriteLine("VerifyHash\t" + hashHex + "\t" + algorithm + "\tsig\t" + signedHash + "\tresult\t" + isValid.ToString().ToLower());
#endif
            return(isValid);
        }