public async Task <AuthenticationTicket> GetRootAuthenticateTicketAsync(string username, string password) { var user = await _dbContext.Set <CcUiAuthen>().AsNoTracking().FirstOrDefaultAsync(p => p.Login == username); if (user == null) { return(null); } IHash hash = HashFactory.Crypto.CreateWhirlpool(); HashResult r = hash.ComputeString(password, Encoding.ASCII); string passwordEncoded = Regex.Replace(r.ToString(), "-", string.Empty).ToLower(); if (user.PwdEncoded != passwordEncoded) { return(null); } var identity = new ClaimsIdentity(); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Userid.ToString())); identity.AddClaim(new Claim(ClaimTypes.Role, "Administrator")); var principal = new ClaimsPrincipal(identity); var prop = new AuthenticationProperties() { IssuedUtc = DateTime.UtcNow, ExpiresUtc = DateTime.UtcNow.AddDays(1) }; return(new AuthenticationTicket(principal, prop, "application")); }
public static string GetChecksum(string file) { IHash crc64 = HashFactory.Checksum.CreateCRC64_ECMA(); HashResult r = crc64.ComputeFile(file); return(r.ToString()); }
/// <summary> /// 加密 /// </summary> /// <param name="sourceStr"></param> /// <returns></returns> public virtual string Encryption(string sourceStr) { if (string.IsNullOrWhiteSpace(sourceStr)) { throw new NothingToEncrypOrDecryptException(); } HashResult result = currentHash.ComputeString(sourceStr, Encode); string resultStr = result.ToString(); return(ResultFilter(resultStr)); }
private void okButton_Click(object sender, EventArgs e) { if (masterEntryTextBox.Text.Trim().Equals("")) { MessageBox.Show("Please enter the password.", "Missing Password"); } else { SQLiteDatabase database = new SQLiteDatabase(); string storedHash = database.SelectHash(); if (Properties.Settings.Default.UseSHA3Hashing) { IHash hash = HashFactory.Crypto.SHA3.CreateKeccak512(); HashResult hashResult = hash.ComputeString(masterEntryTextBox.Text); if (hashResult.ToString().Equals(storedHash)) { // Put user's password in a secure string for later use foreach (char c in masterEntryTextBox.Text) { SharedObject.encryptedPassword.AppendChar(c); } SharedObject.passwordGood = true; this.Close(); } else { MessageBox.Show("Password is not valid. Please try again.", "Invalid Password"); } } else { if (PasswordHash.ValidatePassword(masterEntryTextBox.Text, storedHash)) { // Put user's password in a secure string for later use foreach (char c in masterEntryTextBox.Text) { SharedObject.encryptedPassword.AppendChar(c); } SharedObject.passwordGood = true; this.Close(); } else { MessageBox.Show("Password is not valid. Please try again.", "Invalid Password"); } } } }
private void okButton_Click(object sender, EventArgs e) { if (masterEntryTextBox.Text.Trim().Equals("") || masterConfirmTextBox.Text.Trim().Equals("")) { MessageBox.Show("Password fields cannot be blank.", "Missing Passwords"); } else { if (masterEntryTextBox.Text.Equals(masterConfirmTextBox.Text)) { // Put user's password in a secure string for later use foreach (char c in masterEntryTextBox.Text) { SharedObject.encryptedPassword.AppendChar(c); } string hashString; if (Properties.Settings.Default.UseSHA3Hashing) { IHash hash = HashFactory.Crypto.SHA3.CreateKeccak512(); HashResult hashResult = hash.ComputeString(masterEntryTextBox.Text); hashString = hashResult.ToString(); } else { hashString = PasswordHash.CreateHash(masterEntryTextBox.Text); } // Generate a random salt RNGCryptoServiceProvider saltGenerator = new RNGCryptoServiceProvider(); byte[] saltBytes = new byte[24]; saltGenerator.GetBytes(saltBytes); string salt = Convert.ToBase64String(saltBytes); saltGenerator.Dispose(); SQLiteDatabase database = new SQLiteDatabase(); database.CreateDatabase(); database.InsertHash(hashString, salt); SharedObject.passwordGood = true; this.Close(); } else { MessageBox.Show("Passwords do not match.", "Mismatched Passwords"); } } }
/// <summary> /// /// </summary> /// <param name="args"></param> static void Main(string[] args) { string strSource = "lichaoqiang"; string strMd5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "md5"); var md5 = System.Security.Cryptography.MD5.Create(); byte[] encrypt = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strSource)); StringBuilder sBuilder = new StringBuilder(); // Loop through each byte of the hashed data // and format each one as a hexadecimal string. for (int i = 0; i < encrypt.Length; i++) { sBuilder.Append(encrypt[i].ToString("x2")); } string strMd52 = sBuilder.ToString(); //斯内夫鲁 IHash hash = HashLib.HashFactory.Crypto.CreateSnefru_8_128(); HashResult hashResult = hash.ComputeString("www.lichaoqiang.com"); Console.WriteLine(hashResult.ToString()); //hash32 string strHash32 = HashLib.HashFactory.Hash32.CreateRS().ComputeString("www.lichaoqianag.com").ToString(); Console.WriteLine("CreateRS:{0}", strHash32); strHash32 = HashLib.HashFactory.Hash32.CreatePJW().ComputeString("www.lichaoqianag.com").ToString(); Console.WriteLine("CreatePJW:{0}", strHash32); // string strHmac = HashLib.HashFactory.HMAC.CreateHMAC(hash).ComputeString("www.lichaoqiang.com").ToString(); Console.WriteLine("CreateHMAC:{0}", strHmac); Console.ReadLine(); }
/// <summary> /// Computes Hashes /// </summary> /// <param name="input">Input String</param> /// <returns></returns> protected virtual String Hash(String input) { HashResult r = this._crypto.ComputeString(input, Encoding.UTF8); return(r.ToString().Replace("-", "").ToLower()); }
private void okButton_Click(object sender, EventArgs e) { if (masterEntryTextBox.Text.Trim().Equals("") || masterConfirmTextBox.Text.Trim().Equals("")) { MessageBox.Show("Password fields cannot be blank.", "Missing Password"); } else { if (masterEntryTextBox.Text.Equals(masterConfirmTextBox.Text)) { database = new SQLiteDatabase(); credentials = database.SelectAllSecureCredentials(); // Decrypt all the usernames and passwords using the old key for (int i = 0; i < credentials.Count; i++) { credentials[i].username = CredentialEncryption.DecryptCredential(credentials[i].username); credentials[i].password = CredentialEncryption.DecryptCredential(credentials[i].password); } string hashString; if (Properties.Settings.Default.UseSHA3Hashing) { IHash hash = HashFactory.Crypto.SHA3.CreateKeccak512(); HashResult hashResult = hash.ComputeString(masterEntryTextBox.Text); hashString = hashResult.ToString(); } else { hashString = PasswordHash.CreateHash(masterEntryTextBox.Text); } database.UpdateHash(hashString); // Update salt? // Put user's password in a secure string for later use SharedObject.encryptedPassword.Clear(); foreach (char c in masterEntryTextBox.Text) { SharedObject.encryptedPassword.AppendChar(c); } // Re-encrypt all the usernames and password using the new key CredentialEncryption.DeriveKey(); for (int i = 0; i < credentials.Count; i++) { credentials[i].username = CredentialEncryption.EncryptCredential(credentials[i].username); credentials[i].password = CredentialEncryption.EncryptCredential(credentials[i].password); database.UpdateSecureCredential(credentials[i], credentials[i].id); } // SharedObject.passwordGood = true; this.Close(); } else { MessageBox.Show("Passwords do not match.", "Mismatched Passwords"); } } }