Skip to content

Commit 15b8c41

Browse files
committed
added support for PHP 8.0 and removed PHP 7.0 and PHP 7.1
1 parent f215a5e commit 15b8c41

File tree

6 files changed

+166
-170
lines changed

6 files changed

+166
-170
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
vendor/
33
phpunit.xml
44
composer.phar
5-
composer.lock
5+
composer.lock
6+
.php-cs-fixer.cache
7+
.phpunit.result.cache

.travis.yml

Lines changed: 0 additions & 14 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ PHP crypt-md5
33

44
A pure PHP implementation of an MD5-hashsum-based implementation of the crypt routine, which can be used to generate hashs for Apache's passwd files.
55

6-
[![Build Status](https://travis-ci.org/driehle/php-crypt-md5.png)](https://travis-ci.org/driehle/php-crypt-md5)
6+
[![Build Status](https://github.com/driehle/php-crypt-md5/workflows/Continuous%20Integration/badge.svg)](https://github.com/driehle/php-crypt-md5/actions?query=workflow%3A"Continuous+Integration")
77
[![Latest Stable Version](https://poser.pugx.org/driehle/php-crypt-md5/v/stable)](https://packagist.org/packages/driehle/php-crypt-md5)
88
[![Total Downloads](https://poser.pugx.org/driehle/php-crypt-md5/downloads)](https://packagist.org/packages/driehle/php-crypt-md5)
99
[![Latest Unstable Version](https://poser.pugx.org/driehle/php-crypt-md5/v/unstable)](https://packagist.org/packages/driehle/php-crypt-md5)

composer.json

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,27 @@
2020
"role": "Developer"
2121
}
2222
],
23+
"require": {
24+
"php": "^7.2 || ~8.0.0"
25+
},
2326
"require-dev": {
24-
"phpunit/phpunit": "^6.5.0"
27+
"ergebnis/composer-normalize": "^2.13",
28+
"friendsofphp/php-cs-fixer": "^3.0",
29+
"phpunit/phpunit": "^9.5"
2530
},
26-
"require": {
27-
"php": ">=7.0"
31+
"extra": {
32+
"branch-alias": {
33+
"dev-master": "3.1-dev"
34+
}
2835
},
2936
"autoload": {
3037
"psr-0": {
3138
"Md5Crypt\\": "library/"
3239
}
3340
},
34-
"extra": {
35-
"branch-alias": {
36-
"dev-master": "3.0-dev"
37-
}
41+
"scripts": {
42+
"cs-check": "php-cs-fixer fix --dry-run --verbose",
43+
"cs-fix": "php-cs-fixer fix --verbose",
44+
"test": "phpunit"
3845
}
39-
}
46+
}

library/Md5Crypt/Md5Crypt.php

Lines changed: 109 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -44,115 +44,115 @@
4444

4545
namespace Md5Crypt;
4646

47-
class Md5Crypt
47+
class Md5Crypt
4848
{
49-
static public $itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
50-
// [a-zA-Z0-9./]
51-
52-
static protected function to64($v, $n)
53-
{
54-
$itoa64 = self::$itoa64;
55-
$ret = '';
56-
57-
while(--$n >= 0) {
58-
$ret .= $itoa64[$v & 0x3f];
59-
$v = $v >> 6;
60-
}
61-
62-
return $ret;
63-
}
64-
65-
static public function apache($pw, $salt = NULL)
66-
{
67-
$Magic = '$apr1$';
68-
69-
return self::unix($pw, $salt, $Magic);
70-
}
71-
72-
static public function unix($pw, $salt = NULL, $Magic = '$1$')
73-
{
74-
$itoa64 = self::$itoa64;
75-
76-
if($salt !== NULL) {
77-
// Take care of the magic string if present
78-
if(substr($salt, 0, strlen($Magic)) == $Magic) {
79-
$salt = substr($salt, strlen($Magic), strlen($salt));
80-
}
81-
// Salt can have up to 8 characters
82-
$parts = explode('$', $salt, 1);
83-
$salt = substr($parts[0], 0, 8);
84-
} else {
85-
$salt = '';
86-
mt_srand((double)(microtime() * 10000000));
87-
88-
while(strlen($salt) < 8) {
89-
$salt .= $itoa64[mt_rand(0, strlen($itoa64)-1)];
90-
}
91-
}
92-
93-
$ctx = $pw . $Magic . $salt;
94-
95-
$final = pack('H*', md5($pw . $salt . $pw));
96-
97-
for ($pl = strlen($pw); $pl > 0; $pl -= 16) {
98-
$ctx .= substr($final, 0, ($pl > 16) ? 16 : $pl);
99-
}
100-
101-
// Now the 'weird' xform
102-
for($i = strlen($pw); $i; $i >>= 1) {
103-
if($i & 1) { // This comes from the original version,
104-
$ctx .= pack("C", 0); // where a memset() is done to $final
105-
} else { // before this loop
106-
$ctx .= $pw[0];
107-
}
108-
}
109-
110-
$final = pack('H*', md5($ctx)); // The following is supposed to make
111-
// things run slower
112-
113-
for($i = 0; $i < 1000; $i++) {
114-
$ctx1 = '';
115-
if($i & 1) {
116-
$ctx1 .= $pw;
117-
} else {
118-
$ctx1 .= substr($final, 0, 16);
119-
}
120-
if($i % 3) {
121-
$ctx1 .= $salt;
122-
}
123-
if($i % 7) {
124-
$ctx1 .= $pw;
125-
}
126-
if($i & 1) {
127-
$ctx1 .= substr($final, 0, 16);
128-
} else {
129-
$ctx1 .= $pw;
130-
}
131-
$final = pack('H*', md5($ctx1));
132-
}
133-
134-
// Final xform
135-
$passwd = '';
136-
$passwd .= self::to64((intval(ord($final[0])) << 16)
137-
|(intval(ord($final[6])) << 8)
138-
|(intval(ord($final[12]))),4);
139-
$passwd .= self::to64((intval(ord($final[1])) << 16)
140-
|(intval(ord($final[7])) << 8)
141-
|(intval(ord($final[13]))), 4);
142-
$passwd .= self::to64((intval(ord($final[2])) << 16)
143-
|(intval(ord($final[8])) << 8)
144-
|(intval(ord($final[14]))), 4);
145-
$passwd .= self::to64((intval(ord($final[3])) << 16)
146-
|(intval(ord($final[9])) << 8)
147-
|(intval(ord($final[15]))), 4);
148-
$passwd .= self::to64((intval(ord($final[4]) << 16)
149-
|(intval(ord($final[10])) << 8)
150-
|(intval(ord($final[5])))), 4);
151-
$passwd .= self::to64((intval(ord($final[11]))), 2);
152-
153-
// Return the final string
154-
return $Magic . $salt . '$' . $passwd;
155-
}
49+
public static $itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
50+
// [a-zA-Z0-9./]
51+
52+
protected static function to64($v, $n)
53+
{
54+
$itoa64 = self::$itoa64;
55+
$ret = '';
56+
57+
while (--$n >= 0) {
58+
$ret .= $itoa64[$v & 0x3f];
59+
$v = $v >> 6;
60+
}
61+
62+
return $ret;
63+
}
64+
65+
public static function apache($pw, $salt = null)
66+
{
67+
$Magic = '$apr1$';
68+
69+
return self::unix($pw, $salt, $Magic);
70+
}
71+
72+
public static function unix($pw, $salt = null, $Magic = '$1$')
73+
{
74+
$itoa64 = self::$itoa64;
75+
76+
if (null !== $salt) {
77+
// Take care of the magic string if present
78+
if (substr($salt, 0, strlen($Magic)) == $Magic) {
79+
$salt = substr($salt, strlen($Magic), strlen($salt));
80+
}
81+
// Salt can have up to 8 characters
82+
$parts = explode('$', $salt, 1);
83+
$salt = substr($parts[0], 0, 8);
84+
} else {
85+
$salt = '';
86+
mt_srand((float) (microtime() * 10000000));
87+
88+
while (strlen($salt) < 8) {
89+
$salt .= $itoa64[mt_rand(0, strlen($itoa64) - 1)];
90+
}
91+
}
92+
93+
$ctx = $pw . $Magic . $salt;
94+
95+
$final = pack('H*', md5($pw . $salt . $pw));
96+
97+
for ($pl = strlen($pw); $pl > 0; $pl -= 16) {
98+
$ctx .= substr($final, 0, ($pl > 16) ? 16 : $pl);
99+
}
100+
101+
// Now the 'weird' xform
102+
for ($i = strlen($pw); $i; $i >>= 1) {
103+
if ($i & 1) { // This comes from the original version,
104+
$ctx .= pack('C', 0); // where a memset() is done to $final
105+
} else { // before this loop
106+
$ctx .= $pw[0];
107+
}
108+
}
109+
110+
$final = pack('H*', md5($ctx)); // The following is supposed to make
111+
// things run slower
112+
113+
for ($i = 0; $i < 1000; ++$i) {
114+
$ctx1 = '';
115+
if ($i & 1) {
116+
$ctx1 .= $pw;
117+
} else {
118+
$ctx1 .= substr($final, 0, 16);
119+
}
120+
if ($i % 3) {
121+
$ctx1 .= $salt;
122+
}
123+
if ($i % 7) {
124+
$ctx1 .= $pw;
125+
}
126+
if ($i & 1) {
127+
$ctx1 .= substr($final, 0, 16);
128+
} else {
129+
$ctx1 .= $pw;
130+
}
131+
$final = pack('H*', md5($ctx1));
132+
}
133+
134+
// Final xform
135+
$passwd = '';
136+
$passwd .= self::to64((intval(ord($final[0])) << 16)
137+
| (intval(ord($final[6])) << 8)
138+
| (intval(ord($final[12]))), 4);
139+
$passwd .= self::to64((intval(ord($final[1])) << 16)
140+
| (intval(ord($final[7])) << 8)
141+
| (intval(ord($final[13]))), 4);
142+
$passwd .= self::to64((intval(ord($final[2])) << 16)
143+
| (intval(ord($final[8])) << 8)
144+
| (intval(ord($final[14]))), 4);
145+
$passwd .= self::to64((intval(ord($final[3])) << 16)
146+
| (intval(ord($final[9])) << 8)
147+
| (intval(ord($final[15]))), 4);
148+
$passwd .= self::to64((intval(ord($final[4]) << 16)
149+
| (intval(ord($final[10])) << 8)
150+
| (intval(ord($final[5])))), 4);
151+
$passwd .= self::to64((intval(ord($final[11]))), 2);
152+
153+
// Return the final string
154+
return $Magic . $salt . '$' . $passwd;
155+
}
156156
}
157157

158-
// eof
158+
// eof

tests/Md5Crypt/Md5CryptTest.php

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
11
<?php
22

33
use Md5Crypt\Md5Crypt;
4+
use PHPUnit\Framework\TestCase;
45

5-
class Md5CryptTest extends \PHPUnit\Framework\TestCase
6+
class Md5CryptTest extends TestCase
67
{
7-
public function testCryptApache()
8-
{
9-
$hashs = array(
10-
'$apr1$LhjuTBTG$1yyIoSO1SonJQrGaHwQkG/' => 'test123',
11-
'$apr1$w2go1jJe$7fxPGEMF4Bt9r86NGDdcV/' => 'test123',
12-
'$apr1$0MSTHowM$hP/I/aMgXfOZV9OAVg14t.' => 'test123',
13-
'$apr1$4cumEdQx$82LyOEwV2gxHc4SVnzjB8.' => 'su/P3R%se#ret!',
14-
'$apr1$aKppLQLP$Vk.YsHCENJQVooBehuEeP.' => 'su/P3R%se#ret!',
15-
'$apr1$XdX7skyY$XnBqBmU6XBvbljPX5uyip.' => 'su/P3R%se#ret!'
16-
);
17-
18-
foreach ($hashs as $hash => $plain) {
19-
$salt = substr($hash, 6, 8);
20-
$calc = Md5Crypt::apache($plain, $salt);
21-
$this->assertEquals($hash, $calc);
22-
}
23-
}
24-
25-
public function testCryptUnix()
26-
{
27-
$hashs = array(
28-
'$1$bdy7K2F5$0vwww3cF65jxjSNrqf4D61' => 'test123',
29-
'$1$DINllbW2$fp/T3/GNNqJetH9pg/7q91' => 'test123',
30-
'$1$CDEt7N.1$LSPGEQscQy8ET/OxM2XZx1' => 'test123',
31-
'$1$36D57MdR$dxeAJ0ui5Kw2rnTBm1cdV1' => 'su/P3R%se#ret!',
32-
'$1$lI4qkDn4$Q32BgF2LjjcMmMfAoIPHZ0' => 'su/P3R%se#ret!',
33-
'$1$6D3ntBXk$Z73y/.fMJLxDR1QCgBhug1' => 'su/P3R%se#ret!'
34-
);
35-
36-
foreach ($hashs as $hash => $plain) {
37-
$salt = substr($hash, 3, 8);
38-
$calc = Md5Crypt::unix($plain, $salt);
39-
$this->assertEquals($hash, $calc);
40-
}
41-
}
8+
public function testCryptApache()
9+
{
10+
$hashs = [
11+
'$apr1$LhjuTBTG$1yyIoSO1SonJQrGaHwQkG/' => 'test123',
12+
'$apr1$w2go1jJe$7fxPGEMF4Bt9r86NGDdcV/' => 'test123',
13+
'$apr1$0MSTHowM$hP/I/aMgXfOZV9OAVg14t.' => 'test123',
14+
'$apr1$4cumEdQx$82LyOEwV2gxHc4SVnzjB8.' => 'su/P3R%se#ret!',
15+
'$apr1$aKppLQLP$Vk.YsHCENJQVooBehuEeP.' => 'su/P3R%se#ret!',
16+
'$apr1$XdX7skyY$XnBqBmU6XBvbljPX5uyip.' => 'su/P3R%se#ret!',
17+
];
18+
19+
foreach ($hashs as $hash => $plain) {
20+
$salt = substr($hash, 6, 8);
21+
$calc = Md5Crypt::apache($plain, $salt);
22+
$this->assertEquals($hash, $calc);
23+
}
24+
}
25+
26+
public function testCryptUnix()
27+
{
28+
$hashs = [
29+
'$1$bdy7K2F5$0vwww3cF65jxjSNrqf4D61' => 'test123',
30+
'$1$DINllbW2$fp/T3/GNNqJetH9pg/7q91' => 'test123',
31+
'$1$CDEt7N.1$LSPGEQscQy8ET/OxM2XZx1' => 'test123',
32+
'$1$36D57MdR$dxeAJ0ui5Kw2rnTBm1cdV1' => 'su/P3R%se#ret!',
33+
'$1$lI4qkDn4$Q32BgF2LjjcMmMfAoIPHZ0' => 'su/P3R%se#ret!',
34+
'$1$6D3ntBXk$Z73y/.fMJLxDR1QCgBhug1' => 'su/P3R%se#ret!',
35+
];
36+
37+
foreach ($hashs as $hash => $plain) {
38+
$salt = substr($hash, 3, 8);
39+
$calc = Md5Crypt::unix($plain, $salt);
40+
$this->assertEquals($hash, $calc);
41+
}
42+
}
4243
}
4344

44-
// eof
45+
// eof

0 commit comments

Comments
 (0)