TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Predicting the next Math.random() in Java

150 点作者 nilknarf超过 10 年前

9 条评论

imaginenore超过 10 年前
If you want cryptographic-quality random numbers, both Java and Javascript have them. Math.random() is simply a super-fast decent RNG.<p>Example:<p><pre><code> var buf = new Uint32Array(10); window.crypto.getRandomValues(buf); console.log(buf); </code></pre> Outputs things like:<p><pre><code> [4027145128, 258543382, 1205615760, 2665675208, 4033127244, 2280027866, 3983484449, 510932333, 1911490534, 2609399642] </code></pre> This works in Chrome and FF.<p>IE11 has Crypto.getRandomValues(...)<p>Java has SecureRandom:<p><a href="http://docs.oracle.com/javase/6/docs/api/java/security/SecureRandom.html" rel="nofollow">http:&#x2F;&#x2F;docs.oracle.com&#x2F;javase&#x2F;6&#x2F;docs&#x2F;api&#x2F;java&#x2F;security&#x2F;Secur...</a>
评论 #8254296 未加载
评论 #8254092 未加载
mnw21cam超过 10 年前
That is a nice not-so-subtle reminder. When a PRNG says it is insecure, <i>it is insecure</i>. When a PRNG says it is secure, it <i>might</i> be - get someone very clever to check it first.
phpnode超过 10 年前
nitpick, Firefox doesn&#x27;t use Rhino, it uses SpiderMonkey which is C++.<p><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Mozilla&#x2F;Projects&#x2F;Sp...</a>
drinchev超过 10 年前
How dangerous this prediction can be? I can&#x27;t stop thinking of java-backended real money, poorly written, gaming websites.
评论 #8254172 未加载
评论 #8253951 未加载
评论 #8254162 未加载
评论 #8253952 未加载
评论 #8253985 未加载
xxs超过 10 年前
Math.random() should be used only for tests. That&#x27;s it. Performance sucks as it&#x27;s shared. ThreadLocalRandom is a lot better if you need fast but not-quality random.<p>And there is SecureRandom for security concerns.<p>Last fun fact Math.random() and a Monte Carlo test introduced &quot;CAS in Java&quot; and all that followed with JSR 166.
mda超过 10 年前
Reminded me an interesting Java Random issue with small seeds and power of two intervals:<p><pre><code> for(int i = 0; i &lt; 256; i++) { System.out.println(new Random(i).nextInt(8)); } </code></pre> It returns same number for all seeds.
lunixbochs超过 10 年前
I tested a similar attack against ApacheCommons&#x27; RandomStringUtil. Given a few bytes of output, I could recover the RNG state in 20 minutes on CPU.
jlebar超过 10 年前
As another commenter has said, Firefox doesn&#x27;t use Rhino. Here&#x27;s the relevant code in Firefox&#x27;s JS engine.<p><a href="http://dxr.mozilla.org/mozilla-central/source/js/src/jsmath.cpp#765" rel="nofollow">http:&#x2F;&#x2F;dxr.mozilla.org&#x2F;mozilla-central&#x2F;source&#x2F;js&#x2F;src&#x2F;jsmath....</a>
Peksa超过 10 年前
Hah, funny! I recently did the same to circumvent CSRF-protection based on java.util.Random. Here&#x27;s my solver in JS: <a href="https://peks.as/experiments/random/" rel="nofollow">https:&#x2F;&#x2F;peks.as&#x2F;experiments&#x2F;random&#x2F;</a>