从数字列表生成值

时间:2017-02-13 08:42:53

标签: javascript php encryption hash

我有一个简单的问题,由于我缺乏知识,我无法解决,而且我在网上找不到任何线索。

我想:

  • 从数字列表(int数组)生成“值1”。
  • 从随机数生成“值2”。
  • 然后通过比较“值2”和“值1”来检查数字列表中是否存在随机数。

为什么我想生成这些值而不是简单地对数字列表中的随机数进行数组搜索?

因为有必要隐藏数字列表,并且能够通过比较值进行快速检查。

我已经考虑过“hash”方法,但是没有办法对一个int数组进行哈希。

我还考虑过RSA密码系统,从数字列表中生成“私钥”,然后从随机数生成“公钥”。但我找不到实现它的方法。

我想知道是否有办法做我想做的事情?

感谢您的帮助。

示例:

PHP中的服务器有一个数字列表。 (例如:[10,20,24,6,98])

JS中的客户端向服务器发送一个号码。 (例如:8)

然后

// [10, 20, 24, 6, 98] to hashed or key
$computedIntArray = HashedOrRsaEncrypted($ServerIntArray);

// 8 to hashed or key
$computedClientNumber = HashedOrRsaEncrypted($ClientNumber);

// return true if the clienthashedvalue is present in the serverIntArray
$trueOrFalse = IsPresentInArray($computedClientNumber, $computedIntArray);

1 个答案:

答案 0 :(得分:0)

为了生成,例如10个非重复随机数,您可以使用哈希表。



var hash = {},
    l = 10,
    r;

while (l) {
   r = Math.floor(Math.random() * 100);
   if (!hash[r]) {
       hash[r] = true;
       l--;
   }
}

console.log(Object.keys(hash).map(Number));

.as-console-wrapper { max-height: 100% !important; top: 0; }




使用ES6,您可以使用Set并添加该值,直到该组的大小具有所需的计数。



var hash = new Set,
    l = 10;

while (hash.size < l) {
   hash.add(Math.floor(Math.random() * 100));
}

console.log([...hash]);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;