Ejemplo n.º 1
0
        public static byte[] Create(string message, HashAlgorithms hashAlgorithm, X509Certificate2 certificate)
        {
            RSACryptoServiceProvider csp = (RSACryptoServiceProvider)certificate.PrivateKey;

            if (csp == null)
            {
                throw new Exception("Valid certificate was not found");
            }

            UnicodeEncoding encoding = new UnicodeEncoding();

            byte[] data = encoding.GetBytes(message); //current message encripted in bytes
            byte[] hash = null;                       //hash result

            if (hashAlgorithm.Equals(HashAlgorithms.SHA1))
            {
                SHA1Managed sha1 = new SHA1Managed();
                hash = sha1.ComputeHash(data);
            }
            else if (hashAlgorithm.Equals(HashAlgorithms.SHA256))
            {
                SHA256Managed sha256 = new SHA256Managed();
                hash = sha256.ComputeHash(data);
            }

            return(csp.SignHash(hash, CryptoConfig.MapNameToOID(hashAlgorithm.ToString())));
        }
Ejemplo n.º 2
0
        protected override void ProcessRecord()
        {
            string         hash = string.Empty;
            FileSystemInfo item = null;

            foreach (var path in paths)
            {
                try
                {
                    item = GetFileSystemInfo2(path) as FileInfo;
                    if (item == null)
                    {
                        return;
                    }
                }
                catch (Exception ex)
                {
                    WriteError(new ErrorRecord(ex, "ReadFileError", ErrorCategory.OpenError, path));
                    continue;
                }

                try
                {
                    hash = ((FileInfo)item).GetHash(algorithm);
                }
                catch (UnauthorizedAccessException)
                {
                    try
                    {
                        var ownerInfo     = FileSystemOwner.GetOwner(item);
                        var previousOwner = ownerInfo.Owner;

                        FileSystemOwner.SetOwner(item, System.Security.Principal.WindowsIdentity.GetCurrent().User);

                        hash = ((FileInfo)item).GetHash(algorithm);

                        FileSystemOwner.SetOwner(item, previousOwner);
                    }
                    catch (Exception ex2)
                    {
                        WriteError(new ErrorRecord(ex2, "GetHashError", ErrorCategory.WriteError, path));
                    }
                }
                catch (Exception ex)
                {
                    WriteError(new ErrorRecord(ex, "GetHashError", ErrorCategory.WriteError, path));
                }

                var result = new PSObject(item);
                result.Properties.Add(new PSNoteProperty("Hash", hash));
                result.Properties.Add(new PSNoteProperty("Algorithm", algorithm.ToString()));
                result.TypeNames.Insert(0, "Alphaleonis.Win32.Filesystem.FileInfo+Hash");
                WriteObject(result);
            }
        }
Ejemplo n.º 3
0
        public static bool Verify(string message, HashAlgorithms hashAlgorithm, byte[] signature, X509Certificate2 certificate)
        {
            RSACryptoServiceProvider csp      = (RSACryptoServiceProvider)certificate.PublicKey.Key;
            UnicodeEncoding          encoding = new UnicodeEncoding();

            byte[] data = encoding.GetBytes(message);
            byte[] hash = null;

            if (hashAlgorithm.Equals(HashAlgorithms.SHA1))
            {
                SHA1Managed sha1 = new SHA1Managed();
                hash = sha1.ComputeHash(data);
            }
            else if (hashAlgorithm.Equals(HashAlgorithms.SHA256))
            {
                SHA256Managed sha256 = new SHA256Managed();
                hash = sha256.ComputeHash(data);
            }

            return(csp.VerifyHash(hash, CryptoConfig.MapNameToOID(hashAlgorithm.ToString()), signature));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Verilen input verisinin istenen algoritmaya göre hash'ini üretir.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="alg"></param>
        /// <returns></returns>
        public byte[] GetHash(byte[] input, HashAlgorithms alg)
        {
            HashAlgorithm hash = HashAlgorithm.Create(alg.ToString());

            return(hash.ComputeHash(input));
        }