zend-framework - 使用 Zend_Mail_Transport_Smtp 和一个 MD5 哈希值作为密码

标签 zend-framework zend-mail webmail smtp-auth

我想为我的网络应用程序的用户提供使用我们的 smtp 服务器发送电子邮件的可能性。

用户帐户的密码是 md5-hased 并且 smtp-server 正在散列接收到的值以检查正确的用户名密码组合。

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

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

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

最佳答案

您可以做的是将密码以编码格式存储在数据库中,并在需要时在应用程序中对其进行解码。不幸的是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() 或类似的东西,因为很容易被看到密码的人猜到。
  • 无需 key 即可解码/编码:

    另一种方法是使用函数对密码进行解码/编码,该函数不需要像 Base64 这样的 key 。 :
    // store this in the database
    $pw_base64 = base64_encode( "plain_password" );
    // use this in the application
    $pw_plain = base64_encode( "cGxhaW5fcGFzc3dvcmQ=" );
    

    比上面的好一点,但我只会将它用于测试目的,因为它很容易实现和使用。
  • 使用 key 解码/编码:

    更好的方法是使用 key 和对称块密码,如 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, 0, 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, '', MCRYPT_MODE_ECB, '' );
        $key     = substr( self::KEY, 0, 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 ) {
          $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 哈希值作为密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11502876/

    相关文章:

    php - Zend pdo_oci 和 pdo_mysql 生成不同的查询格式

    javascript - Ajax 调用不会触发 Zend 2 EVENT_DISPATCH 事件

    asp.net - 想要构建一个在线网络邮件脚本

    iis - 处理同一个域下的多个应用程序

    php - Zend_Db_Profiler 记录到文件

    php - 使用 Zend_Mail 时添加 PDF 附件

    php - 将 Zend Framework 最小化为 Zend_Mail?

    PHP、sendmail 和传输 - 如何加速邮件发送

    php - 如何在php中从服务器webmail获取未读消息

    Apache 将除 webmail 之外的所有内容都转发给 Tomcat