public async Task <bool> VerifyAsync(byte[] digest, string signature, string algorithm, CancellationToken token)
        {
            this.EnsureNotDisposed();
            if (digest == null || !digest.Any())
            {
                throw new ArgumentException("digest");
            }
            BasicHasher.ValidateDigestLength(algorithm, digest);
            var verifier = new Signing.Verifier(_rsa);

            var hex = digest.ToHexString();
            var res = await Task.FromResult(verifier.VerifyHash(hex, signature, algorithm));

            //var res = await Task.FromResult(verifier.VerifyHash(digest, signature, algorithm));

            if (!res)
            {
                // legacy code used a double digest hash, so hash once more and check
                var hashBytesLegacy = BasicHasher.GetHashBytes(digest, algorithm);
                res = await Task.FromResult(verifier.VerifyHash(digest, signature, algorithm));

                return(res);
            }
            return(res);
        }
Beispiel #2
0
        public async Task <Tuple <byte[], string> > SignAsync(string hex)
        {
            this.EnsureUsable();
            if (this.NotAfter < DateTime.UtcNow)
            {
                throw new EncryptionException($"Operation is not allowed on expired key; Key '{this.Actor}/{this.KeyId}'.");
            }
            var crypto    = this.GetRSACryptoServiceProvider();
            var signer    = new Signing.Signer(crypto);
            var algorithm = BasicHasher.GetNormalAlgorithm(hex);

            return(await Task.FromResult(signer.SignHash(hex, algorithm)));
        }
Beispiel #3
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);
        }
        public async Task <bool> VerifyAsync(string hex, string signature, CancellationToken token)
        {
            this.EnsureNotDisposed();
            string algorithm = BasicHasher.GetNormalAlgorithm(hex);
            var    verifier  = new Signing.Verifier(_rsa);
            var    res       = await Task.FromResult(verifier.VerifyHash(hex, signature, algorithm));

            if (!res)
            {
                // legacy code used a double digest hash, so hash once more and check
                var hashHexLegacy = BasicHasher.GetHash(hex, algorithm);
                res = await Task.FromResult(verifier.VerifyHash(hashHexLegacy, signature, algorithm));

                return(res);
            }
            return(res);
        }
        public void TestMethod1()
        {
            var toHash       = "pandacat";
            var expectedHash = "9b61a63183cffe1e988f7ccbe77b62c172384efbdf5d10c05f6d431cdf8641cd";
            var hashed       = BasicHasher.GetSha256Hash(toHash);

            Assert.AreEqual(expectedHash, hashed);

            //f1965c91412f0ac9cfd52c6ec54b91ba

            expectedHash = "f1965c91412f0ac9cfd52c6ec54b91ba";
            hashed       = BasicHasher.GetHash(toHash, "MD5");
            Assert.AreEqual(expectedHash, hashed);

            hashed = BasicHasher.GetMd5Hash(toHash);
            Assert.AreEqual(expectedHash, hashed);

            toHash       = "The quick brown fox jumped over the fence.";
            expectedHash = "cae59540cb24e2da1defb0e9b306f25c2e8fa114c265c03854cb75e5ed146b8e";
            hashed       = BasicHasher.GetSha256Hash(toHash);
            Assert.AreEqual(expectedHash, hashed);
        }
Beispiel #6
0
 public bool VerifyMd5Hash(string hashHex, string signedHash)
 {
     byte[] signedBytes = BasicHasher.ConvertFromHexOrBase64(signedHash);
     return(this.VerifyMd5Hash(hashHex, signedBytes));
 }
Beispiel #7
0
        // ----------

        public bool VerifyHash(string hashHex, string signedHash, string algorithm)
        {
            byte[] signedBytes = BasicHasher.ConvertFromHexOrBase64(signedHash);
            return(this.VerifyHash(hashHex, signedBytes, algorithm));
        }
        public void TestMethod1()
        {
            var cert2       = LoadCertificate();
            var key         = new X509Certificate2KeyEncryptionKey(cert2);
            var guid        = new Guid();
            var bytesToHash = Serializer.SerializeToByteArray(guid);

            //var algorithms = new string[] { "MD5", "SHA1", "SHA256" };
            foreach (var algorithm in BasicHasherAlgorithms.AllAlgorithms)
            {
                byte[] thisCopy = new byte[bytesToHash.Count()];
                bytesToHash.CopyTo(thisCopy, 0);
                Assert.IsTrue(bytesToHash.SequenceEqual(thisCopy));

                var hashBytes = BasicHasher.GetHashBytes(bytesToHash, algorithm);

                var hashAsHex1 = hashBytes.ToHexString();
                Console.WriteLine("Hash is: " + hashAsHex1);

                var res = key.SignAsync(hashBytes, algorithm).GetAwaiter().GetResult();
                Assert.IsNotNull(res.Item1);
                Assert.IsNotNull(res.Item2);

                var hex = res.Item1.ToHexString();
                Console.WriteLine("hex len\t" + hex.Length.ToString());
                var b64 = Convert.ToBase64String(res.Item1);
                Console.WriteLine("b64 len\t" + b64.Length.ToString());

                //var item2AsByte = Format.HexStringToByteArray(res.Item2);
                //Assert.IsTrue(item2AsByte.SequenceEqual(res.Item1));

                //// check digest
                //var digest2 = BasicHasher.GetHashBytes(bytesToHash, algorithm);
                //Assert.IsTrue(digest2.SequenceEqual(hashBytes));

                var isValid = key.VerifyAsync(hashBytes, res.Item1, algorithm).GetAwaiter().GetResult();
                Assert.IsTrue(isValid);


                //var isValid2 = key.VerifyAsync(hashBytes, res.Item2, algorithm).GetAwaiter().GetResult();
                //Assert.IsTrue(isValid2);

                //Assert.IsTrue(bytesToHash.SequenceEqual(thisCopy));

                var hashHex = BasicHasher.GetHash(bytesToHash, algorithm);
                Console.WriteLine("Hash is now: " + hashHex);

                var asBytes = Format.HexStringToByteArray(hashHex);

                Assert.IsTrue(asBytes.SequenceEqual(hashBytes));


                var isValidHex = key.VerifyAsync(hashHex, res.Item2).GetAwaiter().GetResult();
                Assert.IsTrue(isValidHex);

                var hexSigned = key.SignAsync(hashHex).GetAwaiter().GetResult();
                isValidHex = key.VerifyAsync(hashHex, hexSigned.Item2).GetAwaiter().GetResult();
                Assert.IsTrue(isValidHex);

                Console.WriteLine(algorithm + " passed");
            }


            //var res = key.SignAsync(bytesToHash, "MD5").GetAwaiter().GetResult();
            //Assert.IsNotNull(res.Item1);
            //Assert.IsNotNull(res.Item2);

            //var isValid = key.VerifyAsync(bytesToHash, res.Item1, "MD5").GetAwaiter().GetResult();
            //Assert.IsTrue(isValid);

            //var isValid2 = key.VerifyAsync(bytesToHash, res.Item2, "MD5").GetAwaiter().GetResult();
            //Assert.IsTrue(isValid2);

            //var md5HashHex = BasicHasher.GetMd5Hash(bytesToHash); //ChecksumHash.GetMD5Hash(bytesToMd5Hash);
            //var isValidHex = key.VerifyAsync(md5HashHex, res.Item2).GetAwaiter().GetResult();
            //Assert.IsTrue(isValidHex);

            //var signedHex = key.SignAsync(md5HashHex).GetAwaiter().GetResult();
            //isValidHex = key.VerifyAsync(md5HashHex, signedHex.Item2).GetAwaiter().GetResult();
            //Assert.IsTrue(isValidHex);

            //// sha1

            //res = key.SignAsync(bytesToHash, "SHA1").GetAwaiter().GetResult();
            //Assert.IsNotNull(res.Item1);
            //Assert.IsNotNull(res.Item2);

            //isValid = key.VerifyAsync(bytesToHash, res.Item1, "SHA1").GetAwaiter().GetResult();
            //Assert.IsTrue(isValid);

            //isValid2 = key.VerifyAsync(bytesToHash, res.Item2, "SHA1").GetAwaiter().GetResult();
            //Assert.IsTrue(isValid2);

            //var sha1HashHex = BasicHasher.GetSha1Hash(bytesToHash);
            //isValidHex = key.VerifyAsync(sha1HashHex, res.Item2).GetAwaiter().GetResult();
            //Assert.IsTrue(isValidHex);

            //var sha1HexSigned = key.SignAsync(sha1HashHex).GetAwaiter().GetResult();
            //isValidHex = key.VerifyAsync(sha1HashHex, sha1HexSigned.Item2).GetAwaiter().GetResult();
            //Assert.IsTrue(isValidHex);
        }