【问题标题】:How to prevent password from displaying in ruby template file如何防止密码在 ruby​​ 模板文件中显示
【发布时间】:2014-09-30 22:04:12
【问题描述】:

我正在通过登录下载一些 html。无论如何,我的密码不是在代码中以纯文本形式输入的吗?我可以使用一些混淆技术吗?

理想情况下,我想要一个包含我的密码的文件,该文件与我想要共享的源代码分开。加载保存在 \docs\mypass.txt 中的密码的东西会很好用。然后我可以修改它来简单地解读我的真实密码,这样我就可以在 mypass.txt 中保留一个加扰的版本

必须有一些简单的方法来查找和替换 <<mysecretepassword>> 并从文本文件中获取它。

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

【问题讨论】:

  • 大概你可以在一个ruby模板文件中使用ruby。使用 ruby​​,您可以读取文件 (File.read("filename.txt"))、读取环境 (ENV["MY_ENV_VARIABLE"]) 并执行所有其他类型的魔法。
  • 感谢 PSkocik 这是我选择的答案。其他的对于项目当前的范围来说太复杂了。

标签: ruby-on-rails ruby embedded-ruby


【解决方案1】:

在我看来,不要过于重视,您应该永远将您的密码作为纯文本存储在任何文件中。虽然您可以混淆密码,但有锁的地方总有一把钥匙,而且钥匙可以复制。我想说的是密码可以被解读。相反,请尝试将您的密码存储为哈希!我会使用 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"

显然,要在您的情况下工作需要做很多工作。但我只是想给你一些你可能想或不想考虑的选择。谢谢和

编码愉快!

【讨论】:

    【解决方案2】:

    我认为这是一个完美的示例,您可以在其中使用 Rails 4.1 中引入的 config/secrets.yml(参见:http://edgeguides.rubyonrails.org/4_1_release_notes.html#config-secrets-yml)。或者类似 Figaro 的 gem(参见: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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      • 1970-01-01
      • 2021-03-02
      • 2012-12-31
      相关资源
      最近更新 更多