zend-framework – 使用Zend_Mail_Transport_Smtp和MD5-Hashed值作为密码

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了zend-framework – 使用Zend_Mail_Transport_Smtp和MD5-Hashed值作为密码脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我想向用户提供我的网络应用程序使用我们的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,"\0" );
  }

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

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

脚本宝典总结

以上是脚本宝典为你收集整理的zend-framework – 使用Zend_Mail_Transport_Smtp和MD5-Hashed值作为密码全部内容,希望文章能够帮你解决zend-framework – 使用Zend_Mail_Transport_Smtp和MD5-Hashed值作为密码所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。