/// <summary> /// Returns an SHA512 hashed and base64-encoded result from the given plain text. /// </summary> /// <param name="plainText"></param> /// <returns></returns> public static string SHAHashBase64(string plainText) { SHA512 sp = SHA512CryptoServiceProvider.Create(); byte[] hash = sp.ComputeHash(System.Text.Encoding.Default.GetBytes(plainText)); return(Convert.ToBase64String(hash)); }
public ActionResult Create(UserAccount accountToCreate) { if (ModelState.IsValid) { // If the accountToCreate object is valid // we'll need to save it in a database Catalogue.Models.CatalogueDBEntities userAccounts = new Models.CatalogueDBEntities(); userAccounts.Database.CreateIfNotExists(); if (userAccounts.Users.Count <Models.User>() > 0) { foreach (Models.User user in userAccounts.Users) { if (string.Compare(accountToCreate.UserName.ToLower(), user.UserName.ToLower()) == 0) { ViewBag.Error = "Username already exists"; return(View(accountToCreate)); } } } Models.User newUser = new Models.User(); newUser.UserName = accountToCreate.UserName; newUser.Email = accountToCreate.Email; newUser.UserID = userAccounts.Users.Count <Models.User>() + 1; SHA512 encryption = SHA512CryptoServiceProvider.Create(); byte[] data = new byte[accountToCreate.Password.Length]; byte[] result; for (int i = 0; i < accountToCreate.Password.Length; i++) { data[i] = (byte)accountToCreate.Password[i]; } result = encryption.ComputeHash(data); string newString = ""; for (int i = 0; i < result.Length; i++) { newString += String.Format("{0:X2}", result[i]); } newUser.Password = newString; userAccounts.Users.Add(newUser); userAccounts.SaveChanges(); // After saving we'll redirect the user back to Login's Index page return(Redirect("/")); } // Invalid -- redisplay form with errors return(View(accountToCreate)); }
public static string Sha512Cng(this string self) { using (var sha512cng = SHA512CryptoServiceProvider.Create()) { return(sha512cng.ComputeHash(Encoding.UTF8.GetBytes(self)) .Select(hash => hash.ToString("x2")) .Join(string.Empty)); } }
private string GetSHA512(string filename) { using (var sha = SHA512CryptoServiceProvider.Create()) { using (var stream = File.OpenRead(filename)) { return(Encoding.Default.GetString(sha.ComputeHash(stream))); } } }
/// <summary> /// Returns an SHA512 hashed and hex-encoded result from the given plain text. /// </summary> /// <param name="plainText"></param> /// <returns></returns> public static string SHAHash(string plainText) { SHA512 sp = SHA512CryptoServiceProvider.Create(); byte[] hash = sp.ComputeHash(System.Text.Encoding.Default.GetBytes(plainText)); // Convert the hash to a hex-encoded string string ss = HexStringFromBytes(hash, hash.Length); return(ss); }
/// <summary> /// Gets the alogrithm. /// </summary> /// <param name="method">The method.</param> /// <param name="hashName">Name of the hash algorithm.</param> /// <returns></returns> /// <exception cref="System.NotSupportedException"> /// </exception> private static HashAlgorithm GetHashAlogrithm(HashMethod method, string hashName = null) { if (hashName == null) { switch (method) { case HashMethod.MD5: return(MD5CryptoServiceProvider.Create()); case HashMethod.SHA1: return(SHA1CryptoServiceProvider.Create()); case HashMethod.SHA256: return(SHA256CryptoServiceProvider.Create()); case HashMethod.SHA384: return(SHA384CryptoServiceProvider.Create()); case HashMethod.SHA512: return(SHA512CryptoServiceProvider.Create()); case HashMethod.CRC32: return(new CRC32()); default: throw new NotSupportedException( string.Format("Method [{0}] is not supported.", method.ToString())); } } else { switch (method) { case HashMethod.MD5: return(MD5CryptoServiceProvider.Create(hashName)); case HashMethod.SHA1: return(SHA1CryptoServiceProvider.Create(hashName)); case HashMethod.SHA256: return(SHA256CryptoServiceProvider.Create(hashName)); case HashMethod.SHA384: return(SHA384CryptoServiceProvider.Create(hashName)); case HashMethod.SHA512: return(SHA512CryptoServiceProvider.Create(hashName)); default: throw new NotSupportedException( string.Format("Method [{0}] is not supported.", method.ToString())); } } }
public static string CalculateHash(byte[] data) { using (var sha = SHA512CryptoServiceProvider.Create()) { var hash = sha.ComputeHash(data); var builder = new StringBuilder(hash.Length * 2); for (var i = 0; i < hash.Length; ++i) { builder.Append(_hex[hash[i]]); } return(builder.ToString()); } }
/// <summary>Constructor</summary> /// <param name="param">ECParameters</param> /// <param name="isPrivate">bool</param> public JWS_ES512_Param(ECParameters param, bool isPrivate) { // Cng or OpenSsl if (os.Platform == PlatformID.Win32NT) { this.DigitalSignECDsaCng = new DigitalSignECDsaCng(param, isPrivate); } else { #if NETSTD this.DigitalSignECDsaOpenSsl = new DigitalSignECDsaOpenSsl( param, SHA512CryptoServiceProvider.Create()); //HashAlgorithmCmnFunc.CreateHashAlgorithmSP(EnumHashAlgorithm.SHA512_M)); #else throw new NotImplementedException(PublicExceptionMessage.NOT_IMPLEMENTED); #endif } }
public string CalculatePasswordHash(string userName, int userID, string password) { SHA512 hashProvider512 = SHA512CryptoServiceProvider.Create(); SHA256 hashProvider256 = SHA256CryptoServiceProvider.Create(); string salt = userID.ToString() + userName; // construct the salt from the useriD + username byte[] saltBytes = new byte[salt.Length * sizeof(char)]; // initialise new bytearray, size of the string times size of each char System.Buffer.BlockCopy(salt.ToCharArray(), 0, saltBytes, 0, salt.Length); // don't use encoding, just copy pure byte data byte[] saltHash = hashProvider256.ComputeHash(saltBytes); // compute the hash from the salt string string saltString = Convert.ToBase64String(saltHash); // convert the hash back to a string string passwordString = password + saltString; // construct the complete password from the password and the saltstring byte[] passwordBytes = new byte[passwordString.Length * sizeof(char)]; // and initialise the new bytearray System.Buffer.BlockCopy(passwordString.ToCharArray(), 0, passwordBytes, 0, passwordString.Length); // copy it over byte[] passwordHash = hashProvider512.ComputeHash(passwordBytes); // generate the final hash string passwordHashString = Convert.ToBase64String(passwordHash); // and transform that back into a stringr return(passwordHashString); }
protected override global::System.Security.Cryptography.HashAlgorithm New() { return(SHA512CryptoServiceProvider.Create()); }
/// <summary>ハッシュ(キー無し)サービスプロバイダの生成</summary> /// <param name="eha">ハッシュ(キー無し)サービスプロバイダの列挙型</param> /// <returns>ハッシュ(キー無し)サービスプロバイダ</returns> /// <remarks> /// EnumHashAlgorithmから、HashAlgorithmを生成するために追加。 /// HashAlgorithm.Create(HashNameConst.SHA256) は .NET Core 2 で動作せず。 /// - KeyedHashAlgorithm.Create("HMACSHA1") throw PNSE (on .NET Core 2 /// https://github.com/dotnet/standard/issues/530#issuecomment-375043416 /// </remarks> public static HashAlgorithm CreateHashAlgorithmSP(EnumHashAlgorithm eha) { // ハッシュ(キー無し)サービスプロバイダ HashAlgorithm ha = null; if (eha == EnumHashAlgorithm.Default) { // 既定の暗号化サービスプロバイダ ha = HashAlgorithmCmnFunc.GetHashAlgorithmFromNameString(); // devps(1703) } #region MD5 else if (eha == EnumHashAlgorithm.MD5_CSP) { // MD5CryptoServiceProviderサービスプロバイダ ha = MD5CryptoServiceProvider.Create(); // devps(1703) } #if NETSTD #else else if (eha == EnumHashAlgorithm.MD5_CNG) { // MD5Cngサービスプロバイダ ha = MD5Cng.Create(); // devps(1703) } #endif #endregion #region RIPEMD160 else if (eha == EnumHashAlgorithm.RIPEMD160_M) { #if NETSTD ha = null; // BouncyCastleを使用する。 #else // RIPEMD160Managedサービスプロバイダ ha = RIPEMD160Managed.Create(); // devps(1703) #endif } #endregion #region SHA1 else if (eha == EnumHashAlgorithm.SHA1_CSP) { // SHA1CryptoServiceProviderサービスプロバイダ ha = SHA1CryptoServiceProvider.Create(); // devps(1703) } #if NETSTD #else else if (eha == EnumHashAlgorithm.SHA1_CNG) { // SHA1Cngサービスプロバイダ ha = SHA1Cng.Create(); // devps(1703) } #endif else if (eha == EnumHashAlgorithm.SHA1_M) { // SHA1Managedサービスプロバイダ ha = SHA1Managed.Create(); // devps(1703) } #endregion #region SHA256 else if (eha == EnumHashAlgorithm.SHA256_CSP) { // SHA256CryptoServiceProviderサービスプロバイダ ha = SHA256CryptoServiceProvider.Create(); // devps(1703) } #if NETSTD #else else if (eha == EnumHashAlgorithm.SHA256_CNG) { // SHA256Cngサービスプロバイダ ha = SHA256Cng.Create(); // devps(1703) } #endif else if (eha == EnumHashAlgorithm.SHA256_M) { // SHA256Managedサービスプロバイダ ha = SHA256Managed.Create(); // devps(1703) } #endregion #region SHA384 else if (eha == EnumHashAlgorithm.SHA384_CSP) { // SHA384CryptoServiceProviderサービスプロバイダ ha = SHA384CryptoServiceProvider.Create(); // devps(1703) } #if NETSTD #else else if (eha == EnumHashAlgorithm.SHA384_CNG) { // SHA384Cngサービスプロバイダ ha = SHA384Cng.Create(); // devps(1703) } #endif else if (eha == EnumHashAlgorithm.SHA384_M) { // SHA384Managedサービスプロバイダ ha = SHA384Managed.Create(); // devps(1703) } #endregion #region SHA512 else if (eha == EnumHashAlgorithm.SHA512_CSP) { // SHA512CryptoServiceProviderサービスプロバイダ ha = SHA512CryptoServiceProvider.Create(); // devps(1703) } #if NETSTD #else else if (eha == EnumHashAlgorithm.SHA512_CNG) { // SHA512Cngサービスプロバイダ ha = SHA512Cng.Create(); // devps(1703) } #endif else if (eha == EnumHashAlgorithm.SHA512_M) { // SHA512Managedサービスプロバイダ ha = SHA512Managed.Create(); // devps(1703) } #endregion else { // 既定の暗号化サービスプロバイダ ha = HashAlgorithmCmnFunc.GetHashAlgorithmFromNameString(); // devps(1703) } return(ha); }