您好我有一个简单的表单,允许用户输入名称,性别和密码。我使用Digest :: MD5.hexdigest来加密输入。一旦我有加密输入,例如,d1c261ede46c1c66b7e873564291ebdc,我希望能够将它附加到我已创建的文件中。然而,我尝试过的每一件事都是行不通的。任何人都可以提前帮助并感谢您。这就是我所拥有的:
input = STDIN.read( ENV["CONTENT_LENGHT"] )
puts "Content-type: text/html \n\n"
require 'digest/md5'
digest = Digest::MD5.hexdigest(input)
f = File.open("register.txt", "a")
f.write(digest)
f.close
我也试过这个没有运气:
File.open("register.txt", "a") do |f|
f.puts(digest)
end
答案 0 :(得分:1)
如果代码是逐字的,那么我认为你在第一行有一个拼写错误:你的意思是CONTENT_LENGHT
还是错字?如果设置了变量,ENV []将返回一个字符串,这将扰乱STDIN#read
。我得到TypeError: can't convert String into Integer
。假设输入错误,则ENV[]
返回nil
,告诉STDIN#read
读取直到EOF,从控制台意味着,我认为是Control-Z。这可能会导致问题。
我建议您通过修改脚本进行调查:
read_length = ENV["CONTENT_LENGTH"].to_i # assumed typo fixed, convert to integer
puts "read length = #{read_length}"
input = STDIN.read( read_length )
puts "input = #{input}"
puts "Content-type: text/html \n\n" # this seems to serve no purpose
require 'digest/md5'
digest = Digest::MD5.hexdigest(input)
puts "digest = #{digest}"
# prefer this version: it's more idiomatically "Rubyish"
File.open("register.txt", "a") do |f|
puts "file opened"
f.puts(digest)
end
file_content = File.read("register.txt")
puts "done, file content = #{file_content}"
这适用于我的机器,输出如下(当CONTENT_LENGTH设置为12时):
read length = 12
abcdefghijkl
input = abcdefghijkl
Content-type: text/html
digest = 9fc9d606912030dca86582ed62595cf7
file opened
done, file content = 6cfbc6ae37c91b4faf7310fbc2b7d5e8
e271dc47fa80ddc9e6590042ad9ed2b7
b0fb8772912c4ac0f13525409c2b224e
9fc9d606912030dca86582ed62595cf7