程序员面试经验
我正在使用RNGCryptoServiceProvider
为C#中的某些内容生成一些简单的键,但是我需要使用Javascript在客户端生成这些键。
我可以只调用服务器并获取它,但是我想避免在已经很繁重的服务器上再次发出服务器请求。我使用的代码如下;但是,我在Javascript中找不到与RNGCryptoServiceProvider
等效的东西,或类似的东西。
除了那一堂课,我几乎可以翻译这里的所有内容…真的开始困扰我…
/// <summary> /// Generate a key of a given length with specific characters. /// </summary> /// <param name="length"> /// The length of the key to generate. /// </param> /// <param name="allowedChars"> /// The characters allowed in the key. /// </param> /// <returns> /// A generated key. /// </returns> public static string Create(int length, string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") { if (length < 0) throw new ArgumentOutOfRangeException("length", "length cannot be less than zero."); if (string.IsNullOrEmpty(allowedChars)) throw new ArgumentException("allowedChars may not be empty."); const int byteSize = 0x100; var allowedCharSet = new HashSet<char>(allowedChars).ToArray(); if (byteSize < allowedCharSet.Length) throw new ArgumentException(String.Format("allowedChars may contain no more than {0} characters.", byteSize)); // Guid.NewGuid and System.Random are not particularly random. By using a // cryptographically-secure random number generator, the caller is always // protected, regardless of use. using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider()) { var result = new StringBuilder(); var buf = new byte[128]; while (result.Length < length) { rng.GetBytes(buf); for (var i = 0; i < buf.Length && result.Length < length; ++i) { // Divide the byte into allowedCharSet-sized groups. If the // random value falls into the last group and the last group is // too small to choose from the entire allowedCharSet, ignore // the value in order to avoid biasing the result. var outOfRangeStart = byteSize - (byteSize % allowedCharSet.Length); if (outOfRangeStart <= buf[i]) continue; result.Append(allowedCharSet[buf[i] % allowedCharSet.Length]); } } return result.ToString(); } }
c#参考方案
我强烈建议您进行服务器端调用,因为JavaScript是客户端语言,并且对于安全密钥而言并不安全,因为它可以查看完整的算法,并且重新设计可能会暴露您的价值。
因此,一次调用服务器端并不昂贵。
Java中的“ <<”运算符 – java
最喜欢的语句来自Java的Character类:(1 << Character.PARAGRAPH_SEPARATOR)) >> type PARAGRAPH_SEPARATOR是字节,type是整数。这句话中的操作员,他们做什么?如何以及在哪里可以使用这些运算符?这是oracles java.lang.Character文档。该类中…
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? – java
我正在使用Retrofit来获取JSON答复。这是我实施的一部分[email protected]("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…
提交表单后显示模式对话框 – php
提交下载文件后,我有一张表格。我要自动而不是自动下载文件..以显示模态对话框并显示下载链接。<form name="softwareform" id="softwareform" action="../downloadlink.php" method="POST" alig…
DataSourceTransactionManager和JndiObjectFactoryBean和JdbcTemplate的用途是什么? – java
以下的用途是什么:org.springframework.jdbc.core.JdbcTemplate org.springframework.jdbc.datasource.DataSourceTransactionManager org.springframework.jndi.JndiObjectFactoryBean <tx:annotatio…
页面加载而不是提交时发生struts验证 – java
请原谅我;我对Struts有点陌生。我遇到一个问题,即页面加载而不是我实际提交表单时发生了验证。我整天都在论坛上搜寻和搜寻,没有任何运气。我显然做错了一些事情,应该很容易确定,但是我还没有发现问题所在。这是我的struts.xml的片段:<action name="*Test" method="{1}" clas…