Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hash a string with PHP's password_hash() function.
<?php
// Hash a password
// by Gavin Botica
// requires PHP 5.5+
define("RQD_PHP_VER", "5.5");
// check php version
if (version_compare(PHP_VERSION, RQD_PHP_VER) == -1 ) {
echo "<p><b>ERROR:</b> PHP <i>" . RQD_PHP_VER . "</i> required. Version <i>" . PHP_VERSION . "</i> installed.</p>";
exit;
}
?>
<html>
<head>
<title>Hash a Password</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(function() {
var $form_password_input = $('form #input_password').focus();
var $show_input_checkbox = $('form #show_input');
$show_input_checkbox.on('change', function(){
var set_checkbox = $show_input_checkbox.is(':checked') ? $form_password_input.attr('type', 'text') : $form_password_input.attr('type', 'password');
});
if ($("#output").length > 0) {
$("#output").slideDown(750);
}
});
</script>
<style type="text/css">
body {
padding:50px 30px 0;
background-color: #f6f6f6;
text-align: center;
max-width:980px;
margin:auto;
}
body,
input {
font-family: "Courier New", sans-serif;
font-size: 23px;
}
h1, h2 {
font-weight: 800;
}
hr {
border:0;
border-top:1px dashed #ddd;
}
fieldset {
border:1px solid #ccc;
border-radius: 5px;
padding:40px 40px 25px;
background-color: #f9f9f9;
width:88%;
margin:0 auto;
}
legend {
font-weight: bold;
padding:0 20px;
}
input[type=text],
input[type=password] {
margin-top:10px;
padding:6px 5px;
border-radius: 3px;
border: 1px solid #ccc;
}
input[type=submit] {
margin-top:10px;
padding:5px 15px;
font-weight: bold;
color:#777;
border: 1px solid #bbb;
}
input[type=submit]:hover{
cursor:pointer;
color:#444;
border-color:#999;
}
#input_password {
width:60%;
min-width: 320px;
}
#output input {
width:90%;
max-width:940px;
padding:3px;
text-align: center;
}
footer {
font-size:0.6em;
}
.hide {
display: none;
}
</style>
</head>
<body>
<h1>Hash a password!</h1>
<p>Enter a string to hash it with PHP's <a target="_blank" href="http://php.net/manual/en/function.password-hash.php"><i>password_hash()</i></a> function.</p>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Hash Password</legend>
<input tabindex="1" placeholder="String to Hash" id="input_password" type="password" name="h">
<input tabindex="3" type="submit" value="Hash it!">
<br><br><label><input tabindex="2" type="checkbox" id="show_input" value="1" name='s'> Display input?</label>
</fieldset>
</form>
<?php
// If form posted, write OUTPUT
if (isset($_POST["h"]) && $_POST["h"] !== "" ) {
$string_to_hash = $_POST["h"];
$hash = password_hash($string_to_hash, PASSWORD_DEFAULT);
// output
echo "<hr>";
echo "<div id='output' class='hide'>";
echo "<h3>Result</h3>";
if (isset($_POST["s"])) {
echo "<p>INPUT: <b>$string_to_hash</b></p>";
}
echo "<p>OUTPUT: <input type='text' value='" . $hash . "'></p>";
echo "</div>";
}
?>
<hr>
<footer></footer>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.