Laravel制作独特的16个字符哈希

问题描述:

I want to make a completely unique hash that is 16 characters long to make a public link for certain private blog posts. So they can be accessed at:

https://website.com/read/ASDFGHJ9827DYCO9

In Laravel there is a helper function that goes

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

It makes a hash like this:

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

The trouble is that I want to make a hash that only uses letters and numbers and is 16 characters long and is absolutely unique.

我想创建一个16字符长的完全唯一的哈希,以便为某些私人博客帖子建立公共链接。 因此可以通过以下方式访问它们: p>

https:/ /website.com/read/ASDFGHJ9827DYCO9 p> blockquote>

在Laravel中有一个辅助函数 p>

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

它产生这样的哈希: p>

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

麻烦的是,我想制作一个只有哈希值 使用字母和数字,长度为16个字符,绝对是唯一的。 p> div>

John Elmore's answer is correct, additionally, Hashing a number makes the value vulnerable to rainbow table attacks, so I would add some "salt" before the actual id, that is hard to guess, so that the rainbow tables are useless, ideally, of length 20 or more:

md5(salt . $model->id)

HTH

If it doesn't need to be secure (i.e. you don't care if someone can reverse it to get the original value), just md5() it. That'll return a 16-character hexadecimal string which is reasonably-well distributed.

md5($model->id);

Here's two option. Not exactly 16 characters.

  1. Generate random number with php hash function. echo hash('ripemd128', 'some random input'); // echo's hex represented 32byte of data
  2. Encrypt some content, For example id of the page. With symmetric encryption a key will be the unlock for that data. Uniqueness is not important anymore, How can it not be....

Explained in php doc, 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."
";
}

You can look for list of ciphers with this method, http://php.net/manual/en/function.openssl-get-cipher-methods.php