/// <summary>Version 1</summary> /// <param name="password">password</param> /// <returns>hashPassword</returns> private string V1HashAlgorithm(string password) { // $1$ バージョンの実装 return("$1$" + "." + GetKeyedHash.GetSaltedPassword( password, // password EnumKeyedHashAlgorithm.MACTripleDES, // algorithm GetPassword.Generate(10, 3), // key(pwd) 10, // salt length ASPNETIdentityConfig.StretchCount // stretch count )); }
/// <summary>Version 1</summary> /// <param name="hashedPassword">hashedPassword</param> /// <param name="providedPassword">providedPassword</param> /// <returns>検証結果</returns> private PasswordVerificationResult V1VerifyHashAlgorithm( string hashedPassword, string providedPassword) { if (GetKeyedHash.EqualSaltedPassword( providedPassword, hashedPassword.Substring(4), EnumKeyedHashAlgorithm.MACTripleDES)) { return(PasswordVerificationResult.Success); } else { return(PasswordVerificationResult.Failed); } }
/// <summary>KeyedHash</summary> private static void KeyedHash() { string data = "ynyKeiR9FXWPkNQHvUPZkAlfUmouExBv"; string key = "ynyKeiR9FXWPkNQHvUPZkAlfUmouExBv"; MyDebug.OutputDebugAndConsole("KeyedHashAlgorithm.Default", GetKeyedHash.GetKeyedHashString(data, EnumKeyedHashAlgorithm.Default, key)); MyDebug.OutputDebugAndConsole("KeyedHashAlgorithm.HMACMD5", GetKeyedHash.GetKeyedHashString(data, EnumKeyedHashAlgorithm.HMACMD5, key)); MyDebug.OutputDebugAndConsole("KeyedHashAlgorithm.HMACRIPEMD160", GetKeyedHash.GetKeyedHashString(data, EnumKeyedHashAlgorithm.HMACRIPEMD160, key)); MyDebug.OutputDebugAndConsole("KeyedHashAlgorithm.HMACSHA1", GetKeyedHash.GetKeyedHashString(data, EnumKeyedHashAlgorithm.HMACSHA1, key)); MyDebug.OutputDebugAndConsole("KeyedHashAlgorithm.HMACSHA256", GetKeyedHash.GetKeyedHashString(data, EnumKeyedHashAlgorithm.HMACSHA256, key)); MyDebug.OutputDebugAndConsole("KeyedHashAlgorithm.HMACSHA384", GetKeyedHash.GetKeyedHashString(data, EnumKeyedHashAlgorithm.HMACSHA384, key)); MyDebug.OutputDebugAndConsole("KeyedHashAlgorithm.HMACSHA512", GetKeyedHash.GetKeyedHashString(data, EnumKeyedHashAlgorithm.HMACSHA512, key)); MyDebug.OutputDebugAndConsole("KeyedHashAlgorithm.MACTripleDES", GetKeyedHash.GetKeyedHashString(data, EnumKeyedHashAlgorithm.MACTripleDES, key)); }
private void btnSPWDAuth2_Click(object sender, EventArgs e) { // パラメタ系は渡さないで検証可能 if (GetKeyedHash.EqualSaltedPassword( this.txtSPWDRawPassword2.Text, this.txtSPWDSaltedPassword2.Text, (EnumKeyedHashAlgorithm)this.cbxSPWDPV2.SelectedValue)) { MessageBox.Show("認証成功"); } else { MessageBox.Show("認証失敗"); } }
/// <summary>バイト配列のハッシュ値を計算して返す。</summary> /// <param name="asb">バイト配列</param> /// <param name="ekha">ハッシュ(キー付き)アルゴリズム列挙型</param> /// <param name="password">使用するパスワード</param> /// <param name="salt">ソルト</param> /// <param name="stretchCount">ストレッチ回数</param> /// <returns>ハッシュ値(バイト配列)</returns> private static byte[] GetKeyedHashBytes(byte[] asb, EnumKeyedHashAlgorithm ekha, string password, byte[] salt, int stretchCount) { // キーを生成する。 Rfc2898DeriveBytes passwordKey = new Rfc2898DeriveBytes(password, salt, stretchCount); // HMACMD5 :どのサイズのキーでも受け入れる ◯ // HMACRIPEMD160:どのサイズのキーでも受け入れる ◯ // HMACSHA1 :どのサイズのキーでも受け入れる ◯ // HMACSHA256 :どのサイズのキーでも受け入れる ◯ // HMACSHA384 :どのサイズのキーでも受け入れる ◯ // HMACSHA512 :どのサイズのキーでも受け入れる ◯ // MACTripleDES :長さが 16 または 24 バイトのキーを受け入れる // ハッシュ(キー付き)サービスプロバイダを生成 #if NETSTD // NETSTDの場合の実装 if (ekha == EnumKeyedHashAlgorithm.HMACRIPEMD160) { return(GetKeyedHash.GetMacBytesByBC( asb, passwordKey.GetBytes(24), new HMac(new RipeMD160Digest()))); } else if (ekha == EnumKeyedHashAlgorithm.MACTripleDES) { return(GetKeyedHash.GetMacBytesByBC( asb, passwordKey.GetBytes(24), new CbcBlockCipherMac(new DesEdeEngine(), 64))); } #endif KeyedHashAlgorithm kha = HashAlgorithmCmnFunc.CreateKeyedHashAlgorithmSP( ekha, passwordKey.GetBytes(24) // 上記より、24 決め打ちとする。 ); // ハッシュ(キー付き)を生成して返す。 byte[] temp = kha.ComputeHash(asb); kha.Clear(); // devps(1725) return(temp); }
public void GetKeyedHashBytesTest(byte[] asb, EnumKeyedHashAlgorithm eha, string password) { try { // anyWarp 棟梁の部品を使用してハッシュ値を取得 byte[] hashBytes = GetKeyedHash.GetKeyedHashBytes(asb, eha, password); // anyWarp 棟梁の部品を使用して、もう一度ハッシュ値を取得 byte[] hashBytes2 = GetKeyedHash.GetKeyedHashBytes(asb, eha, password); // ハッシュ値が同じかどうかをチェック Assert.AreNotEqual(asb, hashBytes); Assert.AreNotEqual(asb, hashBytes2); Assert.AreEqual(hashBytes, hashBytes2); } catch (Exception ex) { // Print a stack trace when an exception occurs. Console.WriteLine(ex.StackTrace); throw; } }
[TestCase(null, 999, "test@123", ExpectedException = typeof(ArgumentNullException), TestName = "TestID-018A")] // The encryption method that is not defined public void GetKeyedHashStringTest(string sourceString, EnumKeyedHashAlgorithm eha, string password) { try { // Get the hash value using the components of touryo. string hashString = GetKeyedHash.GetKeyedHashString(sourceString, eha, password); // Using the components of touryo, and get the hash value again. string hashString2 = GetKeyedHash.GetKeyedHashString(sourceString, eha, password); // Check the hash value. Assert.AreNotEqual(sourceString, hashString); Assert.AreNotEqual(sourceString, hashString2); Assert.AreEqual(hashString, hashString2); } catch (Exception ex) { // Print a stack trace when an exception occurs. Console.WriteLine(":" + ex.StackTrace); throw; } }
/// <summary>キー付きハッシュ</summary> private void btnGetKeyedHash_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtKHSSalt.Text)) { // ソルト無し if (this.rbnKHSString.Checked) { // String this.txtKHSCode.Text = GetKeyedHash.GetKeyedHashString( this.txtKHSString.Text, (EnumKeyedHashAlgorithm)cbxKHSPV.SelectedValue, this.txtKHSPassword.Text); } else { // Bytes this.txtKHSCode.Text = CustomEncode.ToHexString( GetKeyedHash.GetKeyedHashBytes( CustomEncode.StringToByte(txtKHSString.Text, CustomEncode.UTF_8), (EnumKeyedHashAlgorithm)cbxKHSPV.SelectedValue, this.txtKHSPassword.Text)); } } else { // ソルト有り if (this.nudKHSStretching.Value == 0) { // ストレッチング無し if (this.rbnKHSString.Checked) { // String this.txtKHSCode.Text = GetKeyedHash.GetKeyedHashString( this.txtKHSString.Text, (EnumKeyedHashAlgorithm)cbxKHSPV.SelectedValue, this.txtKHSPassword.Text, CustomEncode.StringToByte(this.txtKHSPassword.Text, CustomEncode.UTF_8)); } else { // Bytes this.txtKHSCode.Text = CustomEncode.ToHexString( GetKeyedHash.GetKeyedHashBytes( CustomEncode.StringToByte(txtKHSString.Text, CustomEncode.UTF_8), (EnumKeyedHashAlgorithm)cbxKHSPV.SelectedValue, this.txtKHSPassword.Text, CustomEncode.StringToByte(this.txtKHSPassword.Text, CustomEncode.UTF_8))); } } else { // ストレッチング有り if (this.rbnKHSString.Checked) { // String this.txtKHSCode.Text = GetKeyedHash.GetKeyedHashString( this.txtKHSString.Text, (EnumKeyedHashAlgorithm)cbxKHSPV.SelectedValue, this.txtKHSPassword.Text, CustomEncode.StringToByte(this.txtKHSPassword.Text, CustomEncode.UTF_8), (int)nudKHSStretching.Value); } else { // Bytes this.txtKHSCode.Text = CustomEncode.ToHexString( GetKeyedHash.GetKeyedHashBytes( CustomEncode.StringToByte(txtKHSString.Text, CustomEncode.UTF_8), (EnumKeyedHashAlgorithm)cbxKHSPV.SelectedValue, this.txtKHSPassword.Text, CustomEncode.StringToByte(this.txtKHSPassword.Text, CustomEncode.UTF_8), (int)nudKHSStretching.Value)); } } } }
private void btnSPWDGen2_Click(object sender, EventArgs e) { this.txtSPWDSaltedPassword2.Text = GetKeyedHash.GetSaltedPassword( this.txtSPWDRawPassword2.Text, (EnumKeyedHashAlgorithm)this.cbxSPWDPV2.SelectedValue, this.txtSPWDKey2.Text, (int)this.nudSPWDSaltLength2.Value, (int)this.nudSPWDStretchCount2.Value); }