Exemplo n.º 1
0
        public void TestDecoding0()
        {
            //01110010 01000100 10001000 00011001 11101100
            var s = Base32StringCoding.Decode(new byte[] { 0x72, 0x44, 0x88, 0x19, 0xEC });

            Assert.AreEqual("e928g6fc", s);
        }
Exemplo n.º 2
0
        public void TestDecoding4()
        {
            //10011111 10100111 00111000 01100101 01110010 01000100 10001000 00011001 11101100
            var s = Base32StringCoding.Decode(new byte[] { 0x9f, 0xa7, 0x38, 0x65, 0x72, 0x44, 0x88, 0x19, 0xEC });

            Assert.AreEqual("2fqee35e928g6fc", s);
        }
Exemplo n.º 3
0
        public static string GetHashedPassword(string password)
        {
            var bytes = Hash.ComputeHash(Encoding.UTF8.GetBytes(password));
            var ret   = Base32StringCoding.Decode(bytes);

            return(ret);
        }
Exemplo n.º 4
0
 protected static string GenIndexName(string n, int maxLength)
 {
     if (string.IsNullOrEmpty(n))
     {
         var bytes = Util.NewGuid().ToByteArray();
         var s     = Base32StringCoding.Decode(bytes);
         return(s);
     }
     return(GenIdentityName(n, maxLength));
 }
Exemplo n.º 5
0
        public static string SerializeToString(string sessionId)
        {
            if (sessionId == null)
            {
                return(null);
            }
            var bs = Encoding.UTF8.GetBytes(sessionId);

            return(Base32StringCoding.Decode(bs));
        }
Exemplo n.º 6
0
        public static User DeserializeFromString(string source)
        {
            if (source == null)
            {
                return(null);
            }
            var bs = Base32StringCoding.Encode(source);
            var s  = Encoding.UTF8.GetString(bs);

            return(GetUserForLogin(s));
        }
Exemplo n.º 7
0
 protected static string GenIdentityName(string n, int maxLength)
 {
     if (string.IsNullOrEmpty(n))
     {
         throw new DataException("Identity name could not be null or empty");
     }
     if (n.Length > maxLength)
     {
         var bytes = StringHelper.HashMd5(n);
         var s     = Base32StringCoding.Decode(bytes);
         return(s);
     }
     return(null);
 }
Exemplo n.º 8
0
        private void RegError(string msg, DateTime now)
        {
            Flash.Warning = msg;
            var t  = now.Ticks.ToString();
            var x  = t.Substring(0, BlogSettings.RegTimeXbits);
            var t2 = int.Parse(t.Substring(BlogSettings.RegTimeXbits));
            var z  = Rand.Next(BlogSettings.RegTimeRandMax);
            var y  = t2 - z;

            this["RegTimeX"]    = x;
            this["RegTimeY"]    = y.ToString();
            this["RegTimeZ"]    = z.ToString();
            this["RegValidate"] = Base32StringCoding.Decode(StringHelper.HashMd5(BlogSettings.RegSolt + t));
        }
Exemplo n.º 9
0
    public static string ToBase32String(this Guid guid)
    {
        var bs = guid.ToByteArray();

        return(Base32StringCoding.Decode(bs));
    }
Exemplo n.º 10
0
        public void TestEncoding0()
        {
            var bs = Base32StringCoding.Encode("e928g6fc");

            Assert.AreEqual(new byte[] { 0x72, 0x44, 0x88, 0x19, 0xEC }, bs);
        }
Exemplo n.º 11
0
        public void TestEncoding4()
        {
            var bs = Base32StringCoding.Encode("2fqee35e928g6fc");

            Assert.AreEqual(new byte[] { 0x9f, 0xa7, 0x38, 0x65, 0x72, 0x44, 0x88, 0x19, 0xEC }, bs);
        }
Exemplo n.º 12
0
        public string Register()
        {
            var now = DateTime.Now;

            if (!BlogSettings.AllowRegister)
            {
                RegError("暂时不允许注册", now);
                return(null);
            }
            var  email    = Bind("email");
            var  password = Bind("password");
            var  showname = Bind("showname");
            var  time     = Bind("time");
            var  validate = Bind("validate");
            long ticks;

            if (email.LikeNull() || password.LikeNull() || showname.LikeNull() || email.IndexOf("@") < 0 ||
                !long.TryParse(time, out ticks) || validate.LikeNull())
            {
                RegError("Email密码以及显示名都是必填项", now);
                return(null);
            }
            var regTime = new DateTime(ticks);

            if (Math.Abs((now - regTime).TotalSeconds) > BlogSettings.RegTimeDiffSecs)
            {
                RegError("注册超时,请刷新后重新注册", now);
                return(null);
            }
            var realValidate = Base32StringCoding.Decode(StringHelper.HashMd5(BlogSettings.RegSolt + time));

            if (validate != realValidate)
            {
                Logger.Default.Trace("validate: " + validate + " - realValidate: " + realValidate + " - time: " + time);
                RegError("注册错误,请联系管理员", now);
                return(null);
            }
            if (password.Length >= 100)
            {
                return(null);
            }
            var u = new User
            {
                Email     = email,
                Password  = password,
                ShowName  = showname,
                Role      = UserRole.NonactivatedUser,
                SessionId = Guid.NewGuid().ToString()
            };
            var v = u.Validate();

            if (v.IsValid)
            {
                u.Save();
                var uv = new UserValidate().Init(u.Id);
                uv.Mode = 'a';
                uv.Save();

                SendMail(u, uv);

                Flash.Notice = "用户创建成功";
                return(UrlTo <UserController>(p => p.Login()));
            }
            var sb = new StringBuilder();

            foreach (var message in v.ErrorMessages)
            {
                sb.Append(message);
            }
            RegError(sb.ToString(), now);
            return(null);
        }