2012-07-16 65 views
4

我想为用户提供我的web应用程序使用我们的smtp服务器发送电子邮件的可能性。使用Zend_Mail_Transport_Smtp和MD5哈希值作为密码

用户帐户的密码为md5-hased,并且smtp服务器将接收到的值散列化以检查正确的用户名密码kobination。

现在我正在寻找一种设置Zend_Mail_Transport_Smtp的好方法 - 我显然需要明文密码并将其转发给smtp服务器,然后将其转换为md5哈希。 但是这意味着我必须将用户密码存储在纯文本中,这是我想避免的。

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

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

+0

你的问题解决了吗? – Jalpesh 2012-07-16 11:39:25

+0

不,它不是...... – jantoenjes 2012-07-16 14:06:57

回答

3

你可以做的是将密码以编码格式存储在数据库中,并在需要时在应用程序中对其进行解码。不幸的是MD5只是一个哈希函数,你不能解码为简单的密码。我知道三种方式来实现:

  1. 替换字母:

    您可以使用类似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()或类似的东西,因为很容易被看到密码的人猜到。

  2. 解码/编码,没有一个密钥:

    另一种方式是解码/用功能,它不需要像Base64关键编码的密码:

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

    一个更好一点然后上面,但我会使用它仅用于测试目的,因为它很容易实现和使用。

  3. 解码/编码用钥匙:

    一个更好的办法是使用密钥和对称分组密码一样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"); 
        } 
    

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

而最重要的是:切勿存储普通密码。