~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Helper/Pbkdf2Hash.php

  • Committer: Dan Garner
  • Date: 2016-02-18 16:07:16 UTC
  • mfrom: (454.4.137)
  • Revision ID: git-v1:8867f12675bc9e0e67e7e622c80da7471b9f294a
Merge pull request #139 from dasgarner/feature/nested-display-groups

Feature/nested display groups

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Spring Signage Ltd - http://www.springsignage.com
4
 
 * Copyright (C) 2016 Spring Signage Ltd
5
 
 * (PasswordStorage.php)
6
 
 */
7
 
 
8
 
 
9
 
namespace Xibo\Helper;
10
 
 
11
 
/**
12
 
 * Class Pbkdf2Hash
13
 
 * @package Xibo\Helper
14
 
 */
15
 
class Pbkdf2Hash
16
 
{
17
 
    // These constants may be changed without breaking existing hashes.
18
 
    const PBKDF2_HASH_ALGORITHM = 'sha256';
19
 
    const PBKDF2_ITERATIONS = 1000;
20
 
    const PBKDF2_SALT_BYTES = 24;
21
 
    const PBKDF2_HASH_BYTES = 24;
22
 
 
23
 
    // These constants define the encoding and may not be changed.
24
 
    const HASH_SECTIONS = 4;
25
 
    const HASH_ALGORITHM_INDEX = 0;
26
 
    const HASH_ITERATION_INDEX = 1;
27
 
    const HASH_SALT_INDEX = 2;
28
 
    const HASH_PBKDF2_INDEX = 3;
29
 
 
30
 
    /**
31
 
     * @param string $password
32
 
     * @param string $hash
33
 
     * @return bool
34
 
     */
35
 
    public static function verifyPassword($password, $hash)
36
 
    {
37
 
        $params = explode(':', $hash);
38
 
        if (count($params) < self::HASH_SECTIONS)
39
 
            throw new \InvalidArgumentException('Invalid password hash - not enough hash sections');
40
 
 
41
 
        $pbkdf2 = base64_decode($params[self::HASH_PBKDF2_INDEX]);
42
 
 
43
 
        // Check to see if the hash created from the provided password is the same as the hash we have stored already
44
 
        return (self::slowEquals(
45
 
            $pbkdf2,
46
 
            self::pbkdf2(
47
 
                $params[self::HASH_ALGORITHM_INDEX],
48
 
                $password,
49
 
                $params[self::HASH_SALT_INDEX],
50
 
                (int)$params[self::HASH_ITERATION_INDEX],
51
 
                strlen($pbkdf2),
52
 
                true
53
 
            )
54
 
        ));
55
 
    }
56
 
 
57
 
    /**
58
 
     * Compares two strings $a and $b in length-constant time.
59
 
     * @param string $a
60
 
     * @param string $b
61
 
     * @return bool
62
 
     */
63
 
    private static function slowEquals($a, $b)
64
 
    {
65
 
        $diff = strlen($a) ^ strlen($b);
66
 
        for($i = 0; $i < strlen($a) && $i < strlen($b); $i++)
67
 
        {
68
 
            $diff |= ord($a[$i]) ^ ord($b[$i]);
69
 
        }
70
 
        return $diff === 0;
71
 
    }
72
 
 
73
 
    /**
74
 
     * PBKDF2 key derivation function as defined by RSA's PKCS #5: https://www.ietf.org/rfc/rfc2898.txt
75
 
     *
76
 
     * Test vectors can be found here: https://www.ietf.org/rfc/rfc6070.txt
77
 
     *
78
 
     * This implementation of PBKDF2 was originally created by https://defuse.ca
79
 
     * With improvements by http://www.variations-of-shadow.com
80
 
     *
81
 
     * @param string $algorithm The hash algorithm to use. Recommended: SHA256
82
 
     * @param string $password The password.
83
 
     * @param string $salt A salt that is unique to the password.
84
 
     * @param int $count Iteration count. Higher is better, but slower. Recommended: At least 1000.
85
 
     * @param int $key_length The length of the derived key in bytes.
86
 
     * @param bool $raw_output If true, the key is returned in raw binary format. Hex encoded otherwise.
87
 
     * @return string A $key_length-byte key derived from the password and salt.
88
 
     */
89
 
    public static function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false)
90
 
    {
91
 
        $algorithm = strtolower($algorithm);
92
 
        if (!in_array($algorithm, hash_algos(), true))
93
 
            throw new \InvalidArgumentException('PBKDF2 ERROR: Invalid hash algorithm.');
94
 
        if ($count <= 0 || $key_length <= 0)
95
 
            throw new \InvalidArgumentException('PBKDF2 ERROR: Invalid parameters.');
96
 
 
97
 
        $hash_length = strlen(hash($algorithm, "", true));
98
 
        $block_count = ceil($key_length / $hash_length);
99
 
 
100
 
        $output = "";
101
 
        for ($i = 1; $i <= $block_count; $i++) {
102
 
            // $i encoded as 4 bytes, big endian.
103
 
            $last = $salt . pack("N", $i);
104
 
            // first iteration
105
 
            $last = $xorsum = hash_hmac($algorithm, $last, $password, true);
106
 
            // perform the other $count - 1 iterations
107
 
            for ($j = 1; $j < $count; $j++) {
108
 
                $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
109
 
            }
110
 
            $output .= $xorsum;
111
 
        }
112
 
 
113
 
        if ($raw_output)
114
 
            return substr($output, 0, $key_length);
115
 
        else
116
 
            return bin2hex(substr($output, 0, $key_length));
117
 
    }
118
 
}
 
 
b'\\ No newline at end of file'