Hash functions have a wide range of applications in computer science and security:
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.
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
basicHash
function, which takes an inputString
as its argument.hashCode
variable to zero, which will be used to accumulate the hash value.inputString
.hashCode
.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
Driver code simpleHash(inputString)
inputString
salt
string : an independent string using generateSalt()
functionhash
string using customHash(inputString)
hash
+ salt
and generates hash
1000 timeshash + 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