Cara menggunakan php generate random hash

6aruSzs0qJ47190bba9f6361764d423317d202402d55 7190bba9f6361764d423317d202402d567190bba9f6361764d423317d202402d57 7190bba9f6361764d423317d202402d587190bba9f6361764d423317d202402d57 7898decff6889ba2521bb32259e571be9880da250$n6aruSzs0qJ87190bba9f6361764d423317d202402d577898decff6889ba2521bb32259e571be9880da254

7898decff6889ba2521bb32259e571be9880da2557898decff6889ba2521bb32259e571be9880da256 7898decff6889ba2521bb32259e571be9880da2577898decff6889ba2521bb32259e571be9880da2587190bba9f6361764d423317d202402d566aruSzs0qJ5690a4fea15d64168b512ad893f5e44bf13741a5c954f70fb6553e508965ad6f51

7898decff6889ba2521bb32259e571be9880da2557190bba9f6361764d423317d202402d50 690a4fea15d64168b512ad893f5e44bf13741a5c954f70fb6553e508965ad6f546aruSzs0qJ5690a4fea15d64168b512ad893f5e44bf13741a5c954f70fb6553e508965ad6f567898decff6889ba2521bb32259e571be9880da256690a4fea15d64168b512ad893f5e44bf13741a5c954f70fb6553e508965ad6f58

6aruSzs0qJ46317481fbacc10

 

6aruSzs0qJ46317481fbacc12 7190bba9f6361764d423317d202402d506aruSzs0qJ8

6317481fbacc10

 

6317481fbacc16 6aruSzs0qJ1$n6317481fbacc19

67de19022831ea109df3ab3b483a2c0912005fdc0

Output6aruSzs0qJ

APPROACH 2: Using Hashing Functions 

PHP has a few functions like md5(), sha1() and hash(), that can be used to hash a string based on certain algorithms like “sha1”, “sha256”, “md5” etc. All these function takes a string as an argument and output an Alpha-Numeric hashed string. 

To learn more about these functions click here. Once we understand how we utilize these functions, our task becomes pretty simple.

  • Generate a random number using rand() function.
  • Hash it using one of the above functions.

Program 1: 

php




<?php

67de19022831ea109df3ab3b483a2c0912005fdc267de19022831ea109df3ab3b483a2c0912005fdc3

67de19022831ea109df3ab3b483a2c0912005fdc4 67de19022831ea109df3ab3b483a2c0912005fdc567de19022831ea109df3ab3b483a2c0912005fdc26317481fbacc19

6317481fbacc16 67de19022831ea109df3ab3b483a2c0912005fdc46aruSzs0qJ8

67de19022831ea109df3ab3b483a2c0912005fdc0

Output7190bba9f6361764d423317d202402d5

Program 2: 

php




<?php

67de19022831ea109df3ab3b483a2c0912005fdc267de19022831ea109df3ab3b483a2c0912005fdc3

67de19022831ea109df3ab3b483a2c0912005fdc4 <?php667de19022831ea109df3ab3b483a2c0912005fdc26317481fbacc19

6317481fbacc16 67de19022831ea109df3ab3b483a2c0912005fdc46aruSzs0qJ8

67de19022831ea109df3ab3b483a2c0912005fdc0

Output7898decff6889ba2521bb32259e571be9880da25

Program 3: 

php




<?php

67de19022831ea109df3ab3b483a2c0912005fdc2 $n5

67de19022831ea109df3ab3b483a2c0912005fdc4 $n7$n8$n967de19022831ea109df3ab3b483a2c0912005fdc26317481fbacc19

6317481fbacc16 67de19022831ea109df3ab3b483a2c0912005fdc46aruSzs0qJ8

67de19022831ea109df3ab3b483a2c0912005fdc0

Output690a4fea15d64168b512ad893f5e44bf13741a5c954f70fb6553e508965ad6f5

NOTE: All the above functions are hashing functions, hence the length of the string generated will always depend on the algorithm used, but for an algorithm it will always remain constant. So if you want to generate string of a fixed length, you can either truncate the generated string or concatenate with another string, based on the requirement. 

Approach 3: Using uniqid() function. 

The uniqid( ) function in PHP is an inbuilt function which is used to generate a unique ID based on the current time in microseconds (micro time). By default, it returns a 13 character long unique string. Program: 

php




<?php

67de19022831ea109df3ab3b483a2c0912005fdc4 =10;8

6317481fbacc16 67de19022831ea109df3ab3b483a2c0912005fdc46aruSzs0qJ8

67de19022831ea109df3ab3b483a2c0912005fdc0

Output6317481fbacc1

NOTE: All the above approaches are built on rand() and uniqid() functions. These functions are not cryptographically secure random generators. So it is advised that if the degree of randomness affect the security of an application, these methods should be avoided. 

Approach 4: Using random_bytes() function. (Cryptographically Secure) 

The random_bytes() function generates cryptographically secure pseudo-random bytes, which can later be converted to hexadecimal format using bin2hex() function. 

Program: 

php




<?php

$n 6aruSzs0qJ05

67de19022831ea109df3ab3b483a2c0912005fdc4 6aruSzs0qJ07$n6aruSzs0qJ09

6317481fbacc16 67de19022831ea109df3ab3b483a2c0912005fdc46aruSzs0qJ8

67de19022831ea109df3ab3b483a2c0912005fdc0

Output67de19022831ea109df3ab3b483a2c0912005fdc

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

Postingan terbaru

LIHAT SEMUA