Skip to content

Instantly share code, notes, and snippets.

<?php
/**
Copyright 2011 Audun Larsen. All rights reserved.
larsen@xqus.com
Redistribution and use, with or without modification,
are permitted provided that the following condition is met:
* Redistribution and use of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
<?php
/* Set up array with options for the context used by file_get_contents(). */
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"User-Agent: phpSec (http://phpsec.xqus.com)\r\n"
)
);
@xqus
xqus / SHA_256.php
Created February 26, 2011 10:46
Grunnleggende kryptografi for webutviklere
<?php
echo hash('sha256', 'my password');
/* Produserer: bb14292d91c6d0920a5536bb41f3a50f66351b7b9d94c804dfce8a96ca1051f2 */
@xqus
xqus / gist:1074931
Created July 10, 2011 20:21
Creating an encryption key from a secret using PHP
<?php
/**
* Get a key from a secret.
* What we do is create two different hashes from the secret, combine them
* and pick out the number of characters we need.
* We used the raw binary output of the hash function for maximum
* bit strength (we have 255 chars to choose from, instead of 16).
*
* @param string $secret
* The secret to generate a key from.
<?php
/**
* Inject a salt into a password to create the string to be hashed.
* @author Audun Larsen <larsen@xqus.com>
*
* @param string $password
* Plain-text password.
*
* @param string $salt
* Well, the salt to inject into the password.
@xqus
xqus / new.php
Created November 7, 2011 16:59
phpSec key history
<?php
/* Check key size. */
$keySize = strlen($key);
$keySizes = mcrypt_enc_get_supported_key_sizes($td);
if(count($keySizes) > 0) {
/* Encryption method requires a specific key size. */
if(!in_array($keySize, $keySizes)) {
phpsec::error('Key is out of range. Should be one of: '. var_export($keySizes ,1));
return false;
}
ALTER TABLE table MODIFY column INT(11) UNSIGNED AFTER some_column;
@xqus
xqus / gist:1503797
Created December 20, 2011 23:27
PDO field escaping
<?php
$sth = $dbh->prepare(
'DESCRIBE `'.str_replace(array('\\',"\0" ,'`'), '', $table).'`'
);
@xqus
xqus / bcrypt.php
Created December 30, 2011 02:57 — forked from dzuelke/bcrypt.php
How to use bcrypt in PHP to safely store passwords (PHP 5.3+ only)
<?php
// secure hashing of passwords using bcrypt, needs PHP 5.3+
// see http://codahale.com/how-to-safely-store-a-password/
// salt for bcrypt needs to be 22 base64 characters (but just [./0-9A-Za-z]), see http://php.net/crypt
// just an example; please use something more secure/random than sha1(microtime) :)
$salt = substr(str_replace('+', '.', base64_encode(sha1(microtime(true), true))), 0, 22);
// 2a is the bcrypt algorithm selector, see http://php.net/crypt
@xqus
xqus / example.php
Created December 19, 2012 14:25
Signing of hash using ECDSA in PHP
<?php
// configure the ECC lib
if (!defined('USE_EXT')) {
if (extension_loaded('gmp')) {
define('USE_EXT', 'GMP');
} else if(extension_loaded('bcmath')) {
define('USE_EXT', 'BCMATH');
} else {
die('GMP or bcmath required. (GMP is faster).');
}