Skip to content

Instantly share code, notes, and snippets.

@ircmaxell
ircmaxell / gist:4112366
Created November 19, 2012 18:02
FUBAR password hashing implementation
<?php
if( ! function_exists('hash_password'))
{
function hash_password($password = NULL, $salt = NULL, $salt2)
{
if($password === NULL)
return FALSE;
if($salt === NULL)
@ircmaxell
ircmaxell / gist:4121166
Created November 20, 2012 21:12
Predictable Sequence Generator
<?php
class PseudoRandomGenerator {
protected $state = null;
public function __construct($seed) {
$this->state = $seed;
}
public function next($max) {
$bits = (int) floor(log($max, 2) + 1);
$bytes = (int) max(ceil($bits / 8), 1);
Comment Reply To http://www.joshparker.us/php/securely_hashing_passwords_with_php.html
Ok, before I dig into this, you really should read this post: http://blog.ircmaxell.com/2012/12/seven-ways-to-screw-up-bcrypt.html
Now, with that said, let’s review your implementation.
1. Using a hard-coded cost parameter to bcrypt. This is bad, because the cost is hardware dependent and you need to adjust it for the server you’re running on.
2. You never use your salt. You’re not returning anything from the “salt()” method. Therefore all your hashes are unsalted (and as such will be DES, which is BAD).
@ircmaxell
ircmaxell / Class1.php
Last active December 10, 2015 23:49
AMD Autoloader Sneak Peak
<?php
namespace RequirePHP\Test;
class Class1 implements Interface1 {
}
<?php
function swap(array &$array, $k1, $k2) {
$temp = $array[$k1];
$array[$k1] = $array[$k2];
$array[$k2] = $temp;
}
function partition(array &$array, $left, $right, $pivot) {
$pValue = $array[$pivot];
<?php
class Foo {
public $bar;
function __construct($bar)
{
$this->bar = $bar;
}
}
@ircmaxell
ircmaxell / equality.php
Last active April 19, 2019 13:52
Unicode Set Functions
<?php
const ✓ = true;
const ✕ = false;
function ≠($left, $right) {
return $left != $right;
}
function ≅($left, $right) {
protected function setOptionDefaults(array &$storage, array $options) {
$queue = array(array(&$storage, $options));
unset($storage, $options);
do {
$row = array_shift($queue);
$options = $row[1];
$storage = &$row[0];
foreach ($options as $option => $definition) {
if (isset($definition['contains'])) {
$storage[$option] = array();
@ircmaxell
ircmaxell / gist:5265418
Created March 28, 2013 17:59
Reddit Post In Reply To My "Becoming A Better Developer" video

I agree with this concept, on the basis that you already have an established basic understanding of what's out there.

A lot of people are basically lost in an overwhelming sea of solutions and don't always understand which is the best most applicable one. Without initial direction, learning to learn has a good chance to learn the wrong way to do something. Asking questions sometimes just means someone wants to do it right the first time. Both are useful, one is quicker.

I think the biggest problem people starting out have is a lack of knowing where to begin at all. What should I learn first? What best prepares me for learning what to learn. Do I learn straight php? Should I focus on OO? Do I use a CMS? Which one? Framework? Which one?

Asking for guidance is not bad, but it's hard to give because there is no perfect answer. For those expecting guidance, telling them to learn how to learn may seem sagelike but it's also a cop-out.

@ircmaxell
ircmaxell / setMath.js
Last active December 15, 2015 14:19
Math, using set operations in JavaScript. Why? Because `+` and `-` are just too hard to use... ProTip: don't use large numbers, unless you dislike your CPU... :-D
(function() {
var Unit = function(value) {
var elements = [];
if (value instanceof Array) {
elements = value.slice();
} else {
for (var i = 0; i < value; i++) {
elements.push(1);
}