2009-11-17 60 views
1

我已經將舊的joomla安裝遷移到了django。雖然密碼哈希值是一個問題。我不得不修改contrib.auth.models中的get_hexdigest以使用額外的if語句來顛倒生成散列的方式。將代碼添加到contrib.auth的最簡潔方法

# Custom for Joomla 
if algorithm == 'joomla': 
    return md5_constructor(raw_password + salt).hexdigest() 
# Djangos original md5 
if algorithm == 'md5': 
    return md5_constructor(salt + raw_password).hexdigest() 

我還添加以下到用戶模式登錄後更新的密碼,如果他們有舊的Joomla風格:

# Joomla Backwards compat 
algo, salt, hsh = self.password.split('$') 
if algo == 'joomla': 
    is_correct = (hsh == get_hexdigest(algo, salt, raw_password)) 
    if is_correct: 
     # Convert the password to the new more secure format. 
     self.set_password(raw_password) 
     self.save() 
    return is_correct 

一切工作完美,但我寧願不修改這個代碼直接在django樹中。在我自己的項目中是否有更簡單的方法來實現這一點?

感謝

+1

備案:就安全性而言,最好先用鹽(見:http://programming.arantius.com/how-to-salt-your-hash) – Jiaaro 2009-11-17 13:22:14

+0

感謝您的支持。我沒有意識到這一點。但在這種情況下,我不能改變它。我正在處理來自Joomla的遺留數據,並以另一種方式進行處理。 – Clarence 2009-11-18 03:36:34

回答

6

你最好的選擇是推出一個自定義的身份驗證後端,並在那裏重寫get_hexdigest。從來沒有做過,但有關如何這樣做的文檔可在http://docs.djangoproject.com/en/dev/topics/auth/#authentication-backends

+1

+1:我花了幾天的時間在我的場景中正確地做到了這一點,但在你的情況下,它應該很容易做,而且是最「Djangoic」的路要走。 – Boldewyn 2009-11-17 09:08:15

+0

謝謝。我知道自定義auth後端,但因爲我在models.py中找到了這個東西,我認爲它沒有應用。在您的鏈接之後,我重新閱讀了所有內容,這正是我需要的。 Circle獲得廣場! – Clarence 2009-11-18 03:37:43

0

感謝您的指導。對於需要以DJ密碼進入其他方式(DJangoJoomla)的DJ,DJ格式爲Sha1$salt$crypt

Joomla標準身份驗證插件和Joomla核心JUserHelper不實現相同的SHA1 algorithum但它是很容易修補成joomla.php在該插件,這裏的插件不正常的上':'爆炸。做一個由'$'salt = [1]三個部分爆炸,與$encrypted = sha1($salt.$plaintext)比較,匹配crypt [2]

相關問題