public static byte[] GetScriptHashFromPublicKey(byte[] publicKey) { byte[] script = GetScriptFromPublicKey(publicKey); var scripthash = sha256.ComputeHash(script); scripthash = ripemd160.ComputeHash(scripthash); return(scripthash); }
} // End Function Sha256_2 public static string BitcoinAddressHash(string bla) { byte[] ba = System.Text.Encoding.UTF8.GetBytes(bla); /* * // https://www.nuget.org/packages/System.Security.Cryptography.Algorithms/ * // https://dotnet.myget.org/feed/cli-deps/package/nuget/System.Security.Cryptography.Algorithms * // PM> Install-Package System.Security.Cryptography.Algorithms -Version 4.2.0 * using (var algorithm = System.Security.Cryptography.Algorithms.SHA256.Create()) * { * // Create the at_hash using the access token returned by CreateAccessTokenAsync. * var hash = algorithm.ComputeHash(Encoding.ASCII.GetBytes(response.AccessToken)); * * // Note: only the left-most half of the hash of the octets is used. * // See http://openid.net/specs/openid-connect-core-1_0.html#CodeIDToken * identity.AddClaim(JwtRegisteredClaimNames.AtHash, Base64UrlEncoder.Encode(hash, 0, hash.Length / 2)); * } */ using (System.Security.Cryptography.SHA256CryptoServiceProvider sha256 = new System.Security.Cryptography.SHA256CryptoServiceProvider()) { using (System.Security.Cryptography.RIPEMD160 md160 = System.Security.Cryptography.RIPEMD160.Create()) { ba = sha256.ComputeHash(ba); ba = md160.ComputeHash(ba); } /* * using (System.Security.Cryptography.RIPEMD160 md160 = new System.Security.Cryptography.RIPEMD160Managed()) * { * ba = sha256.ComputeHash(ba); * ba = md160.ComputeHash(ba); * } // End Using md160 */ } // End Using sha256 return(System.BitConverter.ToString(ba).Replace("-", "").ToLowerInvariant()); } // End Function BitcoinAddressHash
/// <summary> /// Register an account. Returns true if succeed, otherwise return false and write logs. /// </summary> /// <param name="username"></param> /// <param name="rawPassword"></param> /// <param name="hashType"></param> /// <returns></returns> public static bool RegisterUser(string username, string rawPassword, int hashType) { try { string hashSaltBase64 = null; string passwordHashBase64 = null; byte[] saltBytes; System.Security.Cryptography.HMAC hmac; switch (hashType) { case (int)Enums.HMAC.MD5: hashSaltBase64 = null; using (System.Security.Cryptography.MD5 hasher = System.Security.Cryptography.MD5.Create()) { passwordHashBase64 = Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(rawPassword))); } break; case (int)Enums.HMAC.RIPEMD160: hashSaltBase64 = null; using (System.Security.Cryptography.RIPEMD160 hasher = System.Security.Cryptography.RIPEMD160.Create()) { passwordHashBase64 = Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(rawPassword))); } break; case (int)Enums.HMAC.SHA1: hashSaltBase64 = null; using (System.Security.Cryptography.SHA1 hasher = System.Security.Cryptography.SHA1.Create()) { passwordHashBase64 = Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(rawPassword))); } break; case (int)Enums.HMAC.SHA256: hashSaltBase64 = null; using (System.Security.Cryptography.SHA256 hasher = System.Security.Cryptography.SHA256.Create()) { passwordHashBase64 = Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(rawPassword))); } break; case (int)Enums.HMAC.SHA384: hashSaltBase64 = null; using (System.Security.Cryptography.SHA384 hasher = System.Security.Cryptography.SHA384.Create()) { passwordHashBase64 = Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(rawPassword))); } break; case (int)Enums.HMAC.SHA512: hashSaltBase64 = null; using (System.Security.Cryptography.SHA512 hasher = System.Security.Cryptography.SHA512.Create()) { passwordHashBase64 = Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(rawPassword))); } break; case (int)Enums.HMAC.HMACMD5: saltBytes = new byte[Settings.InitSetting.Instance.MaxNumberOfBytesInSalt]; new Random().NextBytes(saltBytes); hashSaltBase64 = Convert.ToBase64String(saltBytes); hmac = new System.Security.Cryptography.HMACMD5(saltBytes); passwordHashBase64 = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(rawPassword))); break; case (int)Enums.HMAC.HMACRIPEMD160: saltBytes = new byte[Settings.InitSetting.Instance.MaxNumberOfBytesInSalt]; new Random().NextBytes(saltBytes); hashSaltBase64 = Convert.ToBase64String(saltBytes); hmac = new System.Security.Cryptography.HMACRIPEMD160(saltBytes); passwordHashBase64 = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(rawPassword))); break; case (int)Enums.HMAC.HMACSHA1: saltBytes = new byte[Settings.InitSetting.Instance.MaxNumberOfBytesInSalt]; new Random().NextBytes(saltBytes); hashSaltBase64 = Convert.ToBase64String(saltBytes); hmac = new System.Security.Cryptography.HMACSHA1(saltBytes); passwordHashBase64 = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(rawPassword))); break; case (int)Enums.HMAC.HMACSHA256: saltBytes = new byte[Settings.InitSetting.Instance.MaxNumberOfBytesInSalt]; new Random().NextBytes(saltBytes); hashSaltBase64 = Convert.ToBase64String(saltBytes); hmac = new System.Security.Cryptography.HMACSHA256(saltBytes); passwordHashBase64 = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(rawPassword))); break; case (int)Enums.HMAC.HMACSHA384: saltBytes = new byte[Settings.InitSetting.Instance.MaxNumberOfBytesInSalt]; new Random().NextBytes(saltBytes); hashSaltBase64 = Convert.ToBase64String(saltBytes); hmac = new System.Security.Cryptography.HMACSHA384(saltBytes); passwordHashBase64 = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(rawPassword))); break; case (int)Enums.HMAC.HMACSHA512: saltBytes = new byte[Settings.InitSetting.Instance.MaxNumberOfBytesInSalt]; new Random().NextBytes(saltBytes); hashSaltBase64 = Convert.ToBase64String(saltBytes); hmac = new System.Security.Cryptography.HMACSHA512(saltBytes); passwordHashBase64 = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(rawPassword))); break; default: throw new NotImplementedException("Unspecified hash type."); } var acc = new Account() { Uname = username, HashSaltBase64 = hashSaltBase64, HashTypeId = hashType, PasswordHashBase64 = passwordHashBase64, IsTwoFactor = false, TwoFactorSecretBase32 = null }; using (AuthorizeEntities ctx = new AuthorizeEntities()) { ctx.Accounts.Add(acc); ctx.SaveChanges(); } } catch (Exception ex) { Log4netLogger.Error(MethodBase.GetCurrentMethod().DeclaringType, "Cannot register user", ex); return(false); } return(true); }
/// <summary> /// Hash of string as byte array. /// </summary> /// <param name="inString">Input string</param> /// <returns>Output bytes</returns> public byte[] hashString(string inString) { byte[] toHash = System.Text.Encoding.ASCII.GetBytes(inString); return(csp.ComputeHash(toHash)); }
public static bool Login(string username, string password) { bool isOk = false; byte[] saltBytes; System.Security.Cryptography.HMAC hmac; try { using (AuthorizeEntities ctx = new AuthorizeEntities()) { var acc = ctx.Accounts.Where(x => x.Uname == username).FirstOrDefault(); if (acc != null) { switch (acc.HashTypeId) { case (int)Enums.HMAC.MD5: using (System.Security.Cryptography.MD5 hasher = System.Security.Cryptography.MD5.Create()) { isOk = acc.PasswordHashBase64.Equals(Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(password)))); } break; case (int)Enums.HMAC.RIPEMD160: using (System.Security.Cryptography.RIPEMD160 hasher = System.Security.Cryptography.RIPEMD160.Create()) { isOk = acc.PasswordHashBase64.Equals(Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(password)))); } break; case (int)Enums.HMAC.SHA1: using (System.Security.Cryptography.SHA1 hasher = System.Security.Cryptography.SHA1.Create()) { isOk = acc.PasswordHashBase64.Equals(Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(password)))); } break; case (int)Enums.HMAC.SHA256: using (System.Security.Cryptography.SHA256 hasher = System.Security.Cryptography.SHA256.Create()) { isOk = acc.PasswordHashBase64.Equals(Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(password)))); } break; case (int)Enums.HMAC.SHA384: using (System.Security.Cryptography.SHA384 hasher = System.Security.Cryptography.SHA384.Create()) { isOk = acc.PasswordHashBase64.Equals(Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(password)))); } break; case (int)Enums.HMAC.SHA512: using (System.Security.Cryptography.SHA512 hasher = System.Security.Cryptography.SHA512.Create()) { isOk = acc.PasswordHashBase64.Equals(Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(password)))); } break; case (int)Enums.HMAC.HMACMD5: saltBytes = Convert.FromBase64String(acc.HashSaltBase64); hmac = new System.Security.Cryptography.HMACMD5(saltBytes); isOk = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(password))).Equals(acc.PasswordHashBase64); break; case (int)Enums.HMAC.HMACRIPEMD160: saltBytes = Convert.FromBase64String(acc.HashSaltBase64); hmac = new System.Security.Cryptography.HMACRIPEMD160(saltBytes); isOk = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(password))).Equals(acc.PasswordHashBase64); break; case (int)Enums.HMAC.HMACSHA1: saltBytes = Convert.FromBase64String(acc.HashSaltBase64); hmac = new System.Security.Cryptography.HMACSHA1(saltBytes); isOk = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(password))).Equals(acc.PasswordHashBase64); break; case (int)Enums.HMAC.HMACSHA256: saltBytes = Convert.FromBase64String(acc.HashSaltBase64); hmac = new System.Security.Cryptography.HMACSHA256(saltBytes); isOk = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(password))).Equals(acc.PasswordHashBase64); break; case (int)Enums.HMAC.HMACSHA384: saltBytes = Convert.FromBase64String(acc.HashSaltBase64); hmac = new System.Security.Cryptography.HMACSHA384(saltBytes); isOk = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(password))).Equals(acc.PasswordHashBase64); break; case (int)Enums.HMAC.HMACSHA512: saltBytes = Convert.FromBase64String(acc.HashSaltBase64); hmac = new System.Security.Cryptography.HMACSHA512(saltBytes); isOk = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(password))).Equals(acc.PasswordHashBase64); break; default: throw new NotImplementedException("Unspecified hash type."); } } } } catch (Exception ex) { Log4netLogger.Error(MethodBase.GetCurrentMethod().DeclaringType, "Cannot login", ex); isOk = false; } return(isOk); }