Exemple #1
0
        /// <summary>
        /// Create password information from password
        /// </summary>
        /// <param name="password">The password</param>
        /// <param name="slat">Slat, use a random value if gived null</param>
        /// <param name="type">Password type</param>
        public static PasswordInfo FromPassword(string password,
                                                byte[] slat = null, PasswordHashType type = PasswordHashType.PBKDF2)
        {
            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException("password can't be empty");
            }
            var info = new PasswordInfo()
            {
                Type = type
            };
            var passwordBytes = Encoding.UTF8.GetBytes(password);

            if (type == PasswordHashType.PBKDF2)
            {
                slat      = slat ?? RandomUtils.SystemRandomBytes(8);
                info.Slat = Convert.ToBase64String(slat);
                info.Hash = Convert.ToBase64String(PasswordUtils.PBKDF2Sum(passwordBytes, slat));
            }
            else if (type == PasswordHashType.Md5)
            {
                info.Hash = Convert.ToBase64String(PasswordUtils.Md5Sum(passwordBytes));
            }
            else if (type == PasswordHashType.Sha1)
            {
                info.Hash = Convert.ToBase64String(PasswordUtils.Sha1Sum(passwordBytes));
            }
            return(info);
        }
Exemple #2
0
		/// <summary>
		/// 从密码创建密码信息
		/// </summary>
		/// <param name="password">密码</param>
		/// <param name="slat">参与校验的随机数据,不指定时使用默认值</param>
		/// <param name="type">密码类型</param>
		public static PasswordInfo FromPassword(string password,
			byte[] slat = null, PasswordHashType type = PasswordHashType.PBKDF2) {
			if (string.IsNullOrEmpty(password)) {
				throw new ArgumentNullException("password can't be empty");
			}
			var info = new PasswordInfo() { Type = type };
			var passwordBytes = Encoding.UTF8.GetBytes(password);
			if (type == PasswordHashType.PBKDF2) {
				slat = slat ?? RandomUtils.SystemRandomBytes(8);
				info.Slat = Convert.ToBase64String(slat);
				info.Hash = Convert.ToBase64String(PasswordUtils.PBKDF2Sum(passwordBytes, slat));
			} else if (type == PasswordHashType.Md5) {
				info.Hash = Convert.ToBase64String(PasswordUtils.Md5Sum(passwordBytes));
			} else if (type == PasswordHashType.Sha1) {
				info.Hash = Convert.ToBase64String(PasswordUtils.Sha1Sum(passwordBytes));
			}
			return info;
		}