Пример #1
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;
 }
Пример #2
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);
 }
Пример #3
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);
 }
Пример #4
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);
 }
 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);
 }
Пример #6
0
        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);
        }
Пример #7
0
        /// <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);
        }
Пример #8
0
        public string EncryptPassword(string unencryptedPassword)
        {
            SHA512CryptoServiceProvider x = new SHA512CryptoServiceProvider();

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

            return Encoding.ASCII.GetString(data);
        }
Пример #9
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);
        }
Пример #10
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;
 }
Пример #11
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;
        }
Пример #12
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);
 }
Пример #13
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 );
        }
Пример #14
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);
 }
Пример #15
0
 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();
 }
Пример #16
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"));
     }
 }
        /// <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);
            }
        }
Пример #18
0
        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;
        }
Пример #19
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 ();
        }
        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);
            }
        }
Пример #21
0
        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);
        }
Пример #23
0
Файл: Up1.cs Проект: k3d3/ShareX
        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);
        }
Пример #24
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;
        }
Пример #25
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));

            }
        }
Пример #26
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);
        }
Пример #27
0
 /// <summary>
 /// 获取字符串的 40 位 SHA512 大写。
 /// </summary>
 /// <param name="input">需计算 SHA512 的字符串。</param>
 /// <param name="prefix">需添加的字符串前缀。</param>
 /// <returns>40 位 SHA512 大写。</returns>
 /// <exception cref="ArgumentNullException"><c>input</c> 为 null。</exception>
 public static string GetStringSHA512(string input, string prefix = "")
 {
     if (input == null)
     {
         throw new ArgumentNullException("input", "input 不能为空。");
     }
     using (SHA512CryptoServiceProvider sha512Csp = new SHA512CryptoServiceProvider())
     {
         byte[] bytes = sha512Csp.ComputeHash(Encoding.UTF8.GetBytes(prefix + input));
         StringBuilder sb = new StringBuilder(40);
         foreach (var temp in bytes)
         {
             sb.AppendFormat("{0:X2}", temp);
         }
         return sb.ToString();
     }
 }
Пример #28
0
        public static string HashSha512Hex(string password, string salt)
        {
            if (password == null)
            {
                return null;
            }

            if (salt == null)
            {
                return null;
            }

            using (SHA512CryptoServiceProvider provider = new SHA512CryptoServiceProvider())
            {
                return BitConverter.ToString(provider.ComputeHash(Encoding.UTF8.GetBytes(password + salt))).Replace("-", "").ToLower(CultureInfo.InvariantCulture);
            }
        }
Пример #29
0
        public static byte[] HashRaw(EHashType type, byte[] bs)
        {
            if (bs == null) return null;

            HashAlgorithm cmd5 = null;
            switch (type)
            {
                case EHashType.Md5: cmd5 = new MD5CryptoServiceProvider(); break;
                case EHashType.Sha1: cmd5 = new SHA1CryptoServiceProvider(); break;
                case EHashType.Sha256: cmd5 = new SHA256CryptoServiceProvider(); break;
                case EHashType.Sha384: cmd5 = new SHA384CryptoServiceProvider(); break;
                case EHashType.Sha512: cmd5 = new SHA512CryptoServiceProvider(); break;
            }

            bs = cmd5.ComputeHash(bs);
            cmd5.Dispose();
            return bs;
        }
Пример #30
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);
            }
        }
Пример #31
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());
        }
Пример #32
0
 /// <summary>
 /// 获取文件的 40 位 SHA512 大写。
 /// </summary>
 /// <param name="filePath">需计算 SHA512 的文件。</param>
 /// <returns>40 位 SHA512 大写。</returns>
 /// <exception cref="FileNotFoundException"><c>filePath</c> 不存在。</exception>
 public static string GetFileSHA512(string filePath)
 {
     if (File.Exists(filePath) == false)
     {
         throw new FileNotFoundException("文件不存在!", filePath);
     }
     using (SHA512CryptoServiceProvider sha512Csp = new SHA512CryptoServiceProvider())
     {
         using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
         {
             byte[] bytes = sha512Csp.ComputeHash(fs);
             StringBuilder sb = new StringBuilder(40);
             foreach (var temp in bytes)
             {
                 sb.AppendFormat("{0:X2}", temp);
             }
             return sb.ToString();
         }
     }
 }
Пример #33
0
        public static byte[] HashRaw(EHashType type, Stream bs, bool seekBegin)
        {
            if (bs == null) return null;

            HashAlgorithm cmd5 = null;
            switch (type)
            {
                case EHashType.Md5: cmd5 = new MD5CryptoServiceProvider(); break;
                case EHashType.Sha1: cmd5 = new SHA1CryptoServiceProvider(); break;
                case EHashType.Sha256: cmd5 = new SHA256CryptoServiceProvider(); break;
                case EHashType.Sha384: cmd5 = new SHA384CryptoServiceProvider(); break;
                case EHashType.Sha512: cmd5 = new SHA512CryptoServiceProvider(); break;
            }

            if (seekBegin) bs.Seek(0, SeekOrigin.Begin);

            byte[] bsh = cmd5.ComputeHash(bs);
            cmd5.Dispose();
            return bsh;
        }
Пример #34
0
        public bool AuthenticateUser(string username, string password, string nick, string ident)
        {
            SHA512CryptoServiceProvider sha512hasher = new SHA512CryptoServiceProvider();
            string hash = sha512hasher.ComputeHash(Encoding.Default.GetBytes(password)).ByteArrayToString();

            var users = from x in db.User where x.Username == username && x.Password == hash select x;

            if (users.Any())
            {
                var user = users.First();
                authedUsers[ident] = user;

                user.LastNick = nick;
                user.LastIdent = ident;

                db.SubmitChanges();

                return true;
            }
            else
                return false;
        }
Пример #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("-", ""));
 }