Example #1
0
function SHA256_hash($str)
{
    // Increase speed for newest version of PHP
    if (function_exists('hash_algos')) {
        $algos = hash_algos();
        if (in_array('sha256', $algos) && function_exists('hash')) {
            return hash('sha256', $str);
        }
    }
    $chunks = null;
    $M = strlen($str);
    //    number of bytes
    $L1 = $M >> 28 & 0xf;
    //    top order bits
    $L2 = $M << 3;
    //    number of bits
    $l = pack('N*', $L1, $L2);
    $k = $L2 + 64 + 1 + 511;
    $k -= $k % 512 + $L2 + 64 + 1;
    $k >>= 3;
    //    convert to byte count
    $str .= chr(0x80) . str_repeat(chr(0), $k) . $l;
    preg_match_all('#.{64}#', $str, $chunks);
    $chunks = $chunks[0];
    // H(0)
    $hash = array((int) 0x6a09e667, (int) 0xbb67ae85, (int) 0x3c6ef372, (int) 0xa54ff53a, (int) 0x510e527f, (int) 0x9b05688c, (int) 0x1f83d9ab, (int) 0x5be0cd19);
    // Compute
    $vars = 'abcdefgh';
    $K = null;
    $a = $b = $c = $d = $e = $f = $h = $g = false;
    if ($K === null) {
        $K = array((int) 0x428a2f98, (int) 0x71374491, (int) 0xb5c0fbcf, (int) 0xe9b5dba5, (int) 0x3956c25b, (int) 0x59f111f1, (int) 0x923f82a4, (int) 0xab1c5ed5, (int) 0xd807aa98, (int) 0x12835b01, (int) 0x243185be, (int) 0x550c7dc3, (int) 0x72be5d74, (int) 0x80deb1fe, (int) 0x9bdc06a7, (int) 0xc19bf174, (int) 0xe49b69c1, (int) 0xefbe4786, (int) 0xfc19dc6, (int) 0x240ca1cc, (int) 0x2de92c6f, (int) 0x4a7484aa, (int) 0x5cb0a9dc, (int) 0x76f988da, (int) 0x983e5152, (int) 0xa831c66d, (int) 0xb00327c8, (int) 0xbf597fc7, (int) 0xc6e00bf3, (int) 0xd5a79147, (int) 0x6ca6351, (int) 0x14292967, (int) 0x27b70a85, (int) 0x2e1b2138, (int) 0x4d2c6dfc, (int) 0x53380d13, (int) 0x650a7354, (int) 0x766a0abb, (int) 0x81c2c92e, (int) 0x92722c85, (int) 0xa2bfe8a1, (int) 0xa81a664b, (int) 0xc24b8b70, (int) 0xc76c51a3, (int) 0xd192e819, (int) 0xd6990624, (int) 0xf40e3585, (int) 0x106aa070, (int) 0x19a4c116, (int) 0x1e376c08, (int) 0x2748774c, (int) 0x34b0bcb5, (int) 0x391c0cb3, (int) 0x4ed8aa4a, (int) 0x5b9cca4f, (int) 0x682e6ff3, (int) 0x748f82ee, (int) 0x78a5636f, (int) 0x84c87814, (int) 0x8cc70208, (int) 0x90befffa, (int) 0xa4506ceb, (int) 0xbef9a3f7, (int) 0xc67178f2);
    }
    $W = array();
    for ($i = 0, $numChunks = sizeof($chunks); $i < $numChunks; $i++) {
        //    initialize the registers
        for ($j = 0; $j < 8; $j++) {
            ${$vars[$j]} = $hash[$j];
        }
        //    the SHA-256 compression function
        for ($j = 0; $j < 64; $j++) {
            if ($j < 16) {
                $T1 = ord($chunks[$i][$j * 4]) & 0xff;
                $T1 <<= 8;
                $T1 |= ord($chunks[$i][$j * 4 + 1]) & 0xff;
                $T1 <<= 8;
                $T1 |= ord($chunks[$i][$j * 4 + 2]) & 0xff;
                $T1 <<= 8;
                $T1 |= ord($chunks[$i][$j * 4 + 3]) & 0xff;
                $W[$j] = $T1;
            } else {
                $W[$j] = SHA256_sum(($W[$j - 2] >> 17 & 0x7fff | $W[$j - 2] << 15) ^ ($W[$j - 2] >> 19 & 0x1fff | $W[$j - 2] << 13) ^ $W[$j - 2] >> 10 & 0x3fffff, $W[$j - 7], ($W[$j - 15] >> 7 & 0x1ffffff | $W[$j - 15] << 25) ^ ($W[$j - 15] >> 18 & 0x3fff | $W[$j - 15] << 14) ^ $W[$j - 15] >> 3 & 0x1fffffff, $W[$j - 16]);
            }
            $T1 = SHA256_sum($h, ($e >> 6 & 0x3ffffff | $e << 26) ^ ($e >> 11 & 0x1fffff | $e << 21) ^ ($e >> 25 & 0x7f | $e << 7), $e & $f ^ ~$e & $g, $K[$j], $W[$j]);
            $T2 = SHA256_sum(($a >> 2 & 0x3fffffff | $a << 30) ^ ($a >> 13 & 0x7ffff | $a << 19) ^ ($a >> 22 & 0x3ff | $a << 10), $a & $b ^ $a & $c ^ $b & $c);
            $h = $g;
            $g = $f;
            $f = $e;
            $e = SHA256_sum($d, $T1);
            $d = $c;
            $c = $b;
            $b = $a;
            $a = SHA256_sum($T1, $T2);
        }
        //    compute the next hash set
        for ($j = 0; $j < 8; $j++) {
            $hash[$j] = SHA256_sum(${$vars[$j]}, $hash[$j]);
        }
    }
    // HASH HEX
    $str = '';
    reset($hash);
    do {
        $str .= sprintf('%08x', current($hash));
    } while (next($hash));
    return $str;
}
Example #2
0
function SHA256_hash($str)
{
    $chunks = null;
    $M = strlen($str);
    //    number of bytes
    $L1 = $M >> 28 & 0xf;
    //    top order bits
    $L2 = $M << 3;
    //    number of bits
    $l = pack('N*', $L1, $L2);
    $k = $L2 + 64 + 1 + 511;
    $k -= $k % 512 + $L2 + 64 + 1;
    $k >>= 3;
    //    convert to byte count
    $str .= chr(0x80) . str_repeat(chr(0), $k) . $l;
    preg_match_all('#.{64}#', $str, $chunks);
    $chunks = $chunks[0];
    // H(0)
    $hash = array((int) 0x6a09e667, (int) 0.0, (int) 0x3c6ef372, (int) 2773480762.0, (int) 0x510e527f, (int) 2600822924.0, (int) 0x1f83d9ab, (int) 0x5be0cd19);
    // Compute
    $vars = 'abcdefgh';
    $K = null;
    $a = $b = $c = $d = $e = $f = $h = $g = false;
    if ($K === null) {
        $K = array((int) 0x428a2f98, (int) 0x71374491, (int) 3049323471.0, (int) 0.0, (int) 0x3956c25b, (int) 0x59f111f1, (int) 2453635748.0, (int) 0.0, (int) 3624381080.0, (int) 0x12835b01, (int) 0x243185be, (int) 0x550c7dc3, (int) 0x72be5d74, (int) 0.0, (int) 2614888103.0, (int) 3248222580.0, (int) 0.0, (int) 0.0, (int) 0xfc19dc6, (int) 0x240ca1cc, (int) 0x2de92c6f, (int) 0x4a7484aa, (int) 0x5cb0a9dc, (int) 0x76f988da, (int) 0.0, (int) 2821834349.0, (int) 2952996808.0, (int) 3210313671.0, (int) 0.0, (int) 3584528711.0, (int) 0x6ca6351, (int) 0x14292967, (int) 0x27b70a85, (int) 0x2e1b2138, (int) 0x4d2c6dfc, (int) 0x53380d13, (int) 0x650a7354, (int) 0x766a0abb, (int) 0.0, (int) 2456956037.0, (int) 0.0, (int) 2820302411.0, (int) 3259730800.0, (int) 3345764771.0, (int) 0.0, (int) 3600352804.0, (int) 0.0, (int) 0x106aa070, (int) 0x19a4c116, (int) 0x1e376c08, (int) 0x2748774c, (int) 0x34b0bcb5, (int) 0x391c0cb3, (int) 0x4ed8aa4a, (int) 0x5b9cca4f, (int) 0x682e6ff3, (int) 0x748f82ee, (int) 0x78a5636f, (int) 2227730452.0, (int) 2361852424.0, (int) 0.0, (int) 0.0, (int) 0.0, (int) 3329325298.0);
    }
    $W = array();
    for ($i = 0, $numChunks = sizeof($chunks); $i < $numChunks; $i++) {
        //    initialize the registers
        for ($j = 0; $j < 8; $j++) {
            ${$vars[$j]} = $hash[$j];
        }
        //    the SHA-256 compression function
        for ($j = 0; $j < 64; $j++) {
            if ($j < 16) {
                $T1 = ord($chunks[$i][$j * 4]) & 0xff;
                $T1 <<= 8;
                $T1 |= ord($chunks[$i][$j * 4 + 1]) & 0xff;
                $T1 <<= 8;
                $T1 |= ord($chunks[$i][$j * 4 + 2]) & 0xff;
                $T1 <<= 8;
                $T1 |= ord($chunks[$i][$j * 4 + 3]) & 0xff;
                $W[$j] = $T1;
            } else {
                $W[$j] = SHA256_sum(($W[$j - 2] >> 17 & 0x7fff | $W[$j - 2] << 15) ^ ($W[$j - 2] >> 19 & 0x1fff | $W[$j - 2] << 13) ^ $W[$j - 2] >> 10 & 0x3fffff, $W[$j - 7], ($W[$j - 15] >> 7 & 0x1ffffff | $W[$j - 15] << 25) ^ ($W[$j - 15] >> 18 & 0x3fff | $W[$j - 15] << 14) ^ $W[$j - 15] >> 3 & 0x1fffffff, $W[$j - 16]);
            }
            $T1 = SHA256_sum($h, ($e >> 6 & 0x3ffffff | $e << 26) ^ ($e >> 11 & 0x1fffff | $e << 21) ^ ($e >> 25 & 0x7f | $e << 7), $e & $f ^ ~$e & $g, $K[$j], $W[$j]);
            $T2 = SHA256_sum(($a >> 2 & 0x3fffffff | $a << 30) ^ ($a >> 13 & 0x7ffff | $a << 19) ^ ($a >> 22 & 0x3ff | $a << 10), $a & $b ^ $a & $c ^ $b & $c);
            $h = $g;
            $g = $f;
            $f = $e;
            $e = SHA256_sum($d, $T1);
            $d = $c;
            $c = $b;
            $b = $a;
            $a = SHA256_sum($T1, $T2);
        }
        //    compute the next hash set
        for ($j = 0; $j < 8; $j++) {
            $hash[$j] = SHA256_sum(${$vars[$j]}, $hash[$j]);
        }
    }
    // HASH HEX
    $str = '';
    reset($hash);
    do {
        $str .= sprintf('%08x', current($hash));
    } while (next($hash));
    return $str;
}