Skip to content

Commit 10e4c1e

Browse files
committed
Merge pull request #1 from Rican7/feature/openssl-encryption-compat
Feature - OpenSSL encryption compatibility
2 parents cbc5c30 + 55b9396 commit 10e4c1e

3 files changed

Lines changed: 155 additions & 0 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"ext-mbstring": "*",
2525
"ext-iconv": "*",
2626
"ext-spl": "*",
27+
"ext-openssl": "*",
2728
"phpunit/phpunit": "^4.7",
2829
"phpunit/php-code-coverage": "^2.2",
2930
"squizlabs/php_codesniffer": "^2.3"
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Robin NTLM
4+
*
5+
* @copyright 2015 Robin Powered, Inc.
6+
* @link https://robinpowered.com/
7+
*/
8+
9+
namespace Robin\Ntlm\Crypt\Des;
10+
11+
use InvalidArgumentException;
12+
use Robin\Ntlm\Crypt\CipherMode;
13+
use Robin\Ntlm\Crypt\Exception\CryptographicFailureException;
14+
15+
/**
16+
* An engine used to encrypt data using the DES standard algorithm and
17+
* implemented using the PHP "openssl" extension.
18+
*
19+
* @link http://php.net/openssl
20+
*/
21+
class OpenSslDesEncrypter implements DesEncrypterInterface
22+
{
23+
24+
/**
25+
* Constants
26+
*/
27+
28+
/**
29+
* The default OpenSSL encryption options.
30+
*
31+
* @type int
32+
*/
33+
const DEFAULT_OPENSSL_OPTIONS = OPENSSL_RAW_DATA;
34+
35+
36+
/**
37+
* Properties
38+
*/
39+
40+
/**
41+
* A map of {@link CipherMode}s to the "openssl" extension equivalents.
42+
*/
43+
private static $cipher_mode_map = [
44+
CipherMode::CBC => 'des-cbc',
45+
CipherMode::CFB => 'des-cfb',
46+
CipherMode::ECB => 'des-ecb',
47+
CipherMode::OFB => 'des-ofb',
48+
];
49+
50+
/**
51+
* Whether or not to zero-byte pad the data before encrypting for some
52+
* cipher modes.
53+
*
54+
* @type bool
55+
*/
56+
private $zero_pad;
57+
58+
59+
/**
60+
* Methods
61+
*/
62+
63+
/**
64+
* Constructor
65+
*
66+
* @param bool $zero_pad Whether or not to zero-byte pad the data before
67+
* encrypting for some cipher modes.
68+
*/
69+
public function __construct($zero_pad = true)
70+
{
71+
$this->zero_pad = $zero_pad;
72+
}
73+
74+
/**
75+
* {@inheritDoc}
76+
*/
77+
public function encrypt($key, $data, $mode, $initialization_vector)
78+
{
79+
if (isset(self::$cipher_mode_map[$mode])) {
80+
$mode = self::$cipher_mode_map[$mode];
81+
} else {
82+
throw new InvalidArgumentException('Unknown cipher mode "'. $mode .'"');
83+
}
84+
85+
$options = $this->getOpenSslEncryptionOptions();
86+
87+
$encrypted = openssl_encrypt($data, $mode, $key, $options, $initialization_vector);
88+
89+
if (false === $encrypted) {
90+
throw CryptographicFailureException::forReasonCode(
91+
CryptographicFailureException::CODE_FOR_ENCRYPTION_FAILURE
92+
);
93+
}
94+
95+
return $encrypted;
96+
}
97+
98+
/**
99+
* Gets the OpenSSL encryption options.
100+
*
101+
* @return int The options to use in an OpenSSL encryption call.
102+
*/
103+
private function getOpenSslEncryptionOptions()
104+
{
105+
$options = self::DEFAULT_OPENSSL_OPTIONS;
106+
107+
if ($this->zero_pad) {
108+
$options = $options | OPENSSL_ZERO_PADDING;
109+
}
110+
111+
return $options;
112+
}
113+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Robin NTLM
4+
*
5+
* @copyright 2015 Robin Powered, Inc.
6+
* @link https://robinpowered.com/
7+
*/
8+
9+
namespace Robin\Ntlm\Crypt\Random;
10+
11+
use Robin\Ntlm\Crypt\Exception\CryptographicFailureException;
12+
13+
/**
14+
* A cryptographically secure random byte generator implemented using the PHP
15+
* "openssl" extension.
16+
*
17+
* @link http://php.net/openssl
18+
*/
19+
class OpenSslRandomByteGenerator implements RandomByteGeneratorInterface
20+
{
21+
22+
/**
23+
* Methods
24+
*/
25+
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
public function generate($size)
30+
{
31+
$generated = openssl_random_pseudo_bytes($size, $strong);
32+
33+
if (false === $generated || strlen($generated) !== $size || false === $strong) {
34+
throw CryptographicFailureException::forReasonCode(
35+
CryptographicFailureException::CODE_FOR_RANDOM_DATA_GENERATION_FAILURE
36+
);
37+
}
38+
39+
return $generated;
40+
}
41+
}

0 commit comments

Comments
 (0)