zoukankan      html  css  js  c++  java
  • password加密的算法

    加密原理:採用不同的加密算法对字符串进行加盐加密处理。

    • 用以防止密文被md5字典进行反向暴力破解。
    • 採用美国家安全局发布的加密算法(RFC 4357)加密,不採用自己创建的加密算法,以避免有安全漏洞。

      下面是基于Yii框架的实现代码。

    <?

    php /** * 密码加密算法 * 对不同类型密码採用不同的加密算法进行加密处理 * @author yagas<yagas@sina.com> * @url http://blog.csdn.net/yagas * @version 0.1 * @example: * $passwd = new TPassword( TPassword::UserPassword ); * $passwd->encode( "123456" ); * $passwd->ckechPassword( "xxxxxx", "123456" ); */ class TPassword extends CModel { /** * 密码盐长度 * @var int */ private $_satlsLen = 5; /** * 盐在密文中的偏移值 * @var int */ private $_offset = 10; /** * 加密算法名称 * @var string */ private $_passwordType; /** * 会员登录password * @var string */ const UserPassword = "sha224"; /** * 登陆员登录password * @var string */ const AdminPassword = "snefru256"; /** * 支付密码 * @var string */ const PayPassword = "haval128,3"; public function __construct( $passwordType ) { $this->_passwordType = $passwordType; } public function attributeNames() { return array(); } /** * 加密字符串 * @param string $password 须要进行加密的字符串 * @param string $satls 加密盐 * @return string 密文 */ public function encode( $password, $satls=null ) { if( is_null( $satls ) ) { $satls = ''; while( strlen( $satls ) > $this->_satlsLen ) { $i = mt_rand( 65, 90 ); $satls .= chr( $i ); } } $password = hash( $this->_passwordType, $password.$satls ); $password = md5( $password ); $newPassword = substr( $password, 0, $this->_offset ); $newPassword .= strtolower( $satls ) . substr( $password, $this->_offset ); return substr( $newPassword, 0, 32 ); } /** * 验证密码是否正确 * @param string $securtyString 密钥 * @param string $password 密码 * @return boolean */ public function checkPassword( $securtyString, $password ) { $satls = substr( $securtyString, $this->_offset, $this->_satlsLen ); $password = $this->encode( $password, strtoupper( $satls ) ); return $securtyString == $password; } }

  • 相关阅读:
    函数对象与闭包函数
    day15
    应用在tomcat下的四种部署方式(原创)
    Servlet的四种映射模式
    背景框代码
    RabbitMQ 示例-生产者-消费者-direct-topic-fanout
    idea 设置自动生成注释
    cSpring Boot整合RabbitMQ详细教程
    Windows下RabbitMQ安装及配置
    Threadlocal线程本地变量理解
  • 原文地址:https://www.cnblogs.com/yfceshi/p/7103482.html
Copyright © 2011-2022 走看看