[英]How can I generate a hash value for a string on both my Rails backend and in Javascript on the browser?
I'm building a web game in Ruby on Rails and I want most of the game logic to take place on the browser.我正在用 Ruby on Rails 构建一个网页游戏,我希望大部分游戏逻辑都发生在浏览器上。 The game involves a user answering a question.
该游戏涉及用户回答问题。 I need to embed the answers in the web page since that's where I'm putting the code that verifies the user answered the question correctly.
我需要在网页中嵌入答案,因为这是我放置验证用户正确回答问题的代码的地方。 I don't want put the answers in plain text since savvy users can view the source to see the answers, so I figured I'd encrypt them using a hash before I embed them in the page.
我不想将答案以纯文本形式显示,因为精明的用户可以查看源代码以查看答案,因此我想在将它们嵌入页面之前使用散列对其进行加密。
To do this, I need to first create the hash value for the answer string on my Rails backend, and then when the user answers the question, I need to create the hash value for their answer and compare that hash value with the embedded hash value.为此,我需要首先在 Rails 后端为答案字符串创建哈希值,然后当用户回答问题时,我需要为他们的答案创建哈希值并将该哈希值与嵌入的哈希值进行比较. If they're equal, then they answered the question correctly and I notify my server.
如果他们相等,那么他们正确回答了问题,我会通知我的服务器。
How can I generate a hash value for a string on both my Rails backend and in Javascript on the browser?如何在 Rails 后端和浏览器的 Javascript 中为字符串生成哈希值?
Would you suggest a better way to hide the answers on my webpage?你会建议一种更好的方法来隐藏我网页上的答案吗?
Given the purpose MD5 should suffix, from a client side point of view you can use the phpjs.org version of MD5 from here鉴于目的 MD5 应该后缀,从客户端的角度来看,您可以从这里使用 MD5 的 phpjs.org 版本
http://phpjs.org/functions/md5/ http://phpjs.org/functions/md5/
If MD5 is not good enough, you can look into other hashing possibilities in Google Closure Library如果 MD5 不够好,您可以在 Google Closure Library 中查看其他散列可能性
http://closure-library.googlecode.com/svn/docs/namespace_goog_crypt.html http://closure-library.googlecode.com/svn/docs/namespace_goog_crypt.html
I never worked with ruby but the MD5 counterpart appears to be this我从来没有用过 ruby,但 MD5 对应的似乎是这个
http://ruby-doc.org/stdlib-1.8.6/libdoc/digest/rdoc/MD5.htmlhttp://ruby-doc.org/stdlib-1.8.6/libdoc/digest/rdoc/MD5.html
With the ready availability of sites like the one lostsource posted, to strengthen the hash, you should add multiply the timestamp to it.随着像 one lostsource 发布的站点的现成可用性,为了加强散列,您应该向其添加乘法时间戳。 You then have the timestamp in your data and can call the phpjs md5 hashcode with "#{timestamp}#{hashcode}".
然后,您的数据中就有时间戳,并且可以使用“#{timestamp}#{hashcode}”调用 phpjs md5 哈希码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.