コード例 #1
0
        /// <summary>
        /// Password entered by the userをDB保存する際、
        /// Salted and hashed passwordとして保存する必要がある。
        /// </summary>
        /// <param name="rawPassword">Password entered by the user.</param>
        /// <param name="ekha">ハッシュ・アルゴリズム列挙型</param>
        /// <param name="key">キー</param>
        /// <param name="saltLength">ソルトの文字列長</param>
        /// <param name="stretchCount">ストレッチ回数</param>
        /// <returns>Salted and hashed password.</returns>
        public static string GetSaltedPassword(string rawPassword,
                                               EnumKeyedHashAlgorithm ekha, string key, int saltLength, int stretchCount)
        {
            // ランダム・ソルト文字列を生成(区切り記号は含まなくても良い)
            string salt = GetPassword.Generate(saltLength, 0); //Membership.GeneratePassword(saltLength, 0);

            byte[] saltByte = CustomEncode.StringToByte(salt, CustomEncode.UTF_8);

            // KeyedHashのキーを生成する。
            Rfc2898DeriveBytes passwordKey = new Rfc2898DeriveBytes(key, saltByte, stretchCount);

            // Salted and hashed password(文字列)を生成して返す。
            return

                // key
                (CustomEncode.ToBase64String(CustomEncode.StringToByte(key, CustomEncode.UTF_8))

                 // saltByte
                 + "." + CustomEncode.ToBase64String(saltByte)

                 // stretchCount
                 + "." + CustomEncode.ToBase64String(CustomEncode.StringToByte(stretchCount.ToString(), CustomEncode.UTF_8))

                 // Salted and hashed password
                 + "." + CustomEncode.ToBase64String(
                     GetPasswordHashV2.GetKeyedHashBytes(
                         CustomEncode.StringToByte(salt + rawPassword, CustomEncode.UTF_8),
                         ekha, passwordKey.GetBytes(24))));
        }
コード例 #2
0
        /// <summary>
        /// Password entered by the userをDB保存する際、
        /// Salted and hashed passwordとして保存する必要がある。
        /// </summary>
        /// <param name="rawPassword">>Password entered by the user.</param>
        /// <param name="eha">ハッシュ・アルゴリズム列挙型</param>
        /// <param name="saltLength">ソルトの文字列長</param>
        /// <param name="stretchCount">ストレッチ回数</param>
        /// <returns>Salted and hashed password.</returns>
        public static string GetSaltedPassword(string rawPassword, EnumHashAlgorithm eha, int saltLength, int stretchCount)
        {
            // ランダム・ソルト文字列を生成(区切り記号は含まなくても良い)
            string salt = GetPassword.Generate(saltLength, 0); //Membership.GeneratePassword(saltLength, 0);

            // Salted and hashed password(文字列)を生成して返す。
            return
                (CustomEncode.ToBase64String(CustomEncode.StringToByte(salt, CustomEncode.UTF_8))
                 + "." + CustomEncode.ToBase64String(CustomEncode.StringToByte(stretchCount.ToString(), CustomEncode.UTF_8))
                 + "." + CustomEncode.ToBase64String(CustomEncode.StringToByte(GetHash.GetHashString(salt + rawPassword, eha, stretchCount), CustomEncode.UTF_8)));
        }
コード例 #3
0
 /// <summary>Base64UrlSecretを生成</summary>
 /// <param name="byteSize">サイズ(バイト)</param>
 /// <returns>Base64UrlSecret</returns>
 public static string Base64UrlSecret(int byteSize)
 {
     return(CustomEncode.ToBase64UrlString(GetPassword.RandomByte(byteSize)));
 }