Пример #1
0
        /// <summary>
        /// Gets the unique ID for the test case.
        /// </summary>
        protected virtual string GetUniqueID()
        {
            using (var stream = new MemoryStream())
            {
                Write(stream, TestMethod.TestClass.TestCollection.TestAssembly.Assembly.Name);
                Write(stream, TestMethod.TestClass.Class.Name);
                Write(stream, TestMethod.Method.Name);

                if (TestMethodArguments != null)
                {
                    Write(stream, SerializationHelper.Serialize(TestMethodArguments));
                }

                stream.Position = 0;

#if WINDOWS_PHONE_APP
                var buffer = CryptographicBuffer.CreateFromByteArray(stream.ToArray());
                var hash   = Hasher.HashData(buffer).ToArray();
#elif DOTNETCORE
                var hash = new byte[20];
                var data = stream.ToArray();
                Hasher.BlockUpdate(data, 0, data.Length);
                Hasher.DoFinal(hash, 0);
#else
                var hash = Hasher.ComputeHash(stream);
#endif
                return(BytesToHexString(hash));
            }
        }
Пример #2
0
        /// <summary>
        /// Gets the unique ID for the test case.
        /// </summary>
        protected virtual string GetUniqueID()
        {
            using (var stream = new MemoryStream())
            {
                Write(stream, TestMethod.TestClass.TestCollection.TestAssembly.Assembly.Name);
                Write(stream, TestMethod.TestClass.Class.Name);
                Write(stream, TestMethod.Method.Name);

                if (TestMethodArguments != null)
                {
                    Write(stream, SerializationHelper.Serialize(TestMethodArguments));
                }

                stream.Position = 0;

#if WINDOWS_PHONE_APP
                var buffer = CryptographicBuffer.CreateFromByteArray(stream.ToArray());
                var hash   = Hasher.HashData(buffer).ToArray();
#elif DNXCORE50
                byte[] hash;
                using (var hasher = SHA1.Create())
                    hash = hasher.ComputeHash(stream);
#else
                var hash = Hasher.ComputeHash(stream);
#endif

                return(String.Join("", hash.Select(x => x.ToString("x2")).ToArray()));
            }
        }
Пример #3
0
 public ActionResult GetQrcode(string data)
 {
     if (!string.IsNullOrEmpty(data))
     {
         string hash = HashAlgorithmProvider.ComputeHash("MD5", data, true);
         string key  = $"qrcode:{hash}";
         if (!CacheProvider.TryGet(key, out byte[] buffer))
Пример #4
0
        static Guid GuidFromString(string data)
        {
            var hash = Hasher.ComputeHash(Encoding.Unicode.GetBytes(data));
            var b    = new byte[16];

            Array.Copy((Array)hash, (Array)b, 16);
            return(new Guid(b));
        }
Пример #5
0
        internal static string ComputeHash(string text, string algorithm)
        {
            string hash = string.Empty;

            using (HashAlgorithmProvider hashProvider = new HashAlgorithmProvider(algorithm))
            {
                hash = hashProvider.ComputeHash(text);
            }
            return(hash);
        }
Пример #6
0
        public static string Encode(string key, int uid, int expires)
        {
            byte[] keys = HashAlgorithmProvider.ComputeHash("MD5", key);

            var payload = new JwePayload
            {
                iat = DateTimeUtil.Timestamp,
                exp = DateTimeUtil.GetTimestamp(DateTime.Now.AddMinutes(expires)),
                uid = uid,
            };

            return(JWT.Encode(payload, keys, Algorithm, Encryption));
        }
Пример #7
0
        public async Task <IHttpActionResult> LoginAsync(User json)
        {
            if (json == null)
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            string vcode = "";

            if (!CacheProvider.TryGet("vcode." + json.sid, out vcode) || vcode != json.vcode)
            {
                throw new BadRequestException(ResultCode.ArgumentException, "验证码错误");
            }
            if (string.IsNullOrEmpty(json.username))
            {
                throw new BadRequestException(ResultCode.ArgumentException, "登录用户名不能为空");
            }
            if (string.IsNullOrEmpty(json.password))
            {
                throw new BadRequestException(ResultCode.ArgumentException, "登录密码不能为空");
            }

            string password = HashAlgorithmProvider.ComputeHash("MD5", json.password, true);
            var    user     = await this.m_UserStorage.GetAsync(json.username);

            if (user == null || user.password != password)
            {
                throw new BadRequestException(ResultCode.ArgumentException, "用户名或密码错误");
            }
            if (user.status != 1)
            {
                throw new BadRequestException(ResultCode.ArgumentException, "用户已禁用,请联系管理员");
            }

            //remove validate code
            CacheProvider.Remove("vcode." + json.sid);

            var data = new
            {
                user.username,
                user.nickname,
                access_token = JweProvider.Encode(JwtCommon.SignKey, user.id, JwtCommon.ExpireInMinutes),
                expires      = DateTimeUtil.GetTimestamp(DateTime.Now.AddHours(2))
            };

            return(Json(JsonApiResult.Ok(data)));
        }
Пример #8
0
        public async Task <IHttpActionResult> UpdatePasswordAsync(User json)
        {
            if (json == null)
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            if (string.IsNullOrEmpty(json.password))
            {
                throw new BadRequestException(ResultCode.ArgumentException, "旧密码不能为空");
            }
            if (string.IsNullOrEmpty(json.newpassword))
            {
                throw new BadRequestException(ResultCode.ArgumentException, "新密码不能为空");
            }
            if (json.newpassword.Length > 18 || json.newpassword.Length < 5)
            {
                throw new BadRequestException(ResultCode.ArgumentException, "新密码长度为5~18位");
            }

            string oldpassword = HashAlgorithmProvider.ComputeHash("MD5", json.password, true);
            var    user        = Request.GetProperty <User>(HttpPropertyKeys.AuthorizedUser);

            if (user.password != oldpassword)
            {
                throw new BadRequestException(ResultCode.ArgumentException, "旧密码不正确");
            }

            string newpassword = HashAlgorithmProvider.ComputeHash("MD5", json.newpassword, true);

            user.password = newpassword;
            int count = await this.m_UserStorage.UpdateAsync(user);

            if (count <= 0)
            {
                throw new BadRequestException(ResultCode.ActionFail, "更新密码失败");
            }

            return(Json(JsonApiResult.Ok("")));
        }
Пример #9
0
        /// <summary>
        /// 解析Token并返回payload部分信息
        /// </summary>
        /// <param name="token"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static JwePayload Decode(string token, string key)
        {
            byte[] keys = HashAlgorithmProvider.ComputeHash("MD5", key);

            return(JWT.Decode <JwePayload>(token, keys));
        }