A Scripting Language for Web, Linux and Windows

A Scripting Language for Web, Linux and Windows

Example: Hash algorithms

Demonstrates how to work with hash algorithms in crypt module. The crypt() function is full compatible with PHP crypt ().

<?php <?v1

dl ("crypt"); // Load crypt module

// Function to create random strings
function getRandomString (max=22) {
    // Initialize the random timer
    srand (microtime ());

    str = "";
    for (j = 0; j < max; j++) {
        str.=substr("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", rand()%64, 1);
    }
    return str;
}

text = "String to hash";

// DES need a 2 byte salt
salt = getRandomString(2);
print ("DES crypt() = ".crypt(text, salt));

// If no salt is given then MD5 with random salt is chosen
print ("MD5 crypt() = ".crypt(text));

salt = '$2y$07$'.getRandomString();
print ("Blowfish crypt() = ".crypt(text, salt));

salt = '$5$rounds=5000$'.getRandomString(5).'$';
print ("SHA256 crypt() = ".crypt(text, salt));  

salt = '$6$rounds=5000$'.getRandomString(5).'$';
print ("SHA512 crypt() = ".crypt(text, salt));  

// Print integrated hash algorithms
print_r (hash_algos ());

// Calculate hash of a complete file
print ("SHA256 of file v1.exe: ", hash_file ("sha256", "v1.exe"));

?>

back to Home