Exemple #1
20
 public static HashAlgorithm GetHashProvider(HashType type)
 {
     HashAlgorithm hash = null;
     switch (type)
     {
         case HashType.MD5:
             {
                 hash = new MD5CryptoServiceProvider();
                 break;
             }
         case HashType.SHA1:
             {
                 hash = new SHA1CryptoServiceProvider();
                 break;
             }
         case HashType.SHA256:
             {
                 hash = new SHA256CryptoServiceProvider();
                 break;
             }
         case HashType.SHA384:
             {
                 hash = new SHA384CryptoServiceProvider();
                 break;
             }
         case HashType.SHA512:
             {
                 hash = new SHA512CryptoServiceProvider();
                 break;
             }
     }
     return hash;
 }
Exemple #2
0
        private HashAlgorithm GetHashAlgorithm(string hashType)
        {
            HashAlgorithm hash = null;
            string hashTypeLower = hashType.ToLowerInvariant();

            switch (hashTypeLower)
            {
                case "md5":
                    hash = new MD5CryptoServiceProvider();
                    break;
                case "ripemd160":
                    hash = new RIPEMD160Managed();
                    break;
                case "sha1":
                    hash = new SHA1CryptoServiceProvider();
                    break;
                case "sha256":
                    hash = new SHA256CryptoServiceProvider();
                    break;
                case "sha384":
                    hash = new SHA384CryptoServiceProvider();
                    break;
                case "sha512":
                    hash = new SHA512CryptoServiceProvider();
                    break;
                default:
                    break;
            }

            return hash;
        }
 private static string GetHash(string s)
 {
     SHA512 sec = new SHA512CryptoServiceProvider();
     ASCIIEncoding enc = new ASCIIEncoding();
     byte[] bt = enc.GetBytes(s);
     return GetHexString(sec.ComputeHash(bt));
 }
Exemple #4
0
 public static ICryptoTransform CreateEncryptor(string key, CryptionType type)
 {
     ICryptoTransform transform;
     SHA512 sha512 = new SHA512CryptoServiceProvider();
     var bytes = sha512.ComputeHash(Sha1(key).ToAsciiBytes());
     switch (type)
     {
         case CryptionType.Aes:
             var aes = Rijndael.Create();
             aes.Mode = CipherMode.CBC;
             transform = aes.CreateEncryptor(bytes.Skip(17).Take(32).ToArray(), bytes.Skip(17).Take(16).ToArray());
             aes.Clear();
             break;
         case CryptionType.Des:
             var des = new DESCryptoServiceProvider { Mode = CipherMode.CBC };
             transform = des.CreateEncryptor(bytes.Skip(17).Take(8).ToArray(), bytes.Skip(17).Take(16).ToArray());
             des.Clear();
             break;
         default:
             var tripleDes = new TripleDESCryptoServiceProvider { Mode = CipherMode.CBC };
             transform = tripleDes.CreateEncryptor(bytes.Skip(17).Take(24).ToArray(), bytes.Skip(17).Take(16).ToArray());
             tripleDes.Clear();
             break;
     }
     return transform;
 }
Exemple #5
0
 private static string Hash(byte[] clearBuffer, HashAlgorithm algorithm)
 {
     System.Security.Cryptography.HashAlgorithm hashAlgorithm;
     switch (algorithm)
     {
         case HashAlgorithm.MD5:
             hashAlgorithm = new MD5CryptoServiceProvider();
             break;
         case HashAlgorithm.SHA1:
         default:
             hashAlgorithm = new SHA1CryptoServiceProvider();
             break;
         case HashAlgorithm.SHA256:
             hashAlgorithm = new SHA256CryptoServiceProvider();
             break;
         case HashAlgorithm.SHA384:
             hashAlgorithm = new SHA384CryptoServiceProvider();
             break;
         case HashAlgorithm.SHA512:
             hashAlgorithm = new SHA512CryptoServiceProvider();
             break;
     }
     var encryptedBuffer = hashAlgorithm.ComputeHash(clearBuffer);
     return Convert.ToBase64String(encryptedBuffer);
 }
Exemple #6
0
 /// <summary>
 /// 64字节,512位
 /// </summary>
 /// <param name="str"></param>
 /// <returns></returns>
 public static string SHA512(string str)
 {
     byte[] buffer = Encoding.UTF8.GetBytes(str);
     SHA512CryptoServiceProvider SHA512 = new SHA512CryptoServiceProvider();
     byte[] byteArr = SHA512.ComputeHash(buffer);
     return BitConverter.ToString(byteArr);
 }
 private static string SHA512Hashing(string text)
 {
     UnicodeEncoding UE = new UnicodeEncoding();
     byte[] plaintBytes = UE.GetBytes(text);
     SHA512CryptoServiceProvider sha512 = new SHA512CryptoServiceProvider();
     byte[] cipherBytes = sha512.ComputeHash(plaintBytes);
     return Convert.ToBase64String(cipherBytes);
 }
        public static string SHA512Encryptor(string plainText)
        {
            byte[] data = ASCIIEncoding.ASCII.GetBytes(plainText);
            SHA512 sha512 = new SHA512CryptoServiceProvider();
            byte[] result = sha512.ComputeHash(data);

            return Convert.ToBase64String(result);
        }
Exemple #9
0
 public static string CodificarPassword(string primerPassword)
 {
     string clave = "72ggbcye7364ç5%hgd/.83045,72!@847@9dmshfjsnc3/jd";
     SHA512 sha512 = new SHA512CryptoServiceProvider();
     byte[] inputBytes = (new UnicodeEncoding()).GetBytes(primerPassword+clave);
     byte[] hash = sha512.ComputeHash(inputBytes);
     return Convert.ToBase64String(hash);
 }
        /// <summary>
        /// Creates a SHA2 hash out of a password defined as string
        /// </summary>
        /// <param name="password">The password</param>
        /// <returns>The SHA2 hash of the password</returns>
        public static string CreateSha2Hash(this string password)
        {
            var sha2 = new SHA512CryptoServiceProvider();

            var clearPasswordAsByteArray = Encoding.ASCII.GetBytes(password);
            var hashedPasswordAsByteArray = sha2.ComputeHash(clearPasswordAsByteArray);

            return Convert.ToBase64String(hashedPasswordAsByteArray);
        }
        public string EncryptPassword(string unencryptedPassword)
        {
            SHA512CryptoServiceProvider x = new SHA512CryptoServiceProvider();

            byte[] data = Encoding.ASCII.GetBytes(unencryptedPassword);
            data = x.ComputeHash(data);

            return Encoding.ASCII.GetString(data);
        }
Exemple #12
0
 public static bool CheckHash(string path, string hash)
 {
     byte[] buffer = File.ReadAllBytes(path);
     string res = "";
     using (var cryptoProvider = new SHA512CryptoServiceProvider())
     {
         res = BitConverter.ToString(cryptoProvider.ComputeHash(buffer));
     }
     return res == hash;
 }
Exemple #13
0
        private string CalculateHash(string phrase)
        {
            var Provider = new SHA512CryptoServiceProvider();

            var passwordStream = Encoding.UTF8.GetBytes(phrase);

            var hashStream = Provider.ComputeHash(passwordStream);

            return Encoding.UTF8.GetString(hashStream);
        }
Exemple #14
0
        /// <summary>
        /// Creates SHA-512 for text
        /// </summary>
        /// <param name="text">Input text from which is created SHA1</param>
        /// <param name="encoding">Encoding for text</param>
        /// <returns>Cryptografic hash SHA-512</returns>
        public static string CreateSHA512Hash(string text, Encoding encoding)
        {
            byte[] buffer = encoding.GetBytes(text);
            var cryptoTransformSHA512 = new SHA512CryptoServiceProvider();

            string hash = BitConverter.ToString(
                cryptoTransformSHA512.ComputeHash(buffer)).Replace("-", "");

            return hash;
        }
Exemple #15
0
 public override byte[] ComputeFileHash(string[] filenames, int? bufferSize)
 {
     ValidateParameters(filenames, ref bufferSize);
     using (SHA512CryptoServiceProvider cryptoProvider = new SHA512CryptoServiceProvider())
     {
         ICryptoTransform cryptoInterface = cryptoProvider;
         ComputeHashes(filenames, cryptoInterface, bufferSize.Value);
         return cryptoProvider.Hash;
     }
 }
Exemple #16
0
 public static string Compute(string source)
 {
     if (string.IsNullOrEmpty(source))
     {
         throw new ArgumentException("You might not want to hash empty string.", "source");
     }
     var provider = new SHA512CryptoServiceProvider();
     var message = Encoding.UTF8.GetBytes(source);
     var hash = provider.ComputeHash(message);
     return Convert.ToBase64String(hash);
 }
 private static string GetHash(byte[] data)
 {
     SHA512 sha = new SHA512CryptoServiceProvider();
     StringBuilder sb = new StringBuilder();
     data = sha.ComputeHash(data);
     foreach (byte by in data)
     {
         sb.Append(by.ToString("x2"));
     }
     return sb.ToString();
 }
Exemple #18
0
        static void passwordsHash1(string password)
        {             
            byte[] byteRepresentation = UnicodeEncoding.Unicode.GetBytes(password);

            var sha512 = new SHA512CryptoServiceProvider();
            var hash = sha512.ComputeHash(byteRepresentation);
            string hashedText = Convert.ToBase64String(hash);

            Console.WriteLine("Password Hash Simple.");
            Console.WriteLine("Texto de Hash (base64): " + hashedText );
        }
Exemple #19
0
 public static string Compute(object obj)
 {
     if (obj == null)
     {
         throw new ArgumentException("You might not want to hash empty object.", "obj");
     }
     var provider = new SHA512CryptoServiceProvider();
     var message = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(obj));
     var hash = provider.ComputeHash(message);
     return Convert.ToBase64String(hash);
 }
Exemple #20
0
 public static string HashPassword(string password)
 {
     using (var sha = new SHA512CryptoServiceProvider())
     {
         if (password == "")
         {
             return "nonexistent-password";
         }
         var bytes = sha.ComputeHash(Encoding.ASCII.GetBytes(password));
         return bytes.Aggregate("", (s, b) => s + b.ToString("X2"));
     }
 }
Exemple #21
0
        /// <summary>
        /// Calculates the SHA512-hash of the given string
        /// </summary>
        /// <returns>Hexadecimal representation of the SHA512-hash.</returns>
        /// <param name="str">Input string.</param>
        public static string SHA512(string str)
        {
            var bytes = Encoding.ASCII.GetBytes (str);
            byte[] hash;

            // We're using the native SHA512 implementation here.
            using (var hasher = new SHA512CryptoServiceProvider ()) {
                hash = hasher.ComputeHash (bytes);
            }

            return hash.ToHex ();
        }
        /// <summary>
        /// Hashes a stream with SHA-512.
        /// </summary>
        /// <param name="stream">The stream to hash.</param>
        /// <returns>The hash.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="stream" /> is <see langword="null" />.
        /// </exception>
        public static byte[] SHA512(this Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            using (var hasher = new SHA512CryptoServiceProvider())
            {
                return hasher.ComputeHash(stream);
            }
        }
        public static string Sha512Encode(string pwd)
        {
            SHA512 sha512 = new SHA512CryptoServiceProvider();//建立一個SHA512

            byte[] source = Encoding.Default.GetBytes(pwd);//將字串轉為Byte[]

            byte[] crypto = sha512.ComputeHash(source);//進行SHA512加密

            string result = Convert.ToBase64String(crypto);//把加密後的字串從Byte[]轉為字串

            return result;
        }
        private static byte[] ComputeHash(string password)
        {
            using (MemoryStream ms = new MemoryStream())
            using (StreamWriter sw = new StreamWriter(ms))
            {
                sw.Write(password);
                sw.Flush();
                ms.Position = 0;

                using (SHA512CryptoServiceProvider provider = new SHA512CryptoServiceProvider())
                    return provider.ComputeHash(ms);
            }
        }
        public static string GetSHA512(byte[] data)
        {
            System.Security.Cryptography.SHA512 sha = new System.Security.Cryptography.SHA512CryptoServiceProvider();
            byte[] bytResult = sha.ComputeHash(data);

            //转换成字符串,32位
            string strResult = BitConverter.ToString(bytResult);

            //BitConverter转换出来的字符串会在每个字符中间产生一个分隔符,需要去除掉
            strResult = strResult.Replace("-", "");

            return(strResult);
        }
        public static string EncodePassword(string originalPassword)
        {  //Clave que se utilizará para encriptar el usuario y la contraseña
            string clave = "7f9facc418f74439c5e9709832;0ab8a5:OCOdN5Wl,q8SLIQz8i|8agmu¬s13Q7ZXyno/";
            //Se instancia el objeto sha512 para posteriormente usarlo para calcular la matriz de bytes especificada
            SHA512 sha512 = new SHA512CryptoServiceProvider();

            //Se crea un arreglo llamada inputbytes donde se convierte el usuario, la contraseña y la clave a una secuencia de bytes.
            byte[] inputBytes = (new UnicodeEncoding()).GetBytes(originalPassword + clave);
            //Se calcula la matriz de bytes del arreglo anterior y se encripta.
            byte[] hash = sha512.ComputeHash(inputBytes);
            //Convertimos el arreglo de bytes a cadena.
            return Convert.ToBase64String(hash);
        }
        public string Sha512Encrypt(string phrase)
        {
            var bytes = Encoding.UTF8.GetBytes(phrase);
            bytes = new SHA512CryptoServiceProvider().ComputeHash(bytes);
            var hashBuilder = new StringBuilder();

            foreach (byte b in bytes)
            {
                hashBuilder.Append(b.ToString("x2").ToLower());
            }

            return hashBuilder.ToString();
        }
Exemple #28
0
        public static Stream Encrypt(Stream stream, out string seed_encoded, out string ident_encoded, string fileName)
        {
            RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
            byte[] seed = new byte[16];
            rngCsp.GetBytes(seed);
            seed_encoded = UrlBase64Encode(seed);

            SHA512CryptoServiceProvider sha512csp = new SHA512CryptoServiceProvider();
            byte[] seed_result = sha512csp.ComputeHash(seed);
            byte[] key = new byte[32];
            Buffer.BlockCopy(seed_result, 0, key, 0, 32);

            byte[] iv = new byte[16];
            Buffer.BlockCopy(seed_result, 32, iv, 0, 16);

            byte[] ident = new byte[16];
            Buffer.BlockCopy(seed_result, 48, ident, 0, 16);
            ident_encoded = UrlBase64Encode(ident);
            var fi = new FileInfo(fileName);

            Dictionary<string, string> args = new Dictionary<string, string>();

            // text files aren't detected well by the "ClouDeveloper" mime type library, use ShareX's builtin list first.
            if (Helpers.IsTextFile(fileName))
            {
                args["mime"] = "text/plain";
            }
            else
            {
                var mimeOpts = ClouDeveloper.Mime.MediaTypeNames.GetMediaTypeNames(fi.Extension).ToList();
                args["mime"] = mimeOpts.Count > 0 ? mimeOpts[0] : "image/png";
            }
            args["name"] = fileName;

            byte[] d = Encoding.BigEndianUnicode.GetBytes(JsonConvert.SerializeObject(args));

            byte[] rawdata = d.Concat(new byte[] { 0, 0 }).Concat(stream.GetBytes()).ToArray();

            int l = FindIVLen(rawdata.Length);
            byte[] civ = new byte[l];
            Array.Copy(iv, civ, l);
            KeyParameter key_param = new KeyParameter(key);
            var ccmparams = new CcmParameters(key_param, MacSize, civ, new byte[0]);
            var ccmMode = new CcmBlockCipher(new AesFastEngine());
            ccmMode.Init(true, ccmparams);
            var encBytes = new byte[ccmMode.GetOutputSize(rawdata.Length)];
            var res = ccmMode.ProcessBytes(rawdata, 0, rawdata.Length, encBytes, 0);
            ccmMode.DoFinal(encBytes, res);

            return new MemoryStream(encBytes);
        }
Exemple #29
0
        /// <summary>
        /// Computes the hash for the specified stream and compares
        /// it to the value in this object. CRC hashes are not supported 
        /// because there is no built-in support in the .net framework and
        /// a CRC implementation exceeds the scope of this project. If you
        /// attempt to Verify() a CRC hash a NotImplemented() exception will
        /// be thrown.
        /// </summary>
        /// <param name="istream">The stream to compute the hash for</param>
        /// <returns>True if the computed hash matches what's stored in this object.</returns>
        public bool Verify(Stream istream) {
            if (IsValid) {
                HashAlgorithm hashAlg = null;

                switch (m_algorithm) {
                    case FtpHashAlgorithm.SHA1:
                        hashAlg = new SHA1CryptoServiceProvider();
                        break;
#if !NET2
                    case FtpHashAlgorithm.SHA256:
                        hashAlg = new SHA256CryptoServiceProvider();
                        break;
                    case FtpHashAlgorithm.SHA512:
                        hashAlg = new SHA512CryptoServiceProvider();
                        break;
#endif
                    case FtpHashAlgorithm.MD5:
                        hashAlg = new MD5CryptoServiceProvider();
                        break;
                    case FtpHashAlgorithm.CRC:
                        hashAlg = new Crc32();
                        break;
                        //throw new NotImplementedException("There is no built in support for computing CRC hashes.");
                    default:
                        throw new NotImplementedException("Unknown hash algorithm: " + m_algorithm.ToString());
                }

                try {
                    byte[] data = null;
                    string hash = "";

                    data = hashAlg.ComputeHash(istream);
                    if (data != null) {
                        foreach (byte b in data) {
                            hash += b.ToString("x2");
                        }

                        return (hash.ToUpper() == m_value.ToUpper());
                    }
                }
                finally {
#if !NET2 // .NET 2.0 doesn't provide access to Dispose() for HashAlgorithm
                    if (hashAlg != null)
                        hashAlg.Dispose();
#endif
                }
            }

            return false;
        }
Exemple #30
0
        private static byte[] GetTargetHash(int iteratioNo, byte[] initialHash)
        {
            var result = initialHash.Clone() as byte[];

            using (var hash = new SHA512CryptoServiceProvider())
            {
                for (int i = 0; i < iteratioNo; ++i)
                {
                    result = hash.ComputeHash(result);
                }
            }

            return result;
        }
Exemple #31
0
        static void Main(string[] args)
        {
            using(SHA512CryptoServiceProvider sha = new SHA512CryptoServiceProvider())
            while (true)
            {
                string s = Console.ReadLine();
                byte[] a = ASCIIEncoding.ASCII.GetBytes(s.ToCharArray());
                byte[] b = Cript512.ComputeHash(new MemoryStream(a));
                byte[] c = sha.ComputeHash(a);

                Console.WriteLine("{0}\nRAW:{1}\n\nHASH:{2}\n\nSHA:{3}", s, BitConverter.ToString(a), BitConverter.ToString(b),BitConverter.ToString(c));

            }
        }
Exemple #32
0
        public AesEncryptionProvider(string key, int keyLength = 24)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("Key must have have a valid value.", nameof(key));
            }
            if (!validLengths.Contains(keyLength))
            {
                throw new ArgumentException("Invalid key length, key must be of sizes 16, 24 or 32", nameof(key));
            }
            using var hash = new Crypto.SHA512CryptoServiceProvider();
            var hashed = hash.ComputeHash(Encoding.UTF8.GetBytes(key));

            _aesKey = new byte[keyLength];
            Array.ConstrainedCopy(hashed, 0, this._aesKey, 0, keyLength);
        }
Exemple #33
0
        /// <summary>
        /// 计算SHA-512码
        /// </summary>
        /// <param name="word">字符串</param>
        /// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
        /// <returns></returns>
        public static string Hash_SHA_512(string word, bool toUpper = true)
        {
            try
            {
                System.Security.Cryptography.SHA512CryptoServiceProvider SHA512CSP
                    = new System.Security.Cryptography.SHA512CryptoServiceProvider();

                byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
                byte[] bytHash  = SHA512CSP.ComputeHash(bytValue);
                SHA512CSP.Clear();

                //根据计算得到的Hash码翻译为SHA-1码
                string sHash = "", sTemp = "";
                for (int counter = 0; counter < bytHash.Count(); counter++)
                {
                    long i = bytHash[counter] / 16;
                    if (i > 9)
                    {
                        sTemp = ((char)(i - 10 + 0x41)).ToString();
                    }
                    else
                    {
                        sTemp = ((char)(i + 0x30)).ToString();
                    }
                    i = bytHash[counter] % 16;
                    if (i > 9)
                    {
                        sTemp += ((char)(i - 10 + 0x41)).ToString();
                    }
                    else
                    {
                        sTemp += ((char)(i + 0x30)).ToString();
                    }
                    sHash += sTemp;
                }

                //根据大小写规则决定返回的字符串
                return(toUpper ? sHash : sHash.ToLower());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Exemple #34
0
        /// <summary>
        /// 获取文本SHA512
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string GetTextSHA_512(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(string.Empty);
            }
            System.Security.Cryptography.SHA512CryptoServiceProvider SHA512CSP
                = new System.Security.Cryptography.SHA512CryptoServiceProvider();

            byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(text);
            byte[] bytHash  = SHA512CSP.ComputeHash(bytValue);
            SHA512CSP.Clear();

            //根据计算得到的Hash码翻译为SHA-1码
            string sHash = ConvertHashBytes(bytHash);

            //根据大小写规则决定返回的字符串
            return(sHash.ToLower());
        }
Exemple #35
0
 /// <summary>
 /// Compute the hexadecimal SHA512 hash of the specified string.
 /// </summary>
 /// <remarks>
 /// Uses a SHA512CryptoServiceProvider and a BitConverter.
 /// </remarks>
 /// <param name="s">The string to hash.</param>
 /// <returns>The hexadecimal SHA512 hash of the specified string.</returns>
 public static string SHA512Hash(this string s)
 {
     System.Security.Cryptography.SHA512CryptoServiceProvider hasha = new System.Security.Cryptography.SHA512CryptoServiceProvider();
     return(BitConverter.ToString(hasha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(s))).Replace("-", ""));
 }