2014-09-30 56 views
0

我拉下一些HTML通過登錄。反正有沒有在密碼中輸入純文本的密碼?是否有一些我可以使用的混淆技術?如何防止密碼顯示在紅寶石模板文件

理想情況下,我想要一個包含我的密碼的文件,該文件與我想共享的源代碼是分開的。加載保存在\ docs \ mypass.txt中的密碼的東西很好用。然後,我可以修改這個做一個簡單的解密我的真實密碼,所以我可以在mypass.txt保留一個混亂的版本

必須有一些簡單的方法來做一個查找和替換<<mysecretepassword>>和源文本文件。

<% register.ZServLogin.grabItems("ClimbElCap", "<<mysecretpassword>>").each do |item| %> 
+0

Presumab你可以在ruby模板文件中使用ruby。使用ruby,你可以讀取文件('File.read(「filename.txt」)'),讀取環境('ENV [「MY_ENV_VARIABLE」]'),並執行其他所有類型的魔法。 – PSkocik 2014-09-30 22:40:37

+1

謝謝PSkocik這是我選擇的答案。其他項目對於項目目前的範圍來說太複雜了。 – SwimBikeRun 2014-11-05 23:28:26

回答

2

在我看來,不被大量佔用,您應該從未儲存您的密碼在任何文件中明文。雖然你可以混淆你的密碼,但在有鎖的地方總是有一把鑰匙,鑰匙可以複製。我想說的是密碼可以解密。相反,嘗試將您的密碼存儲爲散列!我會使用模塊ruby提供的名爲Digest,但是ruby確實有一些內置的散列方法。 (但我會讓你探索該地區)

例如時間!讓我們假設你想要用戶提供一個密碼,並且你想把這個密碼存儲在一個文本文件中供以後使用。您還希望能夠驗證用戶輸入的密碼是否正確。讓我們開始吧:

#first you need to require the module 
require 'digest' 

#then you need to get the password from the user 
input = gets.chomp 

#now the magic begins, using the digest module we are going to turn the password into a has 
password = Digest::SHA1.hexdigest(input) 

#and you can store it where ever and how ever you would like. (If you are worried about corrupting your file you may want to look into PStore. A great class for persistence) 
write = File.open("password.txt",'w') do |file| 
    file.write(password) 
end 

#Lets say the program ends there but now we want to have the user login 
puts "Login!" 
print "Username: " 
user = gets.chomp 
print "Password: " 
pass = gets.chomp 

#Now in order for him to login we need to compare his password with the one stored in the file 
read = File.read("password.txt") 

pass = Digest::SHA1.hexdigest(pass) 

puts pass == read ? "Passwords match : "Please try again" 

顯然有很多事情需要完成,以便在您的情況下工作。但我只是想給你選擇,你可能會或可能不想考慮。謝謝

快樂編碼!

1

我認爲這是一個完美的例子,您想要使用Rails 4.1中引入的config/secrets.yml(請參閱:http://edgeguides.rubyonrails.org/4_1_release_notes.html#config-secrets-yml)。或者像費加羅一樣的寶石(見:https://github.com/laserlemon/figaro)。

一言以蔽之:添加您的密鑰到config/secrets.yml

development: 
    foo_api_key: 'a-dummy-development-key' 
production: 
    foo_api_key: 'super-secret-production-key' 

你不應該從你的ENV將此文件添加到您的版本控制研究的系統,除非你加載生產關鍵是這樣的:

production: 
    foo_api_key: <%= ENV['super-secret-production-key'] %> 

在你的代碼可以使用鍵像這樣:

...grabItems("ClimbElCap", Rails.application.secrets.foo_api_key)