加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

zend-framework – 使用Zend_Mail_Transport_Smtp和MD5-Hashed值

发布时间:2020-12-13 16:46:06 所属栏目:PHP教程 来源:网络整理
导读:我想向用户提供我的网络应用程序使用我们的smtp服务器发送电子邮件的可能性. 用户帐户的密码是md5-hased,smtp-server正在散列收到的值以检查正确的用户名 – 密码kobination. 现在我正在寻找一种设置Zend_Mail_Transport_Smtp的好方法 – 我显然需要明文密码
我想向用户提供我的网络应用程序使用我们的smtp服务器发送电子邮件的可能性.

用户帐户的密码是md5-hased,smtp-server正在散列收到的值以检查正确的用户名 – 密码kobination.

现在我正在寻找一种设置Zend_Mail_Transport_Smtp的好方法 – 我显然需要明文密码并将其转发到smtp-server,然后将其转换为md5-hash.
但这意味着我必须将用户密码以明文存储在某处,我想避免使用.

有关如何使用zend框架设置webmailer的最佳实践吗?

我唯一的想法是在会话中保存未散列的密码(我的应用程序中的用户帐户与邮件服务器帐户链接),但必须有更好的方法来处理这种情况

解决方法

您可以做的是将密码以编码格式存储在数据库中,并在需要时在应用程序中对其进行解码.不幸的是 MD5只是一个散列函数,你无法解码为普通密码.我知道有三种方法可以做到这一点:

>替补信件:

您可以使用类似ROT13的内容替换普通密码中的字母:

// store this in the database
$pw_rot = str_rot13( "plain_password" );
// use this in the application
$pw_plain = str_rot13( "cynva_cnffjbeq" );

我不建议使用str_rot13()或类似的东西,因为很容易被看到密码的人猜到.
>无键解码/编码:

另一种方法是使用函数解码/编码密码,该函数不需要像Base64这样的密钥:

// store this in the database
$pw_base64 = base64_encode( "plain_password" );
// use this in the application
$pw_plain = base64_encode( "cGxhaW5fcGFzc3dvcmQ=" );

比上面的更好一些,但我会将其用于测试目的,因为它易于实现和使用.
>使用密钥解码/编码:

更好的方法是使用密钥和对称分组密码,如Blowfish:

class Password {
  const KEY = 'your_secret_key_for_the_cipher';

  // encode the plain text with key for storing in the database
  public function encode( $plain_text ) {
    // set up the environment
    $td      = mcrypt_module_open( MCRYPT_BLOWFISH,'',MCRYPT_MODE_ECB,'' );
    $key     = substr( self::KEY,mcrypt_enc_get_key_size( $td ) );
    $iv_size = mcrypt_enc_get_iv_size( $td );
    $iv      = mcrypt_create_iv( $iv_size,MCRYPT_RAND );

    if( mcrypt_generic_init( $td,$key,$iv ) != -1 ) {
      $cipher_text = mcrypt_generic( $td,$plain_text );
      // clean up the mcrypt enviroment
      mcrypt_generic_deinit( $td );
      mcrypt_module_close( $td );
    }

    // use hex value            
    return bin2hex( $cipher_text );
  }

  // decode the stored cipher text with key to use in the application
  public function decode( $cipher_text ) {
    // set up the environment
    $td      = mcrypt_module_open( MCRYPT_BLOWFISH,$iv ) != -1 ) {
      $plain_text = mdecrypt_generic( $td,pack( "H*",$cipher_text ) );
      // clean up the mcrypt environment
      mcrypt_generic_deinit( $td );
      mcrypt_module_close( $td );
    }

    // remove NUL which maybe added by padding the plain_text
    return rtrim( $plain_text,"" );
  }

通过这种方式,只有有权访问数据库和源代码的人才能解密密码.在不利方面,您有一个更复杂的应用程序和一点性能影响.您还可以使用其他对称分组密码.

最重要的是:永远不要存储普通密码.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读