简体   繁体   English

如何在 Rails 后端和浏览器的 Javascript 中为字符串生成哈希值?

[英]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.

相关问题 如何计算浏览器 JavaScript 中字符串的 SHA hash - How to calculate SHA hash of a string in browser JavaScript 如何从带有字符串的javascript中的哈希中选择键? - How can I select keys from a hash in Ruby with a string of javascript? 我如何从字符串中获取哈希,然后从JavaScript中的哈希获取字符串 - How can I get hash from string and then string from hash back in javascript 如何生成在 node.js 和浏览器中都有效的 RSA 对 - How can I generate an RSA pair that works both in node.js and browser 在 Javascript 中从字符串生成哈希 - Generate a Hash from string in Javascript 如何在Chrome和Firefox中将此字符串转换为Javascript日期? - How can I convert this string to a Javascript date in both Chrome and Firefox? 在 Rails 中,如何从 JavaScript 查询我的数据库以便保存查询值? - In Rails, how do I query my database from JavaScript so I can save the value of the query? 如何输出状态的键和值? - How can I output both the key and value of my state? 如何将标题和图像文件从前端 (React.js) 传递到后端 API (Spring Boot)? - How can I pass both the title and an image file from my front end (React.js) to my backend API (Spring Boot)? 如何在Javascript中生成数字字符串和字母? - How can I generate a string of numbers and letters in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM