Introduction

Hash functions have a wide range of applications in computer science and security:

  1. Data Integrity: Hashes are used to verify the integrity of data during transmission or storage. By comparing the hash of received data with a previously generated hash, you can check if the data has been tampered with. Example: package-lock.json
  2. Password Storage: Hash functions are used to securely store passwords in databases. Instead of storing plaintext passwords, systems store the hash of the password. When a user logs in, the system hashes the entered password and compares it to the stored hash. Example: All password authentication
  3. Data Deduplication: Hashes can be used to identify duplicate data and eliminate redundant copies, saving storage space.
  4. Cryptographic Signatures: Hash functions are a crucial component of digital signatures, where they are used to create a unique fingerprint of a document or message to ensure its authenticity and integrity.
  5. Cryptographic Applications: Hash functions play a critical role in various cryptographic protocols, such as HMAC (Hash-based Message Authentication Code) and digital certificates.

Common hash functions include MD5, SHA-1, and SHA-256, although MD5 and SHA-1 are now considered weak for cryptographic purposes due to vulnerabilities. Security-sensitive applications typically use stronger hash functions like SHA-256 or SHA-3 to provide better protection against attacks.


Designing a simple Hash function

As per definition, if any function takes in a string and returns a code unique to that string , we can call it a hash function

  1. We define the basicHash function, which takes an inputString as its argument.
  2. Inside the function, we initialize the hashCode variable to zero, which will be used to accumulate the hash value.
  3. We iterate through each character in the inputString.
  4. For each character, we get its Unicode (ASCII) value, and then we add this value to the hashCode.
  5. Finally, we return the hashCode as the hash result.

As with the Python example, please keep in mind that this is a very basic hash function for educational purposes. It's not suitable for secure or cryptographic applications. Real-world hash functions are far more complex and designed with specific security and efficiency considerations in mind.

<aside> 💡 But this is very easy to decrypt for practical purposes.

</aside>

function basicHash(inputString) {
  let hashCode = 0;

  for (let i = 0; i < inputString.length; i++) {
    // Convert the character to 
		//its Unicode (ASCII) value
    let charValue = inputString.charCodeAt(i);
    
    // Add the Unicode value to the accumulator
    hashCode += charValue;
  }

  // Return the hash code
  return hashCode;
}

//________________________________
// Test the basic hash function

let hashResult = basicHash(inputString);
console.log("Hash Code:", hashResult);

//Output
//Hash Code: 1104

A slightly advanced hash function

Driver code simpleHash(inputString)

  1. Takes in an inputString
  2. Generates salt string : an independent string using generateSalt() function
  3. Generates a hash string using customHash(inputString)
  4. Adds hash + salt and generates hash 1000 times
  5. returns hash + salt

function generateSalt()

We generate a random 32-character salt using a custom generateSalt function.

<aside> 💡 Please note that this salt generation method is not cryptographically secure. it's for demonstration purposes only.

</aside>

function customHash(input)

A simple custom hash function (not recommended for security-critical use)

function simpleHash(inputString) {
  // Generate a random 16-byte salt (not cryptographically secure)
  const salt = generateSalt();

  // Create a hash using a simple custom hash function
  let hash = customHash(inputString);

  // Perform multiple iterations of hashing
  for (let i = 0; i < 1000; i++) {
    hash = customHash(hash + salt);
  }

  // Return the salt (as a hexadecimal string) and the final hash
  return salt + hash;
}
// Function to generate a simple random salt (not cryptographically secure)
function generateSalt() {
  const characters = '0123456789ABCDEF';
  let salt = '';
  for (let i = 0; i < 32; i++) {
    const randomIndex = Math.floor(Math.random() * characters.length);
    salt += characters.charAt(randomIndex);
  }
  return salt;
}
// A simple custom hash function (not recommended for security-critical use)
function customHash(input) {
  let hash = 0;
  for (let i = 0; i < input.length; i++) {
    const char = input.charCodeAt(i);
    hash = (hash << 5) - hash + char;
  }
  return hash.toString(16);
}

<aside> 💡 Things to try