Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Commit 6b3a39d

Browse files
committed
add some helper class
1 parent 034c5c3 commit 6b3a39d

File tree

2 files changed

+630
-0
lines changed

2 files changed

+630
-0
lines changed

src/Token.php

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017/7/26
6+
* Time: 下午11:55
7+
*/
8+
9+
namespace Toolkit\StrUtil;
10+
11+
/**
12+
* Usage:
13+
* $user = $db->query(['name' => $_POST['name'] ]);
14+
* 1.
15+
* gen:
16+
* $password = Token::gen('123456');
17+
* verify:
18+
* Token::verify($user['password'], '123456');
19+
* 2.
20+
* gen:
21+
* $password = Token::hash('123456');
22+
* verify:
23+
* Token::verifyHash($user['password'], '123456');
24+
*/
25+
class Token
26+
{
27+
/**
28+
* 指明应该使用的算法
29+
* $2a BLOWFISH算法。
30+
* $5 SHA-256
31+
* $6 SHA-512
32+
* @var string
33+
*/
34+
private static $algo = '$2y';
35+
36+
/**
37+
* cost parameter 就是成本参数
38+
* $10 这是以2为底的对数,指示计算循环迭代的次数(10 => 2^10 = 1024),取值可以从04到31。
39+
* @var string
40+
*/
41+
private static $cost = '$10';
42+
43+
/**
44+
* *******生成唯一序列号*******
45+
* @param $var array || obj
46+
* @return string
47+
*/
48+
public static function md5($var): string
49+
{
50+
//serialize()序列化,串行化
51+
return \md5(\md5(\serialize($var)));
52+
}
53+
54+
/**
55+
* @return string
56+
*/
57+
public static function uniqueSalt(): string
58+
{
59+
return (string)\substr(\sha1(\mt_rand()), 0, 22);
60+
}
61+
62+
/**
63+
* @param string $pwd
64+
* @param string $algo
65+
* @param array $opts
66+
* @return bool|string
67+
*/
68+
public static function pwdHash(string $pwd, string $algo, array $opts = [])
69+
{
70+
$opts = array_merge([
71+
'cost' => 9
72+
], $opts);
73+
74+
return \password_hash($pwd, $algo, $opts);
75+
}
76+
77+
/**
78+
* @param string $pwd
79+
* @param string $hash
80+
* @return bool|string
81+
*/
82+
public static function pwdVerify(string $pwd, string $hash)
83+
{
84+
return \password_verify($pwd, $hash);
85+
}
86+
87+
/**
88+
* this will be used to generate a hash
89+
* @param $password
90+
* @return string
91+
*/
92+
public static function gen(string $password): string
93+
{
94+
return \crypt($password, self::$algo . self::$cost . '$' . self::uniqueSalt());
95+
}
96+
97+
/**
98+
* this will be used to compare a password against a hash
99+
* @param string $hash
100+
* @param string $password the user input
101+
* @return bool
102+
*/
103+
public static function verify(string $hash, string $password): bool
104+
{
105+
return \hash_equals($hash, \crypt($password, $hash));
106+
}
107+
108+
/**
109+
* 2 生成
110+
* @todo from php.net
111+
* @param $password
112+
* @param int $cost
113+
* @return string
114+
* @throws \RuntimeException
115+
*/
116+
public static function hash(string $password, int $cost = 11): string
117+
{
118+
// $bytes = \random_bytes(17);
119+
$bytes = \openssl_random_pseudo_bytes(17, $cStrong);
120+
121+
if (false === $bytes || false === $cStrong) {
122+
throw new \RuntimeException('exec gen hash error!');
123+
}
124+
125+
/* To generate the salt, first generate enough random bytes. Because
126+
* base64 returns one character for each 6 bits, the we should generate
127+
* at least 22*6/8=16.5 bytes, so we generate 17. Then we get the first
128+
* 22 base64 characters
129+
*/
130+
$salt = substr(base64_encode($bytes), 0, 22);
131+
/* As blowfish takes a salt with the alphabet ./A-Za-z0-9 we have to
132+
* replace any '+' in the base64 string with '.'. We don't have to do
133+
* anything about the '=', as this only occurs when the b64 string is
134+
* padded, which is always after the first 22 characters.
135+
*/
136+
$salt = str_replace('+', '.', $salt);
137+
/* Next, create a string that will be passed to crypt, containing all
138+
* of the settings, separated by dollar signs
139+
*/
140+
$param = '$' . implode('$', [
141+
'2x', //select the most secure version of blowfish (>=PHP 5.3.7)
142+
str_pad($cost, 2, '0', STR_PAD_LEFT), //add the cost in two digits
143+
$salt //add the salt
144+
]);
145+
146+
//now do the actual hashing
147+
return crypt($password, $param);
148+
}
149+
150+
/**
151+
* 2 验证
152+
* Check the password against a hash generated by the generate_hash
153+
* function.
154+
* @param $hash
155+
* @param $password
156+
* @return bool
157+
*/
158+
public static function verifyHash(string $hash, string $password): bool
159+
{
160+
/* Regenerating the with an available hash as the options parameter should
161+
* produce the same hash if the same password is passed.
162+
*/
163+
return crypt($password, $hash) === $hash;
164+
}
165+
166+
/**
167+
* 生成guid
168+
* @return string
169+
*/
170+
public static function GUid(): string
171+
{
172+
mt_srand((double)microtime() * 10000);
173+
174+
$charId = strtolower(md5(uniqid(mt_rand(), true)));
175+
// $hyphen = chr(45);
176+
$uuid = substr($charId, 0, 8) .
177+
substr($charId, 8, 4) .
178+
substr($charId, 12, 4) .
179+
substr($charId, 16, 4) .
180+
substr($charId, 20, 12);
181+
182+
return $uuid;
183+
}
184+
185+
}

0 commit comments

Comments
 (0)