Hash functions are algorithms that transform input data into a fixed-size string of characters, commonly referred to as a hash value or digest. These functions are essential in computer science and cryptography for purposes such as ensuring data integrity, securely storing passwords, and creating digital signatures. Hash functions are characterized by their speed, determinism (the same input always yields the same hash output), and the property that even a slight change in input results in a significantly different hash, facilitating the detection of data alterations.

References

  • “Hash Function,” Wolfram MathWorld

Demo

To hash a text, enter the text in the Input text textbox, specify the shift, and click Encrypt. To decrypt a message, enter the ciphertext in the Ciphertext textbox, specify the shift, and click Decrypt. Note that, in this implementation, strings are converted to upper case before encryption/decryption, and spaces and punctuation marks are not encrypted/decrypted.

             

PHP Implementation of This Demo

To implement the simple demo shown on this page on your own website, follow these simple steps. First, paste the following PHP script at the beginning of your page, after the <body> tag.
<?php
  // Check if a request was submitted
  if ( isset( $_POST[ 'generate' ] ) ) {
    $text = $_POST[ 'text' ];
    $algorithm = $_POST[ 'algorithm' ];

    // Validate the selected algorithm
    $allowed_algorithms = [ 'MD5' => 'md5', 'SHA-1' => 'sha1', 'SHA-256' => 'sha256', 'SHA-512' => 'sha512' ];
    if ( !in_array( $algorithm, array_keys( $allowed_algorithms ) ) ) {
      $html = '<p>Invalid algorithm selected!</p>';
    } else {
      // Generate the hash
      $hash = hash( $allowed_algorithms[ $algorithm ], $text );
      $html = '<p><strong>' . $algorithm . ' Hash</strong>: ' . $hash . '</p>';
    }
  }  
?>
Next, place the following code where you would like the form to be shown.
<form method="post" action="">
  <label for="text"><strong>Input Text</strong></label>
  <input type="text" name="text" id="text" required value="<?= (isset($text))?$text:'' ?>">
  <label for="algorithm"><strong>Select Hash Algorithm</strong></label>
  <select name="algorithm" id="algorithm">
          <option value="MD5"<?= (isset($algorithm))?(($algorithm=='MD5')?'selected':''):'' ?>>MD5</option>
          <option value="SHA-1"<?= (isset($algorithm))?(($algorithm=='SHA-1')?'selected':''):'' ?>>SHA-1</option>
          <option value="SHA-256"<?= (isset($algorithm))?(($algorithm=='SHA-256')?'selected':''):'' ?>>SHA-256</option>
          <option value="SHA-512"<?= (isset($algorithm))?(($algorithm=='SHA-512')?'selected':''):'' ?>>SHA-512</option>
        </select>
  <input type="submit" name="generate" value="Generate Hash">
</form>
<?php
if ( isset( $html ) ) {
  echo $html;
}
?>