• 使用sqlalchemy建立携带加密字段的ORM表


    ####################first region#######################
    from werkzeug.security import generate_password_hash,check_password_hash
    pw1 = generate_password_hash('tony')
    pw2 = generate_password_hash('tony')

    print pw1
    print pw2

    print check_password_hash(pw1,'tony')
    print check_password_hash(pw2,'tony')


    ####################second region####################


    from sqlalchemy.ext.hybrid import hybrid_property
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import create_engine,Column,Integer,String,Sequence
    from sqlalchemy import and_,or_
    from sqlalchemy.orm import sessionmaker

    DB_URI = 'XXX-XXX-XXX'

    eng = create_engine(DB_URI)
    Base = declarative_base()

    class User(Base):
    __tablename__ = 'hash_users'
    id = Column(Integer,primary_key=True)
    name = Column(String(128),nullable=False)
    _password = Column(String(256),nullable=False)

    def __init__(self,name,password):
    self.name = name
    self._password = password

      #hybrid_property装饰器把password变成了一个混合属性,可以通过user.password属性来访问哈希的密码,也会在给user.password
      #赋值的时候触发password.setter

    @hybrid_property
    def password(self):
    return self._password

    @password.setter
    def _set_password(self,plaintext):
    self._password = generate_password_hash(plaintext)

    def verify_password(self,password):
    return check_password_hash(self._password,password)
  • 相关阅读:
    Spring-整合MyBatis-声明式事务
    Spring-AOP
    Spring-使用注解开发
    Spring-bean的自动装配
    Spring-HelloSpring-IOC创建对象的方式
    Objective-C学习笔记2013[NSString]字符串[可变字符串中,加用app减用delete]
    C语言I博客作业04
    C语言I博客作业03
    C语言I博客作业02
    malloc/free 和 new/delete
  • 原文地址:https://www.cnblogs.com/575dsj/p/8213663.html
Copyright © 2020-2023  润新知