【发布时间】:2015-11-18 18:25:26
【问题描述】:
我有一个包含用户名和密码的 YAML 文件。
YAML 概述:
users: test: password: test test2: password: test2
我想使用Digest::MD5 将密码值加密为 MD5 哈希,例如:
user:
Lost Bam:
password: testtesttest #<=I want to overwrite this password with a MD5 hash
在 Digest 中是否有加密哈希值的方法?如果是这样,我该如何将它实现到 YAML 文件中?
md5.rb 来源:
require 'yaml'
require 'digest'
private
def load_file
File.exist?('info.yml') ? YAML.load_file('info.yml') : {users: {}}
end
def read_file
File.read('info.yml')
end
def save_file( hash )
File.open('info.yml', 'w') { |f| f.write(hash.to_yaml)}
end
def add_user
hash = load_file
hash["users"][prompt('Enter username:')] =
{ "password" => prompt('Enter password:') }
puts "Encrypt information?"
information = gets.chomp
case input
when /yes/i
# hash = Digest::MD5.digest(["password"]'value')<-Doesn't work
#
#This is where I want to be able to encrypt the
#value of the password key that was entered by the user
#
# save_file( hash )
else
puts "Add another?"#Not completed yet
end
save_file( hash )
end
main.rb 来源:
require_relative 'md5.rb'
def main
puts <<-END.gsub(/^\s*>/, '')
>
>To load information type "L" to quit system type "Q"
>
END
input = gets.chomp.upcase
case input
when "L"
add_user
when "Q"
exit_system
else
exit_lock
end
end
def exit_system
puts "Exiting..."
exit
end
def exit_lock
puts "Locked out, please contact system administrator"
exit
end
def restart
puts "Encrypt more?"
input = gets.chomp
if input =~ /yes/i
return true
else
exit_system
end
end
def prompt( message )
puts message
gets.chomp
end
main
【问题讨论】:
-
您的意思是在从 YAML 文件中读取后消化这些值吗?
-
@Edward 是的,我编辑了问题
-
“某些东西”是什么意思,为什么在 YAML 中难以实现?请提供一些代码来演示您正在尝试做什么,或者显示您搜索的位置以及为什么答案不能帮助您编写代码。因为不清楚你做了什么。请阅读“How to Ask”。
-
@theTinMan 抱歉,我添加了两个文件的源代码