Exemple #1
0
        public static string sha512(string s)
        {
            SHA512 sha512 = System.Security.Cryptography.SHA512.Create();

            byte[] inputbytes = System.Text.Encoding.ASCII.GetBytes(s);
            byte[] hash       = sha512.ComputeHash(inputbytes);

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return(sb.ToString().ToLower());
        }
Exemple #2
0
        /// <summary>
        /// Methode um einen String konform für MS-SQL Server 2012 'sha2_512' zu hashen
        /// </summary>
        /// <param name="text">der text zum hashen</param>
        /// <returns>Gibt ein ByteArray des gehashten Strings zurück</returns>
        public static Byte[] GetHash(string text)
        {
            //Quelle: Kloiber Christian
            //Damit der Hashwert auch richtig generiert wird MUSS auf Encoding 1252 gestellt werden
            // Erst nachdem das Bytearray mit einem encodierten string nach 1252 erstellt wurde
            // wird dieser mittels ComputeHash gehasht. Danach ist dieser Wert mit
            // MS-SQL Server 2012 VARBINARY(HashByte('sha2_512','HashText')) konform.
            Byte[]   hashbytes   = null;
            SHA512   alg         = SHA512.Create();
            Encoding windows1252 = Encoding.GetEncoding(1252);

            Byte[] result = windows1252.GetBytes(text);
            hashbytes = alg.ComputeHash(result);
            return(hashbytes);
        }
Exemple #3
0
        private static string generate_sha512_string(string input_string)
        {
            SHA512 sha512 = SHA512Managed.Create();

            byte[]        bytes = Encoding.UTF8.GetBytes(input_string);
            byte[]        hash  = sha512.ComputeHash(bytes);
            StringBuilder sb    = new StringBuilder();

            for (int i = 0; i <= hash.Length - 1; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }

            return(sb.ToString());
        }
Exemple #4
0
 public static string Encrypt(string value)
 {
     try
     {
         SHA512 sha512 = SHA512Managed.Create();
         byte[] bytes  = Encoding.UTF8.GetBytes(value);
         byte[] hash   = sha512.ComputeHash(bytes);
         return(GetStringFromHash(hash));
     }
     catch (Exception e)
     {
         Console.WriteLine("[Error] UserDatabase - Failed to encrypt a value : " + e.Message);
         return(null);
     }
 }
Exemple #5
0
        public string TextToSHA512(string password)
        {
            string encryptedPassword = null;
            SHA512 hasher = SHA512.Create();
            byte[] bytes = Encoding.UTF8.GetBytes(password);
            byte[] hash = hasher.ComputeHash(bytes);

            StringBuilder result = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                result.Append(hash[i].ToString("X2"));
            }
            encryptedPassword = result.ToString();
            return encryptedPassword;
        }
            public static string GenerateSHA512String(string inputString)
            {
                SHA512 sha512 = SHA512.Create();

                byte[]        bytes = Encoding.UTF8.GetBytes(inputString);
                byte[]        hash  = sha512.ComputeHash(bytes);
                StringBuilder sb    = new StringBuilder();

                for (int i = 0; i < hash.Length; i++)
                {
                    sb.Append(hash[i].ToString("X2"));
                }

                return(sb.ToString());
            }
            public static string Get(string str)
            {
                SHA512        sha512   = System.Security.Cryptography.SHA512.Create();
                ASCIIEncoding encoding = new ASCIIEncoding();

                byte[]        stream = null;
                StringBuilder sb     = new StringBuilder();

                stream = sha512.ComputeHash(encoding.GetBytes(str));
                for (int i = 0; i < stream.Length; i++)
                {
                    sb.AppendFormat("{0:x2}", stream[i]);
                }
                return(sb.ToString());
            }
        static byte[][] getHash(string filePath)
        {
            byte[][] retValue = new byte[6][];

            try {
                FileStream[] fsArray = new FileStream[6]
                {
                    new FileStream(filePath, FileMode.Open),
                    new FileStream(filePath, FileMode.Open),
                    new FileStream(filePath, FileMode.Open),
                    new FileStream(filePath, FileMode.Open),
                    new FileStream(filePath, FileMode.Open),
                    new FileStream(filePath, FileMode.Open)
                };

                retValue[0] = md5.ComputeHash(fsArray[0]);
                retValue[1] = ripemd160.ComputeHash(fsArray[1]);
                retValue[2] = sha1.ComputeHash(fsArray[2]);
                retValue[3] = sha256.ComputeHash(fsArray[3]);
                retValue[4] = sha384.ComputeHash(fsArray[4]);
                retValue[5] = sha512.ComputeHash(fsArray[5]);

                for (int y = 0; y < 6; ++y)
                {
                    fsArray[y].Close();
                    fsArray[y].Dispose();
                }
            }
            catch (Exception ex) {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("ERROR - " + ex.Message);
                Console.ForegroundColor = ConsoleColor.Gray;
                Environment.Exit(0xFF);
            }
            return(retValue);
        }
Exemple #9
0
        private string EncryptPassword(string password)
        {
            string hash = string.Empty;

            byte[] salty = GenerateRandomCryptographicBytes(64);
            using (SHA512 sha512Hash = SHA512.Create())
            {
                //From String to byte array
                byte[] sourceBytes = Encoding.UTF8.GetBytes(password + salty);
                byte[] hashBytes   = sha512Hash.ComputeHash(sourceBytes);
                hash = BitConverter.ToString(hashBytes).Replace("-", String.Empty);
            }

            return(hash);
        }
Exemple #10
0
 private byte[] Encode_SHA512(string updatedKey)
 {
     try
     {
         using (SHA512 sha = SHA512.Create())
         {
             byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(updatedKey));
             return(hash);
         }
     }
     catch (Exception ex)
     {
         throw new Exception("Error while generating hash key", ex);
     }
 }
        public static string CreateHash(string userPassword, string salt)
        {
            string hashResult = "";

            using (SHA512 sha512Hash = SHA512.Create())
            {
                // From String to byte array + salt
                byte[] hashBytes = sha512Hash.ComputeHash(Encoding.UTF8.GetBytes(userPassword + salt));

                // Converting hashed byte array back to string format
                hashResult = BitConverter.ToString(hashBytes).Replace("-", String.Empty);
            }

            return(hashResult);
        }
        public static string GenerateSHA512(string plainText)
        {
            // Create a SHA256
            using (SHA512 sha = SHA512.Create())
            {
                byte[] bytes = sha.ComputeHash(Encoding.UTF8.GetBytes(plainText));

                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < bytes.Length; i++)
                {
                    builder.Append(bytes[i].ToString("x2"));
                }
                return(builder.ToString());
            }
        }
Exemple #13
0
        /// <summary>
        /// SHA512: recibe un string que nos permite convertirlo a SHA512.
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string SHA512_Certificado(string vp_Texto)
        {
            SHA512        vl_SHA512   = SHA512Managed.Create();
            ASCIIEncoding vl_Encoding = new ASCIIEncoding();

            byte[]        vl_Stream = null;
            StringBuilder vl_SB     = new StringBuilder();

            vl_Stream = vl_SHA512.ComputeHash(vl_Encoding.GetBytes(vp_Texto));
            for (int i = 0; i < vl_Stream.Length; i++)
            {
                vl_SB.AppendFormat("{0:x2}", vl_Stream[i]);
            }
            return(vl_SB.ToString());
        }
Exemple #14
0
        public static byte[] SHA512String(string inputString)
        {
            SHA512 sha = SHA512.Create();

            byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(inputString));

            StringBuilder result = new StringBuilder(hash.Length * 2);

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

            return(hash);
        }
Exemple #15
0
 public static string CalculateSHA512Hash(string input)
 {
     try
     {
         SHA512 sha512     = SHA512.Create();
         byte[] inputBytes = Encoding.UTF8.GetBytes(input);
         byte[] hash       = sha512.ComputeHash(inputBytes);
         return(HexString(hash));
     }
     catch (Exception ex)
     {
         //Utilities.OnError(Utilities.GetCurrentMethod(), ex);
     }
     throw new CryptographicException("Operation Failed.");
 }
Exemple #16
0
    static byte[] Decrypt(byte[] buff, byte[] iv, byte[] dat)
    {
        RijndaelManaged ri = new RijndaelManaged();

        byte[]       ret = new byte[dat.Length];
        MemoryStream ms  = new MemoryStream(dat);

        using (CryptoStream cStr = new CryptoStream(ms, ri.CreateDecryptor(SHA256.Create().ComputeHash(buff), iv), CryptoStreamMode.Read))
        { cStr.Read(ret, 0, dat.Length); }

        SHA512 sha = SHA512.Create();

        byte[] c = sha.ComputeHash(buff);
        for (int i = 0; i < ret.Length; i += 64)
        {
            int len = ret.Length <= i + 64 ? ret.Length : i + 64;
            for (int j = i; j < len; j++)
            {
                ret[j] ^= (byte)(c[j - i] ^ Mutation.Key6I);
            }
            c = sha.ComputeHash(ret, i, len - i);
        }
        return(ret);
    }
Exemple #17
0
 public static string CalculateSHA512HashFile(string fileName)
 {
     try
     {
         SHA512 sha512     = SHA512.Create();
         byte[] inputBytes = System.IO.File.ReadAllBytes(fileName);
         byte[] hash       = sha512.ComputeHash(inputBytes);
         return(HexString(hash));
     }
     catch (Exception ex)
     {
         //Utilities.OnError(Utilities.GetCurrentMethod(), ex);
     }
     throw new CryptographicException("Operation Failed.");
 }
Exemple #18
0
        public static string Password_Hash(string _value)
        {
            StringBuilder sb = new StringBuilder();

            SHA512 _hash = System.Security.Cryptography.SHA512Managed.Create();

            byte[] inputTmp = _hash.ComputeHash(Encoding.UTF8.GetBytes(SALT + _value + SALT));

            foreach (byte b in inputTmp)
            {
                sb.Append(b.ToString("x2"));
            }

            return(sb.ToString());
        }
 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));
     }
 }
Exemple #20
0
        public static byte[] GetSha512HashedData(string clearData)
        {
            try
            {
                SHA512          s512          = SHA512.Create();
                UnicodeEncoding ByteConverter = new UnicodeEncoding();
                byte[]          bytes         = s512.ComputeHash(ByteConverter.GetBytes(clearData));

                return(bytes);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public string Hash(string digest)
        {
            if (String.IsNullOrWhiteSpace(digest))
            {
                throw new ArgumentException("Digest should be non-null non-whitespace string");
            }

            using (SHA512 sha = SHA512.Create())
            {
                byte[] digestBytes = Encoding.Unicode.GetBytes(digest);
                byte[] resultBytes = sha.ComputeHash(digestBytes);

                return(Convert.ToBase64String(resultBytes));
            }
        }
        private async void SaveEmployeeButton_Clicked(object sender, EventArgs e)
        {
            try
            {
                SHA512 sha512 = SHA512.Create();
                byte[] bytes  = Encoding.UTF8.GetBytes(EmployeePasswordEntry.Text);
                byte[] hash   = sha512.ComputeHash(bytes);


                WorkModel data = new WorkModel()
                {
                    EmpOperation   = "Save",
                    ContractorName = ContractorPicker.SelectedItem.ToString(),
                    UserName       = EmployeeUsernameEntry.Text,
                    Password       = hash,
                    FirstName      = FirstNameEntry.Text,
                    LastName       = LastNameEntry.Text,
                    PhoneNumber    = int.Parse(PhoneNumberEntry.Text),
                    Email          = EmailEntry.Text
                };

                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri("https://worksheet.azurewebsites.net");
                string        input   = JsonConvert.SerializeObject(data);
                StringContent content = new StringContent(input, Encoding.UTF8, "application/json");

                HttpResponseMessage message = await client.PostAsync("/api/employee", content);

                string reply = await message.Content.ReadAsStringAsync();

                bool success = JsonConvert.DeserializeObject <bool>(reply);

                if (success)
                {
                    await DisplayAlert("Saved!", "A new employee has been added!", "Close");

                    await PopupNavigation.PopAsync();
                }
                else
                {
                    await DisplayAlert("Save Failed", "Sorry, could not save employee..", "Close");
                }
            }
            catch
            {
                await DisplayAlert("Save Failed!", "Sorry, couldn't get any data from database..", "OK");
            }
        }
        private void Login(string usuario, string clave)
        {
            Usuario usu = null;
            SHA512  alg = SHA512.Create();

            byte[] result = alg.ComputeHash(Encoding.UTF8.GetBytes(clave));
            var    pass   = Encoding.UTF8.GetString(result);

            if (usuario == "ADMIN" && clave == "QWER")
            {
                usu = new Usuario()
                {
                    Nombre = "ADMIN"
                };
                usu.UnidadNegocioPorDefecto = null;
                var servicioEmpresa = FabricaClienteServicio.Instancia.CrearCliente <IServicioABM <Empresa> >();

                //usu.EmpresaPorDefecto = servicioEmpresa.ObtenerPorId(4,CargarRelaciones.NoCargarNada);

                // usu.EmpresaPorDefecto = servicioEmpresa.ObtenerPorCodigo("01", CargarRelaciones.NoCargarNada, "", null);

                usu.EmpresaPorDefecto = servicioEmpresa.ObtenerPorId(1, CargarRelaciones.NoCargarNada, "");
            }
            else
            {
                usu = this.ServicioLogin(usuario.ToUpperInvariant(), pass.ToUpperInvariant());
            }

            if (usu != null)
            {
                this.UnidadDeNegocioActual = usu.UnidadNegocioPorDefecto;
                this.EmpresaActual         = usu.EmpresaPorDefecto;
                this.usuarioActual         = usu;
                this.LoginOk = true;
                this.LoginWindow.Close();
            }
            else
            {
                //si entraste aca es porque la cagaste
                Mensajes.Error("Usuario o Clave incorrecto");
                this.intentos++;
            }

            if (this.intentos == 3)
            {
                this.Cancelar();
            }
        }
Exemple #24
0
        private string CreateSHA512(string hash = null)
        {
            using (SHA512 sHA512 = SHA512.Create())
            {
                byte[] inputBytes = hash == null?Encoding.ASCII.GetBytes($"{ _UserName}{_Password}{_Salt}") :  Encoding.ASCII.GetBytes($"{hash}{_Salt}");

                byte[] hashBytes = sHA512.ComputeHash(inputBytes);

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    sb.Append(hashBytes[i].ToString("X2"));
                }
                return(sb.ToString());
            }
        }
Exemple #25
0
        private static string ToSHA512(string s)
        {
            SHA512 sha = SHA512.Create();

            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s);
            byte[] hash  = sha.ComputeHash(bytes);

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }

            return(sb.ToString());
        }
Exemple #26
0
        public static string ComputeSha512Hash(string rawData)
        {
            // Create a SHA256
            using SHA512 sha512Hash = SHA512.Create();
            // ComputeHash - returns byte array
            byte[] bytes = sha512Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));

            // Convert byte array to a string
            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < bytes.Length; i++)
            {
                builder.Append(bytes[i].ToString("x2"));
            }
            return(builder.ToString());
        }
        public static string Criptografar(string Txt, string Salt)
        {
            using (SHA512 sha512Hash = SHA512.Create())
            {
                // ComputeHash - retorna uma array de bytes
                byte[] bytes = sha512Hash.ComputeHash(Encoding.UTF8.GetBytes(Salt + Txt));

                // Converterter array de bytes para string
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < bytes.Length; i++)
                {
                    builder.Append(bytes[i].ToString("x2"));
                }
                return(builder.ToString());
            }
        }
Exemple #28
0
        public static String CryptPassword(string password)
        {
            byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
            SHA512 hasher        = SHA512.Create();

            byte[]        encryptedPasswordBytes = hasher.ComputeHash(passwordBytes);
            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < encryptedPasswordBytes.Length; i++)
            {
                builder.Append(encryptedPasswordBytes[i].ToString("x2"));
            }
            String encryptedPassword = builder.ToString();

            return(encryptedPassword);
        }
Exemple #29
0
        public static string ComputeSha512Hash(string rawData)
        {
            // Create a SHA512
            using SHA512 sha256Hash = SHA512.Create();
            // ComputeHash - returns byte array
            byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));

            // Convert byte array to a string
            StringBuilder builder = new StringBuilder();

            foreach (var t in bytes)
            {
                builder.Append(t.ToString("x2"));
            }
            return(builder.ToString());
        }
Exemple #30
0
 public static string ConvertBack(string value)
 {
     if (string.IsNullOrEmpty(value))
     {
         return(null);
     }
     using (SHA512 cripto = SHA512.Create()) {
         var bytes = cripto.ComputeHash(new UTF8Encoding().GetBytes(value + "phmicojib"));
         var psw   = new StringBuilder();
         foreach (byte b in bytes)
         {
             psw.Append(b.ToString(CultureInfo.InvariantCulture));
         }
         return(psw.ToString());
     }
 }