【问题标题】:Laravel make unique 16 character hashLaravel 制作唯一的 16 字符哈希
【发布时间】:2018-04-24 01:54:48
【问题描述】:

我想创建一个长度为 16 个字符的完全唯一的哈希,以便为某些私人博客帖子创建公共链接。因此可以在以下位置访问它们:

https://website.com/read/ASDFGHJ9827DYCO9

在 Laravel 中有一个辅助函数

\Hash::make($request->password);

它会产生这样的哈希:

$2y$10$kouytAixb/Yp5gpH7QqL6et/P.fW.qElsGkMncVM.1/29waEqeqjy

问题是我想做一个只使用字母和数字,长度为 16 个字符且绝对唯一的哈希。

【问题讨论】:

    标签: php laravel hash


    【解决方案1】:

    如果它不需要安全(即您不在乎是否有人可以将其反转以获得原始值),只需 md5() 即可。这将返回一个分布合理的 16 个字符的十六进制字符串。

    md5($model->id);
    

    【讨论】:

      【解决方案2】:

      John Elmore 的回答是正确的,另外,散列一个数字会使值容易受到彩虹表攻击,所以我会在实际 id 之前添加一些“盐”,这很难猜到,这样彩虹表就没用了,理想情况下,长度为 20 或更多:

      md5(salt . $model->id)
      

      HTH

      【讨论】:

        【解决方案3】:

        这里有两个选项。不完全是 16 个字符。

        1. 使用 php hash 函数生成随机数。 echo hash('ripemd128', 'some random input'); // echo's hex represented 32byte of data
        2. 加密一些内容,例如页面的id。使用对称加密,key 将成为该数据的解锁。唯一性已经不重要了,怎么可能不......

        在 php 文档中解释,http://php.net/manual/en/function.openssl-encrypt.php

        //$key should have been previously generated in a cryptographically safe way, like openssl_random_pseudo_bytes
        $plaintext = "message to be encrypted";
        $cipher = "aes-128-gcm";
        if (in_array($cipher, openssl_get_cipher_methods()))
        {
            $ivlen = openssl_cipher_iv_length($cipher);
            $iv = openssl_random_pseudo_bytes($ivlen);
            $ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv, $tag);
            //store $cipher, $iv, and $tag for decryption later
            $original_plaintext = openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv, $tag);
            echo $original_plaintext."\n";
        }
        

        您可以使用此方法查找密码列表,http://php.net/manual/en/function.openssl-get-cipher-methods.php

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-22
          • 2023-03-16
          • 2011-02-15
          • 1970-01-01
          • 2020-05-29
          • 1970-01-01
          • 1970-01-01
          • 2021-01-11
          相关资源
          最近更新 更多