Ejemplo n.º 1
0
        public static void VerifySHA512Hash(byte[] bytesA, byte[] bytesB)
        {
            SHA512 shaM = new SHA512Managed();
            byte[] hashA = shaM.ComputeHash(bytesA);
            byte[] hashB = shaM.ComputeHash(bytesB);

            // Create a StringComparer an compare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder hashAStringBuilder = new StringBuilder();
            StringBuilder hashBStringBuilder = new StringBuilder();

            // Loop through each byte of the hashed data
            // and format each one as a hexadecimal string.
            for (int i = 0; i < hashA.Length; i++)
            {
                hashAStringBuilder.Append(hashA[i].ToString("x2"));
            }

            string inputA = hashAStringBuilder.ToString();

            for (int i = 0; i < hashB.Length; i++)
            {
                hashBStringBuilder.Append(hashB[i].ToString("x2"));
            }

            string inputB = hashBStringBuilder.ToString();

            if (comparer.Compare(inputA, inputB) != 0)
            {
                Assert.Fail("Hash not match! Expected: '{0}', but was '{1}'", inputA, inputB);
            }
        }
 public string HashValue(string Value)
 {
     var hash = new SHA512Managed();
     byte[] result = hash.ComputeHash(Encoding.Unicode.GetBytes(Value));
     for (int i = 1; i <= _iterations; i++ )
     {
         result = hash.ComputeHash(result);
     }
     return Convert.ToBase64String(result);
 }
Ejemplo n.º 3
0
        public byte[] CreateHashSha512(string userEnteredPw)
        {
            //userEnteredPw = userEnteredPw.Trim();
            SHA512Managed sha512 = new SHA512Managed();

            return sha512.ComputeHash(Encoding.UTF8.GetBytes(userEnteredPw));
        }
Ejemplo n.º 4
0
        private byte[] CreateIV(string strPassword)
        {
            // Convert strPassword to an array and store in chrData.
            char[] chrData = strPassword.ToCharArray();
            // Use intLength to get strPassword size.
            int intLength = chrData.GetUpperBound(0);

            // Declare bytDataToHash and make it the same size as chrData.
            byte[] bytDataToHash = new byte[intLength + 1];

            // Use For Next to convert and store chrData into bytDataToHash.
            for (int i = 0; i <= chrData.GetUpperBound(0); i++)
            {
                bytDataToHash[i] = System.Convert.ToByte(Asc(chrData[i]));
            }

            // Declare what hash to use.
            System.Security.Cryptography.SHA512Managed SHA512 = new System.Security.Cryptography.SHA512Managed();
            // Declare bytResult, Hash bytDataToHash and store it in bytResult.
            byte[] bytResult = SHA512.ComputeHash(bytDataToHash);
            // Declare bytIV(15).  It will hold 128 bits.
            byte[] bytIV = new byte[16];

            // Use For Next to put a specific size (128 bits) of bytResult into bytIV.
            // The 0 To 30 for bytKey used the first 256 bits of the hashed password.
            // The 32 To 47 will put the next 128 bits into bytIV.
            for (int i = 32; i <= 47; i++)
            {
                bytIV[i - 32] = bytResult[i];
            }

            return(bytIV); // Return the IV.
        }
Ejemplo n.º 5
0
 private string GetPasswordHash(string stringPassword)
 {
     using (var sha512 = new SHA512Managed())
     {
         return BitConverter.ToString(sha512.ComputeHash(Encoding.Default.GetBytes(stringPassword)));
     }
 }
Ejemplo n.º 6
0
 public static string EncryptOneWay(string data)
 {
     byte[] buf = Encoding.ASCII.GetBytes(data);
     SHA512 sha = new SHA512Managed();
     byte[] enc = sha.ComputeHash(buf);
     return HexEncoding.ToString(enc);
 }
Ejemplo n.º 7
0
 public static string CalculateSha512Hash(string input)
 {
     var inputBytes = Encoding.UTF8.GetBytes(input);
     var hasher = new SHA512Managed();
     var hashBytes = hasher.ComputeHash(inputBytes);
     return BitConverter.ToString(hashBytes).Replace("-", string.Empty).ToLowerInvariant();
 }
Ejemplo n.º 8
0
        //Generating Hash Value, Combining userpassword and above random salt value
        //public String GenerateSHA256Hash(String input, String salt)
        //{
        //    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
        //    System.Security.Cryptography.SHA256Managed sha256hashstring = new System.Security.Cryptography.SHA256Managed();
        //    byte[] hash = sha256hashstring.ComputeHash(bytes);
        //    return ByteArrayToHexString(hash);
        //}

        public String GenerateSHA256Hash(String input, String salt)
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
            System.Security.Cryptography.SHA512Managed sha512hashstring = new System.Security.Cryptography.SHA512Managed();
            byte[] hash = sha512hashstring.ComputeHash(bytes);
            return(ByteArrayToHexString(hash));
        }
Ejemplo n.º 9
0
 public static byte[] Hash(string s)
 {
     using (SHA512 shaM = new SHA512Managed())
     {
         return shaM.ComputeHash(Encoding.UTF8.GetBytes(s));
     }
 }
Ejemplo n.º 10
0
 public void Encryption2()
 {
     byte[] data = new byte[2];
     byte[] result;
     SHA512 shaM = new SHA512Managed();
     result = shaM.ComputeHash(data);
 }
Ejemplo n.º 11
0
 public static byte[] BytesToHash(byte[] byteArray)
 {
     byte[] result;
     SHA512 shaM = new SHA512Managed();
     result = shaM.ComputeHash(byteArray);
     return result;
 }
Ejemplo n.º 12
0
        //*************************
        //** Create An IV
        //*************************

        public static byte[] CreateRijndaelIVWithSHA512(string strPassword)
        {
            //Convert strPassword to an array and store in chrData.
            char[] chrData = strPassword.ToCharArray();
            //Use intLength to get strPassword size.
            int intLength = chrData.GetUpperBound(0);
            //Declare bytDataToHash and make it the same size as chrData.
            byte[] bytDataToHash = new byte[intLength + 1];

            //Use For Next to convert and store chrData into bytDataToHash.
            for (int i = 0; i <= chrData.GetUpperBound(0); i++) {
                bytDataToHash[i] = Convert.ToByte(Strings.Asc(chrData[i]));
            }

            //Declare what hash to use.
            System.Security.Cryptography.SHA512Managed SHA512 = new System.Security.Cryptography.SHA512Managed();
            //Declare bytResult, Hash bytDataToHash and store it in bytResult.
            byte[] bytResult = SHA512.ComputeHash(bytDataToHash);
            //Declare bytIV(15).  It will hold 128 bits.
            byte[] bytIV = new byte[16];

            //Use For Next to put a specific size (128 bits) of bytResult into bytIV.
            //The 0 To 30 for bytKey used the first 256 bits of the hashed password.
            //The 32 To 47 will put the next 128 bits into bytIV.
            for (int i = 32; i <= 47; i++) {
                bytIV[i - 32] = bytResult[i];
            }

            return bytIV;
            //Return the IV.
        }
Ejemplo n.º 13
0
        private byte[] CreateKey(string strPassword)
        {
            // Convert strPassword to an array and store in chrData.
            char[] chrData = strPassword.ToCharArray();
            // Use intLength to get strPassword size.
            int intLength = chrData.GetUpperBound(0);

            // Declare bytDataToHash and make it the same size as chrData.
            byte[] bytDataToHash = new byte[intLength + 1];

            // Use For Next to convert and store chrData into bytDataToHash.
            for (int i = 0; i <= chrData.GetUpperBound(0); i++)
            {
                bytDataToHash[i] = System.Convert.ToByte(Asc(chrData[i]));
            }

            // Declare what hash to use.
            System.Security.Cryptography.SHA512Managed SHA512 = new System.Security.Cryptography.SHA512Managed();
            // Declare bytResult, Hash bytDataToHash and store it in bytResult.
            byte[] bytResult = SHA512.ComputeHash(bytDataToHash);
            // Declare bytKey(31).  It will hold 256 bits.
            byte[] bytKey = new byte[32];

            // Use For Next to put a specific size (256 bits) of
            // bytResult into bytKey. The 0 To 31 will put the first 256 bits
            // of 512 bits into bytKey.
            for (int i = 0; i <= 31; i++)
            {
                bytKey[i] = bytResult[i];
            }

            return(bytKey); // Return the key.
        }
Ejemplo n.º 14
0
 public static string CreateSHAHash(string PasswordSHA512)
 {
     System.Security.Cryptography.SHA512Managed sha512 = new System.Security.Cryptography.SHA512Managed();
     Byte[] EncryptedSHA512 = sha512.ComputeHash(System.Text.Encoding.UTF8.GetBytes(PasswordSHA512));
     sha512.Clear();
     return(Convert.ToBase64String(EncryptedSHA512));
 }
Ejemplo n.º 15
0
 public static string sha512encrypt(string phrase)
 {
     UTF8Encoding encoder = new UTF8Encoding();
     SHA512Managed sha512hasher = new SHA512Managed();
     byte[] hashedDataBytes = sha512hasher.ComputeHash(encoder.GetBytes(phrase));
     return byteArrayToString(hashedDataBytes);
 }
        // Add User buttonin logiikka
        private void btn_addUser_Click(object sender, RoutedEventArgs e)
        {
            if (tb_password1.Password == tb_password2.Password && tb_password1.Password != "" && tb_password2.Password != "" && tb_username.Text != "") // Tarkistetaan että kentät on täytetty oikein.
            {

                SHA512 sha512 = new SHA512Managed();                    // SHA512 cryptaus pitäisi riittää.
                UTF8Encoding utf8 = new UTF8Encoding();                 // SHA512 ottaa byte arrayn joten string täytyy muuttaa ennen kryptausta.
                String password = BitConverter.ToString(sha512.ComputeHash(utf8.GetBytes(tb_password1.Password)));  // Aika monimutkainen rivi joka muuttaa string byte arrayksi ja kryptaa sen

                string dbConnectionString = @"Data Source=database.db;Version=3;";               // Määritellään sqlite tietokannan nimeksi database.db ja versioksi 3.
                SQLiteConnection sqliteCon = new SQLiteConnection(dbConnectionString);           // Tehdään yhteys olio sqliteCon jota voidaan käyttää myöhemmin.
                try
                {
                    sqliteCon.Open();                                                            // Avataan sqlite yhteys
                    string Query = "INSERT INTO users (username, password) VALUES('" + this.tb_username.Text + "','" + password + "') ";    // Syötetään uuden käyttäjän tiedot tietokantaan. Huom. kryptattu salasana.
                    SQLiteCommand createCommand = new SQLiteCommand(Query, sqliteCon);           // Tehdään SQlite komento query ja sqlitecon argumenteilla.
                    createCommand.ExecuteNonQuery();                                             // Suoritetaan query.
                    sqliteCon.Close();                                                           // Suljetaan db yhteys.

                    tb_username.Text = "";                                                       // Tyhjennetään kentät ettei vahingossa upita samoja tietoja uudestaan
                    tb_password1.Password = "";
                    tb_password2.Password = "";
                    MessageBox.Show("User added successfully!");                                 // Ilmoitus käyttäjälle onnistumisesta.
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);                                                 // Tai epäonnistumisesta.
                }
            }
            else
            {
                MessageBox.Show("Oops, check the fields.");                                      // Kaikki kentät pitää täyttää, ja vieläpä oikein.
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// 哈希加密一个字符串
        /// </summary>
        /// <param name="Security"></param>
        /// <returns></returns>
        public static string HashEncoding(string Security)
        {
            byte[] Value;
            SHA512Managed Arithmetic = null;
            try
            {
                UnicodeEncoding Code = new UnicodeEncoding();
                byte[] Message = Code.GetBytes(Security);

                Arithmetic = new SHA512Managed();
                Value = Arithmetic.ComputeHash(Message);
                Security = "";
                foreach (byte o in Value)
                {
                    Security += (int)o + "O";
                }

                return Security;
            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                if (Arithmetic!=null)
                {
                    Arithmetic.Clear();
                }
            }
        }
Ejemplo n.º 18
0
        public void CreateUser(UserInfo userInfo)
        {
            if (userInfo == null)
            {
                throw new ArgumentNullException(nameof(userInfo));
            }
            if (string.IsNullOrWhiteSpace(userInfo.Username))
            {
                throw new ArgumentNullException(nameof(userInfo.Username));
            }

            if (string.IsNullOrWhiteSpace(userInfo.Password))
            {
                throw new ArgumentNullException(nameof(userInfo.Password));
            }

            if (UsernameExists(userInfo.Username))
            {
                throw new Exception("Username already exists, please enter a different username");
            }

            using (var shazam = new SHA512Managed())
            {
                var salt = GenerateSaltValue();
                var passwordAndSalt = userInfo.Password + salt;
                var hashedPassword = Encoding.UTF8.GetString(shazam.ComputeHash(Encoding.UTF8.GetBytes(passwordAndSalt)));
                userInfo.Password = hashedPassword;
                userInfo.Salt = salt;
                userDal.CreateUser(userInfo);
            }
        }
Ejemplo n.º 19
0
 // encrypt user IP (one-way) before storing it in database
 public static string CreateHash(string ip)
 {
     using (SHA512 shaM = new SHA512Managed())
     {
         return Convert.ToBase64String(shaM.ComputeHash(Encoding.UTF8.GetBytes(ip)));
     }
 }
Ejemplo n.º 20
0
        /// <summary>
        /// Retorna el hash en format string de l'string paràmetre
        /// </summary>
        /// <param name="textIn"> és el text del qual volem calcular el hash</param>
        /// <returns> retorna un string amb el hash resultat o null si hi ha error</returns>
        static string CalculaHash(string textIn)
        {
            try
            {
                // Convertim l'string a un array de bytes
                byte[] bytesIn = UTF8Encoding.UTF8.GetBytes(textIn);
                // Instanciar classe per fer hash
                SHA512Managed SHA512 = new SHA512Managed();
                // Calcular hash
                byte[] hashResult = SHA512.ComputeHash(bytesIn);

                // Si volem mostrar el hash per pantalla o guardar-lo en un arxiu de text
                // cal convertir-lo a un string

                String textOut = BitConverter.ToString(hashResult, 0);

                // Eliminem la classe instanciada
                SHA512.Dispose();
                return textOut;
            }
            catch (Exception)
            {
                Console.WriteLine("Error calculant el hash");
                Console.ReadKey(true);
                return null;
            }
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Creates a new sum proof.
        /// </summary>
        /// <param name="r">The randomness used to encrypt the vote.</param>
        /// <param name="voteSum">The sum of all votes for which to generate a proof.</param>
        /// <param name="publicKey">The public key with which the vote was encrypted.</param>
        /// <param name="parameters">Cryptographic Parameters.</param>
        public Proof(BigInt r, Vote voteSum, BigInt publicKey, BaseParameters parameters)
        {
            if (r == null)
            throw new ArgumentNullException("r");
              if (voteSum == null)
            throw new ArgumentNullException("vote");
              if (publicKey == null)
            throw new ArgumentNullException("publicKey");
              if (parameters == null)
            throw new ArgumentNullException("parameters");

              BigInt r0 = parameters.Random();

              T0 = publicKey.PowerMod(r0, parameters.P);

              MemoryStream serializeStream = new MemoryStream();
              SerializeContext serializer = new SerializeContext(serializeStream);
              serializer.Write(voteSum.Ciphertext);
              serializer.Write(voteSum.HalfKey);
              serializer.Write(publicKey);
              serializer.Write(T0);
              serializer.Close();
              serializeStream.Close();

              SHA512Managed sha512 = new SHA512Managed();
              byte[] hash = sha512.ComputeHash(serializeStream.ToArray());
              C0 = hash[0] | (hash[1] << 8);

              S0 = r0 + r * C0;
        }
 // Method that combines two different salts with the
 // password in between + hashing and returning the hash
 public string GetHashedPassword(string a_salt1, string a_password, string a_salt2)
 {
     SHA512Managed hsh = new SHA512Managed();
     byte[] combinedPswdHsh = hsh.ComputeHash(Encoding.UTF8.GetBytes
         (a_salt1 + a_password.Trim() + a_salt2));
     return Convert.ToBase64String(combinedPswdHsh).Trim();
 }
Ejemplo n.º 23
0
        private static byte[] CreateKey(string strPassword)
        {
            char[] array = strPassword.ToCharArray ();
            int upperBound = array.GetUpperBound (0);
            byte[] array2 = new byte[upperBound + 1];
            int upperBound2 = array.GetUpperBound (0);

            for (int i = 0; i <= upperBound2; i++)
            {
                array2 [i] = (byte)array [i];
            }

            SHA512Managed sHA512Managed = new SHA512Managed ();
            byte[] array3 = sHA512Managed.ComputeHash (array2);
            byte[] array4 = new byte[32];
            int num = 0;
            do
            {
                array4 [num] = array3 [num];
                num++;
            }
            while (num <= 31);

            return array4;
        }
Ejemplo n.º 24
0
 //Get the file's hash in SHA512
 public string GetFileHash(string file)
 {
     SHA512Managed sha = new SHA512Managed();
     FileStream stream = File.OpenRead(file);
     byte[] hash = sha.ComputeHash(stream);
     return BitConverter.ToString(hash).Replace("-", String.Empty);
 }
Ejemplo n.º 25
0
        string password_crypt(string password, string setting)
        {
            string _setting = setting.Substring(0, 12);
            if (_setting[0] != '$' || _setting[2] != '$')
                return null;

            int count_log2 = password_get_count_log2(setting);
            string salt = _setting.Substring(4, 8);
            if (salt.Length < 8)
                return null;
            int count = 1 << count_log2;
            SHA512 shaM = new SHA512Managed();
            Encoding unicode = new UnicodeEncoding(true, false);
            byte[] data = unicode.GetBytes(salt + password);
            byte[] pass = unicode.GetBytes(password);
            byte[] hash = shaM.ComputeHash(data);
            for (int c = 0; c < count; c++)
            {
                data = new byte[hash.Length + pass.Length];
                hash.CopyTo(data, 0);
                pass.CopyTo(data, hash.Length);
                hash = shaM.ComputeHash(data);
            }
            string output = setting + custom64(hash);
            return output.Substring(0, hash_length);
        }
Ejemplo n.º 26
0
        public HttpResponseMessage Register(User user)
        {
            if (!_validator.Validate(user.Password))
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            using (SHA512 sha = new SHA512Managed())
            {
                user.Password = BitConverter.ToString(sha.ComputeHash(Encoding.UTF8.GetBytes(user.Password))).Replace("-", string.Empty);
            }

            _db.Users.Add(user);
            if(_db.GetValidationErrors().Any())
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            try
            {
                _db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                return Request.CreateResponse(HttpStatusCode.Conflict);
            }

            return Request.CreateResponse(HttpStatusCode.Created);
        }
Ejemplo n.º 27
0
 private byte[] MakeHash(byte[] data)
 {
     using (var sha512Hash = new SHA512Managed())
     {
         return sha512Hash.ComputeHash(data);
     }
 }
Ejemplo n.º 28
0
        public static byte[] GenerateHash(HashingStrategy strategy, string password, byte[] salt, int iterations = 1000)
        {
            var bytes = Encoding.UTF8.GetBytes(password).Concat(salt);

            switch (strategy)
            {
                case HashingStrategy.SHA256:
                    using (var sha256 = new SHA256Managed())
                    {
                        return sha256.ComputeHash(bytes.ToArray());
                    }

                case HashingStrategy.SHA512:
                    using (var sha512 = new SHA512Managed())
                    {
                        return sha512.ComputeHash(bytes.ToArray());
                    }
                case HashingStrategy.PBKDF2:
                    using (var pbkdf2 = new Rfc2898DeriveBytes(password, salt) { IterationCount = iterations })
                    {
                        return pbkdf2.GetBytes(salt.Length);
                    }
            }

            throw new NotImplementedException();
        }
Ejemplo n.º 29
0
        //*************************
        //** Create A Key
        //*************************

        public static byte[] CreateRijndaelKeyWithSHA512(string strPassword)
        {
            //Convert strPassword to an array and store in chrData.
            char[] chrData = strPassword.ToCharArray();
            //Use intLength to get strPassword size.
            int intLength = chrData.GetUpperBound(0);
            //Declare bytDataToHash and make it the same size as chrData.
            byte[] bytDataToHash = new byte[intLength + 1];

            //Use For Next to convert and store chrData into bytDataToHash.
            for (int i = 0; i <= chrData.GetUpperBound(0); i++) {
                bytDataToHash[i] = Convert.ToByte(Strings.Asc(chrData[i]));
            }

            //Declare what hash to use.
            System.Security.Cryptography.SHA512Managed SHA512 = new System.Security.Cryptography.SHA512Managed();
            //Declare bytResult, Hash bytDataToHash and store it in bytResult.
            byte[] bytResult = SHA512.ComputeHash(bytDataToHash);
            //Declare bytKey(31).  It will hold 256 bits.
            byte[] bytKey = new byte[32];

            //Use For / Next to put a specific size (256 bits) of
            //bytResult into bytKey. The 0 To 31 will put the first 256 bits
            //of 512 bits into bytKey.
            for (int i = 0; i <= 31; i++) {
                bytKey[i] = bytResult[i];
            }

            return bytKey;
            //Return the key.
        }
Ejemplo n.º 30
0
 /// <summary>
 /// Creates a SHA512 hash of a string
 /// </summary>
 /// <param name="text"></param>
 /// <param name="salt"></param>
 /// <param name="extraHashKey"></param>
 /// <returns></returns>
 public static string HashSha512(string text, string salt, string extraHashKey = "")
 {
     using (SHA512 shaM = new SHA512Managed())
     {
         return Convert.ToBase64String(shaM.ComputeHash(System.Text.Encoding.UTF8.GetBytes(text + extraHashKey + salt)));
     }
 }
Ejemplo n.º 31
0
 /// <summary>
 /// SHA512加密,不可逆转
 /// </summary>
 private byte[] SHA512Encrypt(byte[] code)
 {
     System.Security.Cryptography.SHA512 s512 = new System.Security.Cryptography.SHA512Managed();
     byte[] result = s512.ComputeHash(code);
     s512.Clear();
     return(result);
 }
Ejemplo n.º 32
0
 public string getSha512(string text)
 {
     System.Security.Cryptography.SHA512Managed HashTool = new System.Security.Cryptography.SHA512Managed();
     Byte[] PasswordAsByte = System.Text.Encoding.UTF8.GetBytes(text);
     Byte[] EncryptedBytes = HashTool.ComputeHash(PasswordAsByte);
     HashTool.Clear();
     return(Convert.ToBase64String(EncryptedBytes));
 }
Ejemplo n.º 33
0
 internal static string CreateSHAHash(string password, string salt)
 {
     System.Security.Cryptography.SHA512Managed HashTool = new System.Security.Cryptography.SHA512Managed();
     Byte[] PasswordAsByte = System.Text.Encoding.UTF8.GetBytes(string.Concat(password, salt));
     Byte[] EncryptedBytes = HashTool.ComputeHash(PasswordAsByte);
     HashTool.Clear();
     return(Convert.ToBase64String(EncryptedBytes));
 }
Ejemplo n.º 34
0
 public static string EncryptToSha512(string text)
 {
     var uEncode = new UnicodeEncoding();
     var bytPassword = uEncode.GetBytes(text);
     var sha = new SHA512Managed();
     var hash = sha.ComputeHash(bytPassword);
     return Convert.ToBase64String(hash);
 }
 /// <summary>
 /// Genera el hash de la familia SHA2, SHA512 de la password en todo proceso de registro o logeo. El parámetro salt se suele dejar vacio, pues no se usa sal en esta aplicación.
 /// </summary>
 /// <param name="password"></param>
 /// <param name="salt"></param>
 /// <returns></returns>
 public static string GetSHA512(string password, string salt)
 {
     System.Security.Cryptography.SHA512Managed HashTool = new System.Security.Cryptography.SHA512Managed();
     Byte[] PasswordAsByte = System.Text.Encoding.UTF8.GetBytes(string.Concat(password, salt));
     Byte[] EncryptedBytes = HashTool.ComputeHash(PasswordAsByte);
     HashTool.Clear();
     return(BitConverter.ToString(EncryptedBytes));
 }
Ejemplo n.º 36
0
         /// <summary>
         /// SHA512加密,不可逆转
         /// </summary>
         /// <param name="str">string str:被加密的字符串</param>
         /// <returns>返回加密后的字符串</returns>
         private static string SHA512Encrypt(string str)
 {
     System.Security.Cryptography.SHA512 s512 = new System.Security.Cryptography.SHA512Managed();
     byte[] byte1;
     byte1 = s512.ComputeHash(Encoding.Default.GetBytes(str));
     s512.Clear();
     return(Convert.ToBase64String(byte1));
 }
Ejemplo n.º 37
0
 /// <summary>
 /// Преобразует входную строку в строку hex.
 /// </summary>
 /// <param name="inputString">Входная строка.</param>
 /// <returns>SHA512 хеш в нижнем регистре (hex).</returns>
 public static String GetSHA512Hash(String inputString)
 {
     using (SHA512 sha = new SHA512Managed())
     {
         Byte[] input = Encoding.UTF8.GetBytes(inputString);
         return ConvertersHelper.ByteArrayToHex(sha.ComputeHash(input));
     }
 }
 /// <summary>
 /// This will return the 512 bit SHA-2 hash.
 /// </summary>
 /// <param name="myString"></param>
 /// <returns></returns>
 public static byte[] GetSHA2_512(this string myString)
 {
     var data = myString.ToBytes();
     SHA512 shaM = new SHA512Managed();
     var result = shaM.ComputeHash(data);
     shaM.Dispose();
     return result;
 }
 private string GetPackageSHA512(string path)
 {
     using (var stream = File.OpenRead(Path.Combine(configuration.DataPath, path)))
     {
         using (var hasher = new SHA512Managed())
             return Convert.ToBase64String(hasher.ComputeHash(stream));
     }
 }
Ejemplo n.º 40
0
        public String GenerateSHA512Hash(String input, String salt)
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
            System.Security.Cryptography.SHA512Managed sha512 =
                new System.Security.Cryptography.SHA512Managed();
            byte[] hash = sha512.ComputeHash(bytes);

            return(BitConverter.ToString(hash).Replace("-", ""));
        }
Ejemplo n.º 41
0
        public static string Encrypt(string password, string salt)
        {
            var saltedPassword = salt + password;
            var saltedBytes    = Encoding.Default.GetBytes(saltedPassword);
            var algorithm      = new CR.SHA512Managed();
            var hashBytes      = algorithm.ComputeHash(saltedBytes);

            return(Convert.ToBase64String(hashBytes));
        }
Ejemplo n.º 42
0
        public static string GTBSHA512(string hash_string)
        {
            System.Security.Cryptography.SHA512Managed sha512 = new System.Security.Cryptography.SHA512Managed();
            Byte[] EncryptedSHA512 = sha512.ComputeHash(System.Text.Encoding.UTF8.GetBytes(hash_string));
            sha512.Clear();
            string hashed = BitConverter.ToString(EncryptedSHA512).Replace("-", "").ToLower();

            return(hashed);
        }
Ejemplo n.º 43
0
 public static string HashPassword(this string password, string salt)
 {
     byte[] bytes = new UTF8Encoding().GetBytes(salt + password);
     byte[] hashBytes;
     using (Security.Cryptography.SHA512Managed algorithm = new Security.Cryptography.SHA512Managed())
     {
         hashBytes = algorithm.ComputeHash(bytes);
     }
     return(Convert.ToBase64String(hashBytes));
 }
        public string CreateSHAHash(string User, string Password, string Salt) //Hash and Salt -- Hash SHA512.
        {
            String Text = User + Password;

            System.Security.Cryptography.SHA512Managed HashTool = new System.Security.Cryptography.SHA512Managed();
            Byte[] HashAsByte     = System.Text.Encoding.UTF8.GetBytes(string.Concat(Text, Salt));
            Byte[] EncryptedBytes = HashTool.ComputeHash(HashAsByte);
            HashTool.Clear();
            return(Convert.ToBase64String(EncryptedBytes));
        }
Ejemplo n.º 45
0
        public static string EncriptarSHA512(string cad)
        {
            byte[] data = System.Text.Encoding.ASCII.GetBytes(cad);
            byte[] result;

            SHA512Managed shaM = new System.Security.Cryptography.SHA512Managed();

            result = shaM.ComputeHash(data);
            return(BitConverter.ToString(result).Replace("-", string.Empty));
        }
Ejemplo n.º 46
0
        // Función que encripta la contraseña mediante SHA512(Se puede cambiar SHA256) utilizando un salt generado previamente.
        private string encriptarSHA512(String argPassword, string salt)
        {
            // Encriptando
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(argPassword + salt);
            System.Security.Cryptography.SHA512Managed sha256hashstring =
                new System.Security.Cryptography.SHA512Managed();
            byte[] hash         = sha256hashstring.ComputeHash(bytes);
            var    passwordCryp = Convert.ToBase64String(hash);

            return(passwordCryp);
        }
Ejemplo n.º 47
0
        public string Hash(string password) //SHA512 hash
        {
            var bytes = new UTF8Encoding().GetBytes(password);

            byte[] hashBytes;
            using (var algorithm = new System.Security.Cryptography.SHA512Managed())
            {
                hashBytes = algorithm.ComputeHash(bytes);
            }
            return(Convert.ToBase64String(hashBytes));
        }
Ejemplo n.º 48
0
 public JsonResult GenerateSHA512StringFors2s(string inputString)
 {
     using (SHA512 sha512Hash = SHA512.Create())
     {
         //From String to byte array
         byte[] sourceBytes = Encoding.UTF8.GetBytes(inputString);
         byte[] hashBytes   = sha512Hash.ComputeHash(sourceBytes);
         string hash        = BitConverter.ToString(hashBytes).Replace("-", String.Empty);
         System.Security.Cryptography.SHA512Managed sha512 = new System.Security.Cryptography.SHA512Managed();
         Byte[] EncryptedSHA512 = sha512.ComputeHash(System.Text.Encoding.UTF8.GetBytes(hash));
         sha512.Clear();
         var bts = Convert.ToBase64String(EncryptedSHA512);
         return(Json(hash, JsonRequestBehavior.AllowGet));
     }
 }
Ejemplo n.º 49
0
 public static string HashEncoding(string Security)
 {
     System.Text.UnicodeEncoding unicodeEncoding = new System.Text.UnicodeEncoding();
     byte[] bytes = unicodeEncoding.GetBytes(Security);
     System.Security.Cryptography.SHA512Managed sHA512Managed = new System.Security.Cryptography.SHA512Managed();
     byte[] array = sHA512Managed.ComputeHash(bytes);
     Security = "";
     byte[] array2 = array;
     for (int i = 0; i < array2.Length; i++)
     {
         byte b = array2[i];
         Security = Security + (int)b + "O";
     }
     return(Security);
 }
Ejemplo n.º 50
0
        public static string Encrypt(string text)
        {
            SHA512 hash = new System.Security.Cryptography.SHA512Managed();

            byte[] textBytes = System.Text.Encoding.Default.GetBytes(text);
            byte[] hashBytes = hash.ComputeHash(textBytes);
            string result    = string.Empty;

            for (int i = 0; i < hashBytes.Length; i++)
            {
                result += hashBytes[i].ToString("X");
            }

            return(result);
        }
Ejemplo n.º 51
0
        public static string SHA512Encode(string input)
        {
            using (System.Security.Cryptography.SHA512Managed sha1 = new System.Security.Cryptography.SHA512Managed())
            {
                var           hash   = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
                StringBuilder result = new StringBuilder(hash.Length * 2);

                foreach (byte b in hash)
                {
                    result.Append(b.ToString("X2"));
                }

                return(result.ToString());
            }
        }
Ejemplo n.º 52
0
        public string obtenerHash(string pCadena)
        {
            string textoPlano = pCadena;
            string hashkey    = "*hg849gh84th==3tg7-534d=_";

            System.Security.Cryptography.SHA512Managed HashTool = new System.Security.Cryptography.SHA512Managed();
            Byte[] HashAsByte     = System.Text.Encoding.UTF8.GetBytes(string.Concat(textoPlano, hashkey));
            Byte[] EncryptedBytes = HashTool.ComputeHash(HashAsByte);
            HashTool.Clear();

            string resultado = Convert.ToBase64String(EncryptedBytes);

            resultado = resultado.Substring(0, 50);

            return(resultado);
        }
Ejemplo n.º 53
0
 public static string HashPassword(this string str)
 {
     try
     {
         var    bytes = new UTF8Encoding().GetBytes(str);
         byte[] hashBytes;
         using (var algorithm = new System.Security.Cryptography.SHA512Managed())
         {
             hashBytes = algorithm.ComputeHash(bytes);
             return(Convert.ToBase64String(hashBytes));
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
Ejemplo n.º 54
0
		public static string MD5Hash(string _password)
		{
			SHA512 sha512 = new System.Security.Cryptography.SHA512Managed();

			byte[] sha512Bytes = System.Text.Encoding.Default.GetBytes(_password);

			byte[] cryString = sha512.ComputeHash(sha512Bytes);

			string hashedPwd = string.Empty;

			for (int i = 0; i < cryString.Length; i++)
			{
				hashedPwd += cryString[i].ToString("X2");
			}

			return hashedPwd;
		}
Ejemplo n.º 55
0
        public static string GetSHA512(string text)
        {
            System.Text.UnicodeEncoding UE = new System.Text.UnicodeEncoding();
            byte[] hashValue;
            byte[] message = UE.GetBytes(text);

            System.Security.Cryptography.SHA512Managed hashString = new System.Security.Cryptography.SHA512Managed();
            string hex = "";

            hashValue = hashString.ComputeHash(message);

            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return(hex);
        }
Ejemplo n.º 56
0
        public static string Hash(string password)
        {
            var bytes = new UTF8Encoding().GetBytes(password);

            byte[] hashBytes;
            try
            {
                using (var algorithm = new System.Security.Cryptography.SHA512Managed())
                {
                    hashBytes = algorithm.ComputeHash(bytes);
                }
                return(Convert.ToBase64String(hashBytes));
            }
            finally
            {
                Log.Error("Problem With Encrypt");
            }
        }
Ejemplo n.º 57
0
        }  // END public String GetHash(String ValueToHash, String ValueLocale, String SaltToUse)

        /// <summary>
        /// This function takes a value you want hashed, uses SHA-512 for strength, and includes a locale extender such as a computer name,
        /// and a salt value that has no intrinsic meaning.  The value returned is the hash string in Base 64.
        /// </summary>
        /// <param name="ValueToHash">This is the value, such as a password.  It will usually be the same over a number of instances on multiple machines.</param>
        /// <param name="ValueLocale">This is a value specific to the machine, such as machine name.  It should be deterministic on every use.</param>
        /// <param name="SaltToUse">This is a value that extends the hash for security.</param>
        /// <returns>The value returned is the hash string in Base 64</returns>
        public String GetHash(String ValueToHash, String ValueLocale, String SaltToUse)
        {
            DateTime dtmMethodStart = DateTime.Now;

            String ReturnValue = "";

            SHA512Managed Hasher = null;

            try
            {
                Hasher = new System.Security.Cryptography.SHA512Managed();

                Byte[] Text2Hash = System.Text.Encoding.UTF8.GetBytes(String.Concat(ValueToHash.Trim(),
                                                                                    ValueLocale.ToUpper(),
                                                                                    SaltToUse.Trim()));

                Byte[] PlateOfHash = Hasher.ComputeHash(Text2Hash);

                ReturnValue = Convert.ToBase64String(PlateOfHash);
            }
            catch (Exception exUnhandled)
            {
                exUnhandled.Data.Add("ValueToHash", ValueToHash ?? "");
                exUnhandled.Data.Add("ValueLocale", ValueLocale ?? "");
                exUnhandled.Data.Add("SaltToUse", SaltToUse ?? "");

                throw;
            } // END catch
            finally
            {
                if (Hasher != null)
                {
                    Hasher.Clear();

                    Hasher.Dispose();

                    Hasher = null;
                }
            }

            return(ReturnValue);
        } // END public  String GetHash(String ValueToHash, String ValueLocale, String SaltToUse)
Ejemplo n.º 58
0
        /*Kóðinn í CreatePasswordHash er tekinn af http://stackoverflow.com/questions/12592036/taking-a-c-sharp-sha512-hash-and-compare-it-in-php
         * og aðlagaður*/
        public static string CreatePasswordHash(string password)
        {
            string Pwd    = password;
            SHA512 sha512 = new System.Security.Cryptography.SHA512Managed(); //Nær í afrit af klasanum SHA512Managed til að við getum hashað

            byte[] sha512Bytes = System.Text.Encoding.UTF8.GetBytes(Pwd);     //Þarna fáum við út byte-in og encodum þau í UTF8 og geymum
            //þessi byte í byte array.

            byte[] cryString = sha512.ComputeHash(sha512Bytes);//Hér er hvert og eitt hash byte reiknað út

            string hashedPwd = null;

            //Svo bara lúppum við í gegnum cryString sem inniheldur hashana. Við setjum þá saman í streng með þessari lúppu:
            for (int i = 0; i < cryString.Length; i++)
            {
                hashedPwd += cryString[i].ToString();
            }

            return(hashedPwd);
        }
Ejemplo n.º 59
0
        public static string CreatePasswordHash(string _password, string _salt)
        {
            string saltAndPwd = String.Concat(_password, _salt);
            SHA512 sha512     = new System.Security.Cryptography.SHA512Managed();

            byte[] sha512Bytes = System.Text.Encoding.Default.GetBytes(saltAndPwd);

            byte[] cryString = sha512.ComputeHash(sha512Bytes);

            string hashedPwd = string.Empty;

            for (int i = 0; i < cryString.Length; i++)
            {
                hashedPwd += cryString[i].ToString("X");
            }

            sha512.Clear();
            sha512.Dispose();

            return(hashedPwd);
        }
Ejemplo n.º 60
0
        public String GetHash(String s)
        {
            System.Security.Cryptography.SHA512Managed sha512 = new System.Security.Cryptography.SHA512Managed();

            //compute hash value from string, returns a byte array
            hash = sha512.ComputeHash(Encoding.UTF8.GetBytes(s));

            StringBuilder stringBuilder = new StringBuilder();

            foreach (byte b in hash)
            {
                //convert each byte of hash value to string
                stringBuilder.Append(b.ToString("X2"));
            }

            String temp = stringBuilder.ToString();             //convert to string

            String temp1 = temp.Substring(0, passwordLength);   //truncate password to desired length

            String password = AddLowerCase(temp1);

            return(password);
        }