def sign(self): username = self.le_username.text() password = self.le_pwd.text() password2 = self.le_pwd_again.text() if len(username) < 8 or len(password) < 8 or len(password2) < 8: self.showmsg("length not enough") return # username = username.replace(" ", "") # password = password.replace(" ", "") # password2 = password2.replace(" ", "") if password != password2: self.showmsg("password do not match") return # 判断库里是不是有重复的 if User.select().where(User.username == username).count() != 0: self.showmsg("username already registered") return pwd_md5 = hashlib.md5(password.encode("utf-8")).hexdigest() pwd2num = passcoder.Utils.str2num(pwd_md5) # convert string to num # 生成rabin公私钥 rabin = passcoder.PKSRabin() rabin_p, rabin_q, rabin_n = rabin.keygen(512) # 使用用户的账号的密码进行保护 rabin_xor_p = rabin_p ^ pwd2num rabin_xor_q = rabin_q ^ pwd2num # 生成rsa公私钥 rsa_sign = passcoder.RsaSign() sign_p, sign_q, sign_e, sign_d = rsa_sign.k_gen(32) sign_xor_p = sign_p ^ pwd2num sign_xor_q = sign_q ^ pwd2num sign_xor_e = sign_e ^ pwd2num sign_xor_d = sign_d ^ pwd2num try: # create user User.create(username=username, pwd=pwd_md5, rabin_p=str(rabin_xor_p), rabin_q=str(rabin_xor_q), rsa_p=str(sign_xor_p), rsa_q=str(sign_xor_q), rsa_e=str(sign_xor_e), rsa_d=str(sign_xor_d)) self.le_username.setText("") self.le_pwd.setText("") self.le_pwd_again.setText("") self.showmsg("register success,close and return to login") except Exception as e: self.showmsg("register failed:{}".format(e)) return
def post(self): ''' request body will be { "username": "******", "password": "******", "email": "xxx", } ''' data = tornado.escape.json_decode(self.request.body) data['join_date'] = datetime.datetime.now() try: User.create(**data) result = True except peewee.IntegrityError: result = False # save to database, return True if successfull otherwise False self.write(dict(result=result))